运行 来自父 pom 的外部 Ant 文件

Running external Ant file from a parent pom

我想 运行 从父 POM 构建 Ant build.xml

这可能看起来像这样:

<project>
    <groupId>my.group</groupId>
    <artifactId>my-parent</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>initialize</phase>
                        <configuration>
                            <tasks>
                                <ant antfile="build.xml"/>
                            </tasks>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

除非我将此模块用作父 POM,否则它工作得很好。 问题出在这一行<ant antfile="build.xml"/>。虽然此 POM 运行宁作为父 POM,但插件没有可用的 build.xml 文件。

如何在所有子构建期间从文件(位于父 POM 中)运行Ant 脚本

PS 我试图将 build.xml 打包在某个分类器下,以使其可供子版本使用。但我不知道,如何在 antrun:run.

之前提取我打包的 build.xml

PPS

项目结构:

<root>
  + Parent POM
  | +- pom.xml
  | +- build.xml
  |
  + Component1
  | + Child1
  | | +- src/main/java
  | | +- ...
  | | +- pom.xml
  | |
  | + Child2
  |   +- src/main/java
  |   +-...
  |   +- pom.xml
  |
  + Component2
    + Child3
    | +- src/main/java
    | +- ...
    | +- pom.xml
    |
    + Child4
      +- src/main/java
      +-...
      +- pom.xml

作为奖励:我还想知道以下情况的答案,即父 POM 是独立构建和部署的(不知道自己的子节点),而子节点的构建只能访问父部署的工件(不源代码)。

为了避免 FileNotFoundException,您可以使用配置的 属性 作为 ant 构建文件的前缀。这样的 属性 在父 pom 上将是空的,而在所需模块中将具有正确的前缀(即到父文件夹的相对路径)。

例如,在您的父 POM 中,您的配置如下所示:

<properties>
    <ant.build.dir.prefix></ant.build.dir.prefix>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
                <execution>
                    <phase>initialize</phase>
                    <configuration>
                        <tasks>
                            <ant antfile="${ant.build.dir.prefix}build.xml" />
                        </tasks>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

注意添加到 ant 调用的 ${ant.build.dir.prefix} 前缀。默认情况下它是空的,这意味着文件应该位于与 pom 相同的目录中。

但是,在模块中,您只需要覆盖 属性 的值,如下所示:

<properties>
    <ant.build.dir.prefix>..\</ant.build.dir.prefix>
</properties>

或文件夹层次结构中的任何其他相对路径。

在 运行 时,该值将被替换,因此 ant 文件的路径将动态更改,强制执行 ant 运行 的通用和集中配置(在父 pom ) 和模块中的特定路径配置(通过 属性 前缀)。

我刚刚在带有 echo ant 任务的示例项目中测试了这两种情况(您的配置和带前缀的配置),能够重现您的问题并按照上面的建议修复它。