如何通过maven-antrun-plugin执行ant jar

5
我们在构建中使用Maven和Ant build.xml文件的组合。maven-antrun-plugin如下:
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-antrun-plugin</artifactId>
  <!-- Version 1.8 uses Ant 1.9.4 -->
  <version>1.8</version>
  <executions>
    <execution>
      <id>Compile sources</id>
      <phase>compile</phase>
      <goals>
        <goal>run</goal>
      </goals>
      <configuration>
        <target>
          <mkdir dir="${temp.dir}"/>
          <mkdir dir="${dist.dir}"/>
          <exec dir="${project.basedir}" executable="${ant.exec}" failonerror="true">
            <arg
              line="-f xlatedb.ant.xml -DDLC ${dlc} -lib ${lib.dir}
                -Dtarget.dir ${target.dir}
                -Ddist.dir ${dist.dir}
                -Dtemp.dir ${temp.dir}
                -Dproject.version ${project.version} "/>
          </exec>
        </target>
      </configuration>
    </execution>
  </executions>
</plugin>

maven-antrun-plugin内部使用的是Ant 1.9.4版本。有没有一种方法可以使用内部ant jar来替换对${ant.exec}的依赖?

现在,我们向mvn命令传递ant.exec。使用ant任务不起作用,因为我们还需要传递-lib参数给ant(lib文件夹包含所有已下载的依赖项)。

使用ant.exec方法的好处是我们可以使用ant 1.10.5版本。但是,我可以在依赖项中指定ant 1.10.5并将其下载到lib文件夹中...但是,我需要一种方法来使用该1.10.5 jar启动ant而不是启动ant.bat文件。


1
我猜你所说的 specify ant 1.10.5 in the dependencies 是指项目依赖关系。在这种情况下,您应该更新此插件的依赖项,对于插件,您也可以更新依赖项。 - Robert Scholte
3个回答

3
你可以通过exec:java目标调用ant的主类。 pom.xml
<properties>        
    <target.dir>theTargetDirectory</target.dir>
</properties>
<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.6.0</version>
            <executions>
                <execution>
                    <phase>compile</phase>
                    <goals>
                        <goal>java</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <mainClass>org.apache.tools.ant.launch.Launcher</mainClass>
                <arguments>-f,xlatedb.ant.xml</arguments>
                <systemProperties>
                    <systemProperty>
                        <key>target.dir</key>
                        <value>${target.dir}</value>
                    </systemProperty>
                </systemProperties>
            </configuration>
        </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>org.apache.ant</groupId>
        <artifactId>ant</artifactId>
        <version>1.10.5</version>
        <scope>compile</scope>
    </dependency>
</dependencies>

xlatedb.ant.xml

<project default="print-version">
    <target name="print-version">
        <echo>${ant.version}</echo>
        <echo>${target.dir}</echo>
    </target>
</project>

mvn compile

输出:

--- exec-maven-plugin:1.6.0:java (默认) @ stack-55711525-maven-antexec ---
构建文件: /xyz/project/xlatedb.ant.xml
print-version:
[echo] Apache Ant(TM) 版本 1.10.5,编译于2018年7月10日
[echo] theTargetDirectory
BUILD SUCCESSFUL
总时间:0 秒


注意:如果您想在 jar 过程中使 maven 的类路径可用于 ant,则可以在生命周期的早期运行 copy-dependencies 插件,然后将依赖项输出目录提供为 ant 的类路径。

例如:

<path id="class.path">
  <fileset dir="target">
    <include name="copied-dependencies-dir/*.jar" />
  </fileset>
</path>

0

你可以尝试将插件对 ant 1.9.4 的依赖替换为 ant 1.10.5,方法如下:

<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
  <dependencies>
    <dependency>
      <groupId>org.apache.ant</groupId>
      <artifactId>ant</artifactId>
      <version>1.10.5</version>
    </dependency>
    <executions>
       ...
    </executions>
    <configuration>
       ...
    </configuration>
  </dependencies>
</plugin>

0
根据http://svn.apache.org/viewvc/maven/plugins/tags/maven-antrun-plugin-1.8/pom.xml?view=markup,插件的版本1.8确实使用了Ant 1.9.4。你尝试过通过排除该依赖项来覆盖它吗?
<dependency>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.8</version>
        <type>maven-plugin</type>
        <exclusions>
            <exclusion>
                <groupId>org.apache.ant</groupId>
                <artifactId>ant</artifactId>
            </exclusion>
        </exclusions>
</dependency>

并显式地添加您所需的版本?


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接