从父pom运行外部Ant文件 [英] Running external Ant file from a parent pom

查看:99
本文介绍了从父pom运行外部Ant文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to run an Ant build.xml build from a parent POM.

这可能看起来像这样:

<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文件.

This works just fine unless I use this module as a parent POM. The problem is in this line <ant antfile="build.xml"/>. While this POM is running as a parent POM there is no build.xml file available to the plugin.

在所有子代构建过程中,如何从文件(位于父POM中)运行Ant脚本?

How can I run an Ant script from a file (located in the parent POM) during all child build?

PS 我尝试将build.xml打包在某些分类器下,以使其可用于子版本.但是我不知道如何在antrun:run之前提取打包的build.xml.

PS I tried to package the build.xml under some classifier to make it available to the children builds. But I have no idea, how can I extract my packaged build.xml prior to the antrun:run.

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是独立构建和部署的(不了解自己的孩子),而子POM只能访问父部署的工件(而不是源)代码).

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

推荐答案

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

To avoid the FileNotFoundException you could use a configured property as prefix of the ant build file. Such a property would be empty on the parent pom while would have the right prefix (i.e. relative path to parent folder) in the required modules.

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

For instance, in your parent POM your configuration would look like:

<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.build.dir.prefix}前缀已添加到ant调用中.默认情况下它将为空,这意味着文件应该与pom位于同一目录.

Note the ${ant.build.dir.prefix} prefix added to the ant invocation. By default it will be empty, which means file would be supposed to be located in the same directory as the 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.

在运行时,该值将被替换,因此ant文件的路径将动态更改,从而对ant运行(在父pom中)和模块中的特定路径配置(通过a)进行通用且集中的配置.属性前缀).

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

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

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.

这篇关于从父pom运行外部Ant文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆