Search code examples
mavenenvironment-variablesmaven-antrun-plugin

What is a better way for checking for the existence of an environment variable in the Maven antrun plugin?


I’m using Maven 3.2.3. I have this for my ant run plugin …

                                                    <plugin>
                            <groupId>org.apache.maven.plugins</groupId>
                            <artifactId>maven-antrun-plugin</artifactId>
                            <version>1.8</version>
                            <dependencies>
                                    <dependency>
                                            <groupId>ant-contrib</groupId>
                                            <artifactId>ant-contrib</artifactId>
                                            <version>20020829</version>
                                    </dependency>
                            </dependencies>
                            <executions>
                                    <execution>
                                            <id>create-dodeploy-file</id>
                                            <phase>package</phase>
                                            <configuration>
                                                    <target>
                                                            <taskdef resource="net/sf/antcontrib/antcontrib.properties" />
                                                            <property environment="env"/>
                                                            <if>
                                                                    <isset property="env.JBOSS_HOME"/>
                                                                    <then> 
                                                                            <echo file="${JBOSS_HOME}/standalone/deployments/${project.artifactId}.war.dodeploy" append="false" message="" />
                                                                    </then>
                                                            </if> 
                                                    </target>
                                            </configuration>
                                            <goals>
                                                    <goal>run</goal>
                                            </goals>
                                    </execution>
                                    </executions>
                                    </plugin>

Despite the fact I know $JBOSS_HOME is defined in my environment (I’m able to run

echo $JBOSS_HOME

from the same bash shell in which I run my Maven build), the “isset” directive always returns false. Is there a better way to test for the existence of an environment variable in the Antrun plugin?


Solution

  • A target also has the ability to perform its execution if (or unless) a property has been set. To make a target sense this property, you should add the if (or unless) attribute with the name of the property that the target should react to.

    <target name="build-if" if="env.JBOSS_HOME"/>
    
    <target name="build-unless" unless="env.JBOSS_HOME"/>