【问题标题】:How do I execute simple ant call through maven?如何通过 maven 执行简单的 ant 调用?
【发布时间】:2012-01-18 19:38:18
【问题描述】:

我的项目使用以下命令运行良好:

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

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

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

这是我给 exec 的 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>

【问题讨论】:

  • 好的,我已经解决了这里的问题。显然在争论中,你不能通过空间。所以这个问题解决了。然而,我仍然很好奇如何在 antrun-plugin 中编写它。这是 pom 文件,它不知道如何解析路径。

标签: maven ant


【解决方案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>

不确定要在上面的 sn-p 中的 -lib 中作为参数传递哪个库,但可以将其声明为 plugin dependencies

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

【讨论】:

    【解决方案2】:

    您应该将所需的依赖项直接传递到 antrun 插件声明中,就在 &lt;executions&gt; 元素之后。

        <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 的&lt;dependencyManagement&gt; 的一部分,所以你必须拼出精确的版本。一种解决方法是在与您的中心 &lt;dependencyManagement&gt; 相同的位置声明版本属性,并使用这些属性而不是硬编码版本。

    【讨论】:

      猜你喜欢
      • 2012-05-11
      • 1970-01-01
      • 2019-09-06
      • 2011-02-07
      • 1970-01-01
      • 2018-04-16
      • 1970-01-01
      • 2012-11-04
      • 2011-04-24
      相关资源
      最近更新 更多