【问题标题】:How to execute ant jar from maven-antrun-plugin如何从 maven-antrun-plugin 执行 ant jar
【发布时间】:2019-09-06 18:06:25
【问题描述】:

我们将 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 文件。

【问题讨论】:

  • 我猜specify ant 1.10.5 in the dependencies 是指项目依赖项。在这种情况下你应该更新这个插件的依赖,对于插件你也可以更新依赖。

标签: maven ant


【解决方案1】:

你可以通过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] 2018 年 7 月 10 日编译的 Apache Ant(TM) 版本 1.10.5
[回显] 目标目录
构建成功
总时间:0 秒


注意:如果你想让 maven 类路径在 jar 期间对 ant 可用,我想你可以在生命周期中尽早运行 copy-dependencies 插件,然后提供依赖项输出目录为ant 的类路径。

例如

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

【讨论】:

    【解决方案2】:

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

    <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>
    

    【讨论】:

      【解决方案3】:

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

      他们确实使用 Ant 1.9.4 作为插件的 1.8 版本。您是否尝试通过排除此依赖项来覆盖它:

      <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>
      

      并明确添加您想要的版本?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-11-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-29
        相关资源
        最近更新 更多