如何从maven-antrun-plugin执行ant jar

How to execute ant jar from maven-antrun-plugin

我们将Maven与Ant的build.xml文件结合使用,以用于buid。

Maven-antrun-plugin看起来像这样:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  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文件夹中……但是,除了启动ant.bat文件之外,我还需要一种使用1.10.5 jar启动ant的方法。


您可以通过exec:java目标调用ant主类

pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<properties>        
    <target.dir>theTargetDirectory</target.dir>
</properties>
<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            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>
                -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>
        ant</artifactId>
        <version>1.10.5</version>
        <scope>compile</scope>
    </dependency>
</dependencies>

xlatedb.ant.xml

1
2
3
4
5
6
<project default="print-version">
    <target name="print-version">
        <echo>${ant.version}</echo>
        <echo>${target.dir}</echo>
    </target>
</project>
1
mvn compile

输出:

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

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

例如

1
2
3
4
5
<path id="class.path">
  <fileset dir="target">
    <include name="copied-dependencies-dir/*.jar" />
  </fileset>
</path>

根据http://svn.apache.org/viewvc/maven/plugins/tags/maven-antrun-plugin-1.8/pom.xml?view=markup

实际上,他们将Ant 1.9.4用于该插件的1.8版本。您是否尝试通过排除此依赖项来覆盖此问题:

1
2
3
4
5
6
7
8
9
10
11
12
<dependency>
        <groupId>org.apache.maven.plugins</groupId>
        maven-antrun-plugin</artifactId>
        <version>1.8</version>
        <type>maven-plugin</type>
        <exclusions>
            <exclusion>
                <groupId>org.apache.ant</groupId>
                ant</artifactId>
            </exclusion>
        </exclusions>
</dependency>

并明确添加所需的版本?


您可能会用ant 1.10.5替换对ant 1.9.4的插件依赖性,例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<plugin>
maven-antrun-plugin</artifactId>
<version>1.8</version>
  <dependencies>
    <dependency>
      <groupId>org.apache.ant</groupId>
      ant</artifactId>
      <version>1.10.5</version>
    </dependency>
    <executions>
       ...
    </executions>
    <configuration>
       ...
    </configuration>
  </dependencies>
</plugin>