Retrieve value from external ant file for use in Maven project

49 Views Asked by At

When using the apache antrun plugin -- more formally known as maven-antrun-plugin -- it's possible (since 2010) to expose antrun properties to Maven using the configuration exportAntProperties set to true. This works great for "inlined" <target> code, which is what most online examples I've found use.

However, according to the antrun main project page, it's encouraged to use an external build.xml file instead of "inline" code, quoting:

"[...] it's encouraged to move all your Ant tasks to a build.xml file and just call it from the POM using Ant's <ant/> task."

... however, it seems much harder to access properties set through the build.xml file.

How do you access/retrieve a property from within a build.xml file from a Maven project? This works fine for inlined code, but external files are proving much more difficult.

Code:

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <name>Antrun Tests</name>
    <description>To test inline antrun tasks versus external antrun tasks</description>

    <groupId>org.example</groupId>
    <artifactId>antrun</artifactId>
    <version>1.0.0</version>
    <build>
        <defaultGoal>validate</defaultGoal>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.8</version>
                <executions>
                    <!-- Inline -->
                    <execution>
                        <id>inline-ant</id>
                        <goals><goal>run</goal></goals>
                        <phase>validate</phase>
                        <configuration>
                            <target name="inline-ant">
                                <property name="inline.ant" value="PASS"/>
                                <echo level="info">Ant scope:</echo>
                                <echo level="info">[inline.ant]:   (set to: ${inline.ant})</echo>
                            </target>
                            <exportAntProperties>true</exportAntProperties>
                        </configuration>
                    </execution>

                    <!-- External -->
                    <execution>
                        <id>external-ant</id>
                        <goals><goal>run</goal></goals>
                        <phase>validate</phase>
                        <configuration>
                            <target name="external-ant">
                                <ant antfile="build.xml"/>
                            </target>
                            <exportAntProperties>true</exportAntProperties>
                        </configuration>
                    </execution>

                    <!-- Echo results -->
                    <execution>
                        <id>results</id>
                        <goals><goal>run</goal></goals>
                        <phase>validate</phase>
                        <configuration>
                            <target name="results">
                                <!--
                                     Note: This conditional logic is superfluous!
                                     Ant can NOT set set a variable if it's already
                                     set! However, since Maven's default behavior allows
                                     this, I'm adding a clear, unnecessary guard as to
                                     not confuse causal onlookers. <3
                                -->
                                <!-- Set to failed if not set -->
                                <condition property="inline.ant" value="FAILED">
                                    <not><isset property="inline.ant"/></not>
                                </condition>
                                <condition property="external.ant" value="FAILED">
                                    <not><isset property="external.ant"/></not>
                                </condition>

                                <!-- Echo to console -->
                                <echo level="info">Maven scope:</echo>
                                <echo level="info">[inline.ant]:   ${inline.ant}</echo>
                                <echo level="info">[external.ant]: ${external.ant}</echo>
                            </target>
                            <exportAntProperties>true</exportAntProperties>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

... and relevant build.xml:

<project default="external-ant">
    <target name="external-ant">
        <property name="external.ant" value="PASS"/>
        <echo level="info">Ant scope:</echo>
        <echo level="info">[external.ant]:   (set to: ${external.ant})</echo>
    </target>
</project>

Edit: I think this may actually be a side-effect of ant's <ant antfile="..."> task, and the antrun plugin is collateral damage to it.

For example, If I had access to <import file="..."/>, I believe it would work, but ant requires imports at the <project> level and antrun doesn't allow anything higher-level than the <target> level (<target> must be a child of <project>).

For example, even if I echo in the same exact ant target, I still can't see the properties, suggesting it a nuance of forcing people to use <ant antfile="..."> to begin with, conflicting with expected behavior, and conflicting with the "encouraged" use from antrun's documentation.

0

There are 0 best solutions below