How can I set a environment variables in Maven per run?

Edison picture Edison · Feb 8, 2012 · Viewed 19.4k times · Source

In my project, we've created a Maven module to get the specific JBoss AS and unpacked.
Then all the test cases can be run under this Jboss AS as embedded container.
We're using jboss-ejb3-embedded-standalone to call the embedded container, however, it just find the JBOSS_HOME from environment variables and use that one to run. Thus we have to update the JBOSS_HOME per mvn install.

I tried to do this in maven by introduce exec-maven-plugin as below:

    <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.2.1</version>
    <configuration>
        <executable>env</executable>
        <environmentVariables>
            <JBOSS_HOME>
                C:/Sample/embedded-container/jboss-${version.org.jboss.jbossas}
            </JBOSS_HOME>
        </environmentVariables>
    </configuration>
    <executions>
        <execution>
            <id>resetJbossHome</id>
            <phase>integration-test</phase>
            <goals>
                <goal>exec</goal>
            </goals>
        </execution>
    </executions>
</plugin>

In the output of console, I can see

[INFO] --- exec-maven-plugin:1.2.1:exec (resetJbossHome) @ test-embedded ---
....
JBOSS_HOME=C:/Sample/embedded-container/jboss-6.1.0.Final

....

But when launching JBOSS, it's still running the one with origin JBOSS_HOME set.

Besides, I've tried using maven-antrun-plugin too.

<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
        <execution>
            <id>copyRelease</id>
            <phase>pre-integration-test</phase>
            <configuration>
                <tasks>
                    <exec executable="env">
       <env key="JBOSS_HOME" value="C:/Sample/embedded-container/jboss-${version.org.jboss.jbossas}"/>
    </exec>
                </tasks>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>

It turns out the same.

Am I wrong on the configuration or there're some better way?

Answer

ndeverge picture ndeverge · Feb 8, 2012

Take a look at the Maven profiles.

You can define one profile for testing, one for production, with different properties, such as

<profiles>
  <profile>
    <id>test</id>
    <jboss.home>PATH TO JBOSS TEST INSTANCE</jboss.home>
  </profile>
  <profile>
    <id>prod</id>
    <jboss.home>PATH TO JBOSS PROD INSTANCE</jboss.home>
  </profile>
</profiles>

And in your exec plugin :

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