简体   繁体   English

从父pom运行外部Ant文件

[英]Running external Ant file from a parent pom

I would like to run an Ant build.xml build from a parent POM. 我想从父POM运行Ant build.xml构建。

This may look like this: 可能看起来像这样:

<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>

This works just fine unless I use this module as a parent POM. 除非我将此模块用作父POM,否则此方法工作正常。 The problem is in this line <ant antfile="build.xml"/> . 问题在这行<ant antfile="build.xml"/> While this POM is running as a parent POM there is no build.xml file available to the plugin. 当此POM作为父POM运行时,插件没有可用的build.xml文件。

How can I run an Ant script from a file (located in the parent POM) during all child build? 在所有子构建期间,如何从文件 (位于父POM中)运行Ant脚本?

PS I tried to package the build.xml under some classifier to make it available to the children builds. PS我尝试将build.xml打包在某些分类器下,以使其可用于子版本。 But I have no idea, how can I extract my packaged build.xml prior to the antrun:run . 但是我不知道如何在antrun:run之前提取打包的build.xml

PPS PPS

The project structure: 项目结构:

<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

As a bonus: I also would like to know the answer for the situations, where the parent POM get built and deployed independently (not knowing own child) and children get built having only access to the parent deployed artifacts (not the source code). 另外,我还想知道以下情况的答案:父POM是独立构建和部署的(不知道自己的孩子),而子POM只能访问父部署的工件(而不是源代码)。

To avoid the FileNotFoundException you could use a configured property as prefix of the ant build file. 为了避免FileNotFoundException您可以使用配置的属性作为ant构建文件的前缀。 Such a property would be empty on the parent pom while would have the right prefix (ie relative path to parent folder) in the required modules. 这样的属性在父pom上为空,而在所需模块中具有正确的前缀(即,父文件夹的相对路径)。

For instance, in your parent POM your configuration would look like: 例如,在您的父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>

Note the ${ant.build.dir.prefix} prefix added to the ant invocation. 请注意, ${ant.build.dir.prefix}前缀添加到ant调用中。 By default it will be empty, which means file would be supposed to be located in the same directory as the pom. 默认情况下它将为空,这意味着文件应该与pom位于同一目录中。

However, in modules you would just need to override the value of the property, as following: 但是,在模块中,您只需要覆盖属性的值,如下所示:

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

Or any other relative path in the folders hierarchy. 或文件夹层次结构中的任何其他相对路径。

At runtime, the value will be replaced and as such the path to the ant file will dynamically change, enforcing a common and centralized configuration of the ant run (in the parent pom) and a specific path configuration in modules (via a property prefix). 在运行时,该值将被替换,因此ant文件的路径将动态更改,从而对ant运行(在父pom中)和模块中的特定路径配置(通过属性前缀)实施通用且集中的配置。 。

I just tested both cases (your configuration and the prefixed one) in a sample project with an echo ant task, being able to reproduce your issue and to fix it as suggested above. 我只是在一个示例项目中使用echo ant任务测试了这两种情况(您的配置和带前缀的一种),能够重现您的问题并按照上面的建议解决它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM