Is it possible to set a maven property from ant?
Asked Answered
D

3

9

I tried to use the maven-antrun-plugin to check in a first execution if a file exists and then set a property accordingly. In another execution (another phase) of the antrun-plugin I want to make use of the property. But the property set in one execution cannot be used in another execution since it's an ant and not a maven property and doesn't get propagated.

Is it possible to propagate the ant property to maven or in other words set a maven property from ant?

Using another Maven build like in this question is not an option.

Another way that might work somehow would be an external build.xml but that's not an option too, because I have to keep things in one pom.

I've read about using GMaven to set a Maven property but I would like to stay with ant.

Disciple answered 21/2, 2011 at 9:45 Comment(1)
D
9

As of version 1.7 of the maven-antrun-plugin it is possible according to the plugin documentation (see exportAntProperties). So, I guess, in earlier versions: it's not ;-).

Disciple answered 21/12, 2011 at 11:24 Comment(0)
D
1

you can redirect your strategy to activation of different profiles depending on the existence of the file instead of antrun-plugin:

<profiles>
    <profile>
        <id>notExist</id>
        <activation>
          <file>
            <missing>target/maven-archiver/notExist.properties</missing>
          </file>
        </activation>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.3</version>
                <executions>
                  <execution>
                    <phase>package</phase>
                    <goals>
                      <goal>run</goal>
                    </goals>
                    <configuration>
                        <tasks>
                            <echo>not exist</echo>
                        </tasks>
                    </configuration>
                  </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</profile>

<profile>
    <id>exist</id>
    <activation>
      <file>
        <exists>target/maven-archiver/pom.properties</exists>
      </file>
    </activation>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.3</version>
                <executions>
                  <execution>
                    <phase>package</phase>
                    <goals>
                      <goal>run</goal>
                    </goals>
                    <configuration>
                        <tasks>
                            <echo>not exist</echo>
                        </tasks>
                    </configuration>
                  </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</profile>

You can use activation profiles maven features to distinguish between one configuration and another according the activation criteria.

I use the antrun-plugin in example only for do the echo

Darky answered 5/8, 2011 at 20:56 Comment(2)
Hi Michael and thanks for your effort. But my question is about property propagation from the maven-antrun plugin to the main maven execution and not about profile activation.Disciple
But @Michel's answer still seems to address your intended aim, which seems to be "to check... if a file exists and then set a property accordingly". A profile can be activated based on the file's existence, and the property can be set in the profile.Argo
A
1

Yes, with even a default value. Example setting install.path with echo:

<plugin>
    <!-- Workaround maven not being able to set a property conditionally based on environment variable -->
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.8</version>
    <executions>
        <execution>
            <phase>validate</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <exportAntProperties>true</exportAntProperties>
                <target>
                    <property environment="env"/>
                    <condition property="install.path" value="${env.INSTALL_HOME}" else="C:\default-install-home">
                        <isset property="env.INSTALL_HOME" />
                    </condition>
                    <echo message="${install.path}"/>
                </target>
            </configuration>
        </execution>
    </executions>
</plugin>
Amby answered 21/4, 2016 at 12:50 Comment(1)
Note that if you try to use ${install.path} in another maven-antrun-plugin, they must have the same version.Amby

© 2022 - 2024 — McMap. All rights reserved.