繁体   English   中英

从父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是独立构建和部署的(不知道自己的孩子),而子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.build.dir.prefix}前缀添加到ant调用中。 默认情况下它将为空,这意味着文件应该与pom位于同一目录中。

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

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

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

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

我只是在一个示例项目中使用echo ant任务测试了这两种情况(您的配置和带前缀的一种),能够重现您的问题并按照上面的建议解决它。

暂无
暂无

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

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