【问题标题】:Running external Ant file from a parent pom从父 pom 运行外部 Ant 文件
【发布时间】:2016-01-31 22:45:23
【问题描述】:

我想从父 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,否则它工作得很好。 问题出在&lt;ant antfile="build.xml"/&gt; 这一行。虽然此 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 是独立构建和部署的(不知道自己的子节点),而子节点只能访问父节点部署的工件(而不是源代码)代码)。

【问题讨论】:

  • 好的。那么你是从根项目运行 Maven 吗?如果有的话,你能发布整个堆栈跟踪吗?
  • 我正在从两个目录运行我的构建:根目录和子目录。我得到的例外是java.io.FileNotFoundException: &lt;root&gt;\Component1\Child2\build.xml
  • 这两者是什么意思?通常,您应该只从&lt;root&gt; 运行mvn clean install
  • 我的项目很大,所以开发人员只有在更改分支或结帐后才从根目录运行mvn clean install。大约需要 15 分钟。大多数情况下,构建是从 ChildX 目录触发的。
  • mvn clean install 用于多模块构建通常是错误的方式...如果您想加快构建使用的第二个:mvn -pl TheModuleYouWouldLikeToBuild -amd clean package ...除此之外,您是否尝试过使用@ 987654335@?此外,为什么需要 Ant 部件?这样做的目的是什么?

标签: maven maven-antrun-plugin


【解决方案1】:

要避免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 run 的通用和集中配置(在父 pom 中)和模块中的特定路径配置(通过属性前缀)。

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2017-04-23
  • 2017-01-13
  • 1970-01-01
  • 2013-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-23
相关资源
最近更新 更多