Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set up an environment variable in mvn pom?

Tags:

maven

pom.xml

How can i set up an environment variable (in other words internally accessible by System.getenv("APP_HOME") in a pom file?

APP_HOME=/path/home 

I realize i can set it up in .profile, but wonder if pom can do the same trick.

Per bmargulies's suggestion below, i tried the following, without luck

<build>     <finalName>KvpStore</finalName>     <plugins>         <plugin>             <artifactId>maven-compiler-plugin</artifactId>             <version>2.3.2</version>             <configuration>                 <source>1.6</source>                 <target>1.6</target>                 <encoding>UTF-8</encoding>             </configuration>         </plugin>         <plugin>             <groupId>org.apache.maven.plugins</groupId>             <artifactId>maven-surefire-plugin</artifactId>             <version>2.12.4</version>             <configuration>                 <includes>                     <include>**/*Test*.java</include>                 </includes>                 <environmentVariables>                     <APP_NAME>blah_blah</APP_NAME>  <------------------------                 </environmentVariables>             </configuration>         </plugin>     </plugins> </build> 
like image 724
James Raitsev Avatar asked Dec 25 '12 01:12

James Raitsev


People also ask

How do I set an environment variable in Maven POM?

To refer to environment variables from the pom. xml, we can use the ${env. VARIABLE_NAME} syntax. We should remember to pass the Java version information via environment variables.

Do we need to set environment variables for Maven?

Note that if you want to use Maven, you need to have Java installed and an environment variable set up. Open Google and search for maven download.

What is M2_HOME environment variable?

M2_HOME is used by Maven, and again it tells the program where to find Maven installation. PATH is not suitable for that purpose, because it may contain many directories not related to Java or Maven.


1 Answers

Some plugins, like surefire, let you set them. There's no way, in general, in the pom.

The doc for surefire is is here. Surefire will set environment variables for the duration of the run of the tests, not for anything else. And you have to make surefire fork.

In the configuration ...

<configuration>   <forkMode>always</forkMode>   <environmentVariables>      <var1>val1</var1>   </environmentVariables> </configuration> 
like image 113
bmargulies Avatar answered Sep 22 '22 06:09

bmargulies