首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从maven-antrun-plugin执行ant jar

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

Stack Overflow用户
提问于 2019-04-16 23:17:34
回答 3查看 1K关注 0票数 5

我们将Maven与Ant build.xml文件结合使用来创建buids。

maven-antrun-plugin如下所示:

代码语言:javascript
复制
<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.exec}的依赖替换为对该内部ant jar的调用?

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

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

EN

回答 3

Stack Overflow用户

发布于 2019-04-23 22:57:52

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

pom.xml

代码语言:javascript
复制
<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

代码语言:javascript
复制
<project default="print-version">
    <target name="print-version">
        <echo>${ant.version}</echo>
        <echo>${target.dir}</echo>
    </target>
</project>
代码语言:javascript
复制
mvn compile

输出:

- exec-maven-plugin:1.6.0:java (默认)@ stack-55711525-maven-antexec

构建文件: /xyz/project/xlatedb.ant.xml

打印版本:

代码语言:javascript
复制
  _[echo] Apache Ant(TM) version 1.10.5 compiled on July 10 2018_
代码语言:javascript
复制
  _[echo] theTargetDirectory_

构建成功

总时间:0秒

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

例如:

代码语言:javascript
复制
<path id="class.path">
  <fileset dir="target">
    <include name="copied-dependencies-dir/*.jar" />
  </fileset>
</path>
票数 3
EN

Stack Overflow用户

发布于 2019-04-25 19:26:00

您可以使用ant 1.10.5替换ant 1.9.4上的插件依赖项,如下所示:

代码语言:javascript
复制
<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
EN

Stack Overflow用户

发布于 2019-04-25 19:30:07

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

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

代码语言:javascript
复制
<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>

然后显式地添加你想要的版本?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55711525

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档