How do I execute simple ant call through maven?

2019-08-20 07:41发布

问题:

My project runs perfectly fine with following commands:

C:\project\<project_name>\ant -lib ant\lib -buildfile applications/<sub-project-path>/ant/build.xml deploy

However, if I wrap this command either in maven-antrun-plugin or exec-maven-plugin in pom, I get all kinds of path issues.

For maven-antrun-plugin, it seems the certain properties can not be loaded due to path issue. In exec-maven-plugin, it seems that ant target never got passed in correctly.
Can someone please advice how I can apply this in a pom file? Much appreciated.

This is my pom for exec:

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2.1</version>
            <executions>
                <execution>
                    <phase>process-resources</phase>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                    <configuration>
                    <executable>ant</executable>
                    <workingDirectory>${basedir}</workingDirectory>
                    <arguments>
                        <argument>'-lib ant/lib'</argument>
                        <argument>'-buildfile $basedir/<project-path>/build.xml'</argument>
                        <argument>deploy</argument>
                    </arguments>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

回答1:

You should pass needed dependencies directly into antrun plugin declaration, right after <executions> element.

    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.2.1</version>
        <executions>
            ...
        </executions>
        <dependencies>
          <dependency>
            <groupId>ant-contrib</groupId>
            <artifactId>ant-contrib</artifactId>
            <version>${ant-contrib.version}</version>
            <exclusions>
              <exclusion>
                <groupId>ant</groupId>
                <artifactId>ant</artifactId>
              </exclusion>
            </exclusions>
          </dependency>
          <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>jasper</artifactId>
            <version>${tomcat.compile.version}</version>
          </dependency>
          <dependency>
            <groupId>com.sun</groupId>
            <artifactId>tools</artifactId>
            <version>${java.version}.0</version>
            <scope>system</scope>
            <systemPath>${jdk.home}/tools.jar</systemPath>
          </dependency>
        </dependencies>
    </plugin>

I've included some libraries that I use in our project, so that you have an example. Note, that if your build uses some non-standard ( i.e. something outside java.lang ) Java API classes, you have to pass tools.jar as a dependency.

Also, if you use ant-contrib do not forget to exclude ant as a dependency, because it is dependent on some ancient version of ant and you will get a version collision.

Another annoying thing is that dependency assigned directly to plugin execution are not part of POM's <dependencyManagement>, so you have to spell out precise versions. One workaround is to declare version properties in the same place as your central <dependencyManagement> and use the properties instead of hardcoded versions.



回答2:

Haven't tried it, but you could do something similar as documented in the maven antrun plugin example.

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.7</version>
        <executions>
          <execution>
            <id>ant</id>
            <phase>process-resources</phase>
            <configuration>
              <target>
                <ant antfile="${basedir}/<project-path>/build.xml">
                  <target name="deploy"/>
                </ant>
              </target>
            </configuration>
            <goals>
              <goal>run</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

Not sure what library you want to pass as argument in -lib in your snippet above, but the same can be declared as plugin dependencies.

Do note that this plugin does not care about the existence of an ant installation on your system. It downloads necessary ant libraries.



标签: maven ant