首页 文章

如何通过maven执行简单的ant调用?

提问于
浏览
0

我的项目使用以下命令完美运行:

C:\project\<project_name>\ant -lib ant\lib -buildfile applications/<sub-project-path>/ant/build.xml deploy

但是,如果我在pom中的maven-antrun-plugin或exec-maven-plugin中包装此命令,我会遇到各种路径问题 .

对于maven-antrun-plugin,由于路径问题,似乎无法加载某些属性 . 在exec-maven-plugin中,似乎ant目标从未正确传递过 .
有人可以建议我如何在pom文件中应用它吗?非常感激 .

这是我执行的pom:

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2.1</version>
            <executions>
                <execution>
                    <phase>process-resources</phase>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                    <configuration>
                    <executable>ant</executable>
                    <workingDirectory>${basedir}</workingDirectory>
                    <arguments>
                        <argument>'-lib ant/lib'</argument>
                        <argument>'-buildfile $basedir/<project-path>/build.xml'</argument>
                        <argument>deploy</argument>
                    </arguments>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

2 回答

  • 0

    您应该在 <executions> 元素之后直接将所需的依赖项传递给antrun插件声明 .

    <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2.1</version>
            <executions>
                ...
            </executions>
            <dependencies>
              <dependency>
                <groupId>ant-contrib</groupId>
                <artifactId>ant-contrib</artifactId>
                <version>${ant-contrib.version}</version>
                <exclusions>
                  <exclusion>
                    <groupId>ant</groupId>
                    <artifactId>ant</artifactId>
                  </exclusion>
                </exclusions>
              </dependency>
              <dependency>
                <groupId>org.apache.tomcat</groupId>
                <artifactId>jasper</artifactId>
                <version>${tomcat.compile.version}</version>
              </dependency>
              <dependency>
                <groupId>com.sun</groupId>
                <artifactId>tools</artifactId>
                <version>${java.version}.0</version>
                <scope>system</scope>
                <systemPath>${jdk.home}/tools.jar</systemPath>
              </dependency>
            </dependencies>
        </plugin>
    

    我已经包含了一些我在项目中使用的库,所以你有一个例子 . 请注意,如果您的构建使用某些非标准(即 java.lang 之外的东西)Java API类,则必须将 tools.jar 作为依赖项传递 .

    另外,如果您使用 ant-contrib ,请不要忘记将 ant 排除为依赖项,因为它依赖于某些古老版本的ant,您将获得版本冲突 .

    另一个令人讨厌的事情是,直接分配给插件执行的依赖项不是POM的 <dependencyManagement> 的一部分,所以你必须拼出精确的版本 . 一种解决方法是在与中央 <dependencyManagement> 相同的位置声明版本属性,并使用属性而不是硬编码版本 .

  • 1

    没试过,但是你可以做类似于maven antrun插件example中记载的类似的东西 .

    <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.7</version>
            <executions>
              <execution>
                <id>ant</id>
                <phase>process-resources</phase>
                <configuration>
                  <target>
                    <ant antfile="${basedir}/<project-path>/build.xml">
                      <target name="deploy"/>
                    </ant>
                  </target>
                </configuration>
                <goals>
                  <goal>run</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
    

    不确定您希望在上面的代码段中的 -lib 中作为参数传递哪个库,但同样可以声明为 plugin dependencies .

    请注意,此插件并不关心系统上是否存在ant安装 . 它下载必要的ant库 .

相关问题