Run Ant task from Maven

I'm using Ant to build a custom jar library, which then I'm using in Maven as dependency.

<dependency>
    <groupId>test-lib</groupId>
    <artifactId>test-lib</artifactId>
    <version>1.0.0system</scope>
    <systemPath>${basedir}/src/main/webapp/WEB-INF/lib/test-lib-1.0.0.jar</systemPath>
</dependency>

So, basically what I do now is:

1) run ant to build custom library (test-lib-1.0.0.jar)
2) run: mvn compile, to compile my project using custom library among others.

Is there an option for me to do all this (packaing custom jar & compiling project) from Maven? I've found maven run plugin, and here are my settings:

<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.4
    <executions>
        <execution>
            <phase>?????what to put here?????/phase>
            <configuration>
                <tasks>
                    <ant antfile="${basedir}/build.xml">
                        <target name="prepare-test-lib" />
                    </ant>
                </tasks>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>

But, when running: mvn compile it complains about missing artifact: test-lib-1.0.0.jar . I've used compile, generate-resouces,... in <phase/> tag, but nothing seems to work.

Is it possible to solve this somehow using this plugin?


When using the Maven Antrun Plugin, Maven tries to resolve the dependencies to build ClassPaths for the AntRun invocation and you thus face a chicken and egg problem : you can't declare a dependency that will be created during AntRun execution that requires this dependency to run. This can't work.

My recommendation would be to mavenize your test-lib , to include it in your project build and to declare a regular dependency on it. In other words, what I mean is moving from Ant to Maven to build your test-lib and setting up a multi-modules project. To illustrate things more "visually", something like this:

my-project
|-- my-module
|   |-- src
|   |   `-- main
|   |       `-- java
|   `-- pom.xml
|-- test-lib
|   |-- src
|   |   `-- main
|   |       `-- java
|   `-- pom.xml
`-- pom.xml

Where my-project/pom.xml is an aggregating pom with a <packaging>pom</packaging> and list the modules under a <modules> element:

<modules>
  <module>my-module</module>
  <module>test-lib</module>
</modules>

And where my-module/pom.xml declares a dependency on the test-lib artifact:

<dependency>
  <groupId>your.group.id</groupId>
  <artifactId>test-lib</artifactId>
  <version>1.0-SNAPSHOT</version>
</dependency>

I'm just giving a very high-level overview here, you need to read the documentation a bit for the details, I can't cover everything. Start with the first book from Sonatype (link below).

But that would be the right way to go (and you should just not (ab)use system scoped dependencies).

References

  • Maven: The Definitive Guide
  • 链接地址: http://www.djcxy.com/p/44318.html

    上一篇: Maven化ANT项目

    下一篇: 从Maven运行Ant任务