1

Currently running into a weird problem with a Maven build that is making a call to ant, within the parent pom the following property is defined: -

<jboss.home>${env.JBOSS_HOME}</jboss.home>

I am trying to override this by passing in -Djboss.home when I run Maven.

This build contains a profile that when activate calls an Ant build: -

<ant antfile="build.xml" inheritRefs="true">
    <target name="all"/>
</ant>

The Ant script uses the property: -

<property name="jboss.dir" value="${jboss.home}"/>

An subsequently outputs it: -

<echo message="jboss dir is: ${jboss.dir}"/>

The problem is that the value it output is ${env.JBOSS_HOME}

Other properties in the parent pom I can override from the command line.

Even the jboss.home property does appear to be overridden if use elsewhere within the Maven build.

After trying various combinations of command it almost appears that the set of properties passed to Ant are resolved from the poms BEFORE any overrides from the command line. If I set the JBOSS_HOME environment variable then all places this variable is used have the correct values.

Is there something I am missing to be able to override this variable on the command line and have the overridden value used in the Ant script?

Darran L
  • 922
  • 1
  • 8
  • 14

3 Answers3

3

Stumbled into this problem today, and did discover an answer. This link provides some background: http://technotes.khitrenovich.com/properties-resolution-maven-implications-antrun-plugin/

Maven properties will work just fine if the property is referenced inline within the <target>. e.g:

    <execution>
      ..
      <configuration>
        <target>
          <echo message="Command-line override of external.property will work OK like this: ${external.property}"/>
        </target>
      </configuration>
    </execution>

However, if you use an external ant file written like this, many/most properties will work, BUT command-line overrides will not be passed into the ant file. You will get the non-overridden value instead.

    <execution>
      ..
      <configuration>
        <target>
          <property name="external.property" value="${external.property}" />
          <ant antfile="build.xml" target="all" />
        </target>
      </configuration>
    </execution>

Instead, for external ant files, use this syntax. The command line value will be passed through correctly to the external antfile:

    <execution>
      ..
      <configuration>
        <target>
          <ant antfile="build.xml" target="all" >
            <property name="external.property" value="${external.property}" />
          <ant/>
        </target>
      </configuration>
    </execution>
Jeff Bennett
  • 996
  • 7
  • 18
1

The problem is that ${env.JBOSS_HOME} is a system environment variable and you are instead passing to the JVM a system property -Djboss.home=.... These are two different things. Apart from that, any variables, args and so on in the Java world are case sensitive.

carlspring
  • 31,231
  • 29
  • 115
  • 197
0

You seem do be doing all the right things, it works for me, I have made a working example with 2 pom.xml files. The parent pom looks like this:

<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>
    <groupId>com.stackoverflow.test</groupId>
        <artifactId>test</artifactId>
        <version>1.0</version>
    <name>Test</name>
    <packaging>pom</packaging>
    <modules>
        <module>test-ant-properties</module>
    </modules>
    <properties>
        <jboss.home>${env.JBOSS_HOME}</jboss.home>
    </properties>
</project>

Then I created a module pom.xml in the sub-folder test-ant-properties that looks like this:

<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>
    <parent>
        <groupId>com.stackoverflow.test</groupId>
        <artifactId>test</artifactId>
        <version>1.0</version>
    </parent>
    <artifactId>test-ant-properties</artifactId>
    <name>Test maven ant properties</name>
    <profiles>
        <profile>
            <id>jboss</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <version>1.6</version>
                        <executions>
                            <execution>
                                <id>jboss-ant</id>
                                <phase>install</phase>
                                <configuration>
                                    <target>
                                        <property name="jboss.dir" value="${jboss.home}"/>
                                        <echo message="jboss dir is: ${jboss.dir}"/>                                
                                    </target>
                                </configuration>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>

I don't have JBOSS installed so for testing purpose I set the environement variable to test1234 like this: set JBOSS_HOME=test1234

When I execute the parent pom with the jboss profile

mvn install -Pjboss

I get the following result: [echo] jboss dir is: test1234

When I execute the same command with the jboss.home setting

mvn install -Pjboss -Djboss.home=my_custom_variable

I get the following result: [echo] jboss dir is: my_custom_variable

seberenimer
  • 123
  • 5