【问题标题】:How to wrap an Ant build with Maven?如何用 Maven 包装 Ant 构建?
【发布时间】:2010-11-30 05:23:23
【问题描述】:

我们将 maven 用于我们的大型产品。我们所有的工件都使用 maven 部署目标部署到共享存档存储库。我现在正在集成具有 ant 构建的第三方产品。我知道如何使用 antrun 插件从 maven 调用 ant 目标,但我不确定如何在这种情况下设置 pom。我不希望 maven 实际生成工件,但我确实希望它在运行 maven 部署目标时拉取由 ant 构建的工件。

我计划让 pom 与 build.xml 相邻。 pom 将使用包目标中的 antrun 插件在适当的时候调用 ant 目标来构建 .war 工件。

问题:

a) 我正在创建一个 .war 文件,但它是通过 ant 而不是 Maven 创建的,因此在 pom 中使用 war 包装类型没有意义。我的包装类型应该是什么?

b) 如何让 maven 从我的 ant 输出目录中提取工件以实现部署目标?

c) 如果 A 和 B 没有好的答案,那么是否有 ant 任务可以复制 maven 部署功能以将我的 .war 工件放入共享存储库?

【问题讨论】:

    标签: maven-2 ant


    【解决方案1】:

    您可以使用maven-antrun-plugin 来调用ant 构建。然后使用build-helper-maven-plugin将ant生成的jar附加到项目中。附加的工件将与 pom 一起安装/部署。
    如果你指定你的项目打包pom,Maven不会和ant build冲突。

    在下面的例子中,假设ant build.xml在src/main/ant,有一个compile目标,输出到ant-output.jar

    <plugin>
      <artifactId>maven-antrun-plugin</artifactId>
      <executions>
        <execution>
          <phase>process-resources</phase>
          <configuration>
            <tasks>
              <ant antfile="src/main/ant/build.xml" target="compile"/>
            </tasks>
          </configuration>
          <goals>
            <goal>run</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>build-helper-maven-plugin</artifactId>
      <version>1.3</version>
      <executions>
        <execution>
          <id>add-jar</id>
          <phase>package</phase>
          <goals>
            <goal>attach-artifact</goal>
          </goals>
          <configuration>
            <artifacts>
              <artifact>
                <file>${project.build.directory}/ant-output.jar</file>
                <type>jar</type>
              </artifact>
            </artifacts>
          </configuration>
        </execution>
      </executions>
    </plugin>
    

    【讨论】:

    • 非常有帮助的答案 Rich,一旦我在我的项目中使用它,我会接受你的答案。
    • 很好的建议。不幸的是,它不适用于 WAR 文件 - Maven 战争插件似乎并不关心附加的 WAR 工件。
    • P.S.请原谅我,一旦我将包装规范更改为 pom,它实际上工作正常 - 附加的 WAR 不再干扰生成的 WAR,因此最终会正确添加到 repo 中。
    【解决方案2】:

    正如我在另一个问题中所写的那样,您实际上可以使用 multiple ant run goals 用 Maven 包装一个 ANT 项目。假设您现有的 ant 项目具有清理和构建任务,这可能是包装项目的一种有用方式,因此您可以使用 maven 目标并将其映射到现有的 ant 代码。

    【讨论】:

    • 谢谢萨尔,很好的回答。不能完全解决我上面的 a、b 和 c,但无论如何都有帮助。
    【解决方案3】:
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-install-plugin</artifactId>
        <version>2.3.1</version>
        <executions>
            <execution>
                <id>install-library</id>
                <phase>install</phase>
                <goals>
                    <goal>install-file</goal>
                </goals>
                <configuration>
                    <groupId>x.x</groupId>
                    <artifactId>ant-out-atifacts</artifactId>
                    <version>${project.version}</version>
                    <file>ant-output.jar</file>
                    <packaging>zip</packaging>
                </configuration>
            </execution>
        </executions>
    </plugin>
    

    【讨论】:

      【解决方案4】:

      请参考:Why you should use the Maven Ant Tasks instead of Maven or Ivy

      具体来说,如何从 Ant 调用 Maven 目标可以在此示例中找到:

      http://code.google.com/p/perfbench/source/browse/trunk/perfbench/grails-gorm/build.xml

      通过以上信息,您应该能够实现您所需要的。如果您有任何问题,请告诉我。

      【讨论】:

        猜你喜欢
        • 2012-08-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-14
        • 2013-09-07
        • 2015-12-30
        • 2020-10-20
        • 1970-01-01
        相关资源
        最近更新 更多