views:

843

answers:

1
+3  Q: 

Maven command line

Hi all,

Is it possible to set environmental variables in a build profile as opposed to setting them in the command line?

For instance, I want to enable the debugger when I'm using my test environment (-Denv=test).

I want maven to do this:

export MAVEN_OPTS="-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=4000,server=y,suspend=n"

This way, I can quickly attach a debugger without having to type the same repeated line over and over.

I don't believe the helps me in this case:

<plugin>
...
<!--    Automatically enable the debugger when running Jetty -->
        <argLine>-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=4000,server=y,suspend=n</argLine>
       </configuration>
...
</plugin>

Walter

+4  A: 

In recent versions of Maven you can activate the debugger by running mvnDebug rather than mvn, the mvnDebug bat/sh file sets MVN__DEBUG_OPTS and passes them to the java.exe. The values passed are:

set MAVEN_DEBUG_OPTS=-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000

If that isn't sufficient, this may work (note I've not yet tested this, I'll update once I have). Maven reads properties prefixed with "env." from the environment, you may be able to set environment variables by prefixing with the same. i.e.:

<profile>
  <id>dev</id>
  <properties>
    <env.MAVEN_OPTS>-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000<env.MAVEN_OPTS>
  </properties>
</profile>

Update: The surefire plugin allows you to specify system properties to be used during test execution. The configuration is as follows:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.4.2</version>
  <configuration>
    <systemProperties>
      <property>
        <name>propertyName</name>
        <value>propertyValue</value>
      </property>
    </systemProperties>
  </configuration>
</plugin>

If none of those work for you, it is possible to write a small plugin configured in your profile that binds to the initialize phase and sets your variables. The plugin would have configuration like this:

<plugin>
  <groupId>name.seller.rich</groupId>
  <artifactId>maven-environment-plugin</artifactId>
  <version>0.0.1</version>
  <executions>
    <execution>
      <id>set-properties</id>
      <phase>initialize</phase>
      <goals>
        <goal>set-properties</goal>
      </goals>
    </execution>
  </executions>                              
  <configuration>
    <properties>
      <env.MAVEN_OPTS>-Xdebug -Xnoagent -Djava.compiler=NONE 
          -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000<env.MAVEN_OPTS>
    </properties>
  </configuration>
</plugin>

during execution the plugin would set each passed property using System.setProperty(). If the first two aren't suitable or don't work this should address your issue.

Rich Seller
Hi Rich,The mvnDebug command worked, but it appears to hang quite a bit and eat a lot of memory. I haven't had any luck with those other options yet, but I will keep trying.Walter