我们将 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} 的依赖?
现在我们将 ant.exec 传递给 mvn 命令。使用 ant 任务不起作用,因为我们还需要将 -lib 传递给 ant。 (lib 文件夹包含所有下载的依赖项)。
使用ant.exec这种方式的好处是我们可以使用ant 1.10.5。然而,我可以在依赖项中指定 ant 1.10.5 并将该 jar 下载到 lib 文件夹...但我需要一种方法来启动带有 1.10.5 jar 的 ant,而不是启动 ant.bat 文件。
最佳答案
您可以通过 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
打印版:
[echo] Apache Ant(TM) 1.10.5 2018年7月10日编译
[echo] 目标目录
build 成功
总时间:0秒
注意:如果你想在
jar
期间让 maven 类路径对 ant 可用,我猜你可以运行copy-dependencies
插件在生命周期中足够早,然后将依赖项输出目录作为类路径提供给 ant。例如
<path id="class.path">
<fileset dir="target">
<include name="copied-dependencies-dir/*.jar" />
</fileset>
</path>
关于maven - 如何从 maven-antrun-plugin 执行 ant jar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55711525/