Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven - How to refer environment variable from eclipse

Tags:

eclipse

maven

I am have a maven project. In pom.xml i refer a environment variable as ${env.MyProjectVersion}. I set the value (export MyProjectVersion=1.0.0) of the variable from command prompt before building the project. The build is successful, I import the maven project into eclipse and m2eclipse attempts to build it failing:

Unable to download xyz-{$env.MyProjectVersion} dependency

Eclipse is not able to resolve the value of environment variable. Is there a way to specify environment variable value with eclipse so that it can be picked by maven plugin in eclipse.

like image 874
deepujain Avatar asked Mar 26 '13 08:03

deepujain


2 Answers

Apparently the Eclipse Maven plugin ignores environment variables during maven builds. A possible workaround - which might work depending on your situation - is to use properties from the settings.xml. The only drawback is that you have to define those variables as environmental variables and a second time in the settings.xml

In my answer I am describing the following scenario. Building artefacts in eclipse using the standard maven plugin functionality and additionally building the same artifacts via the maven command line. Also during command line builds i am referencing the repository folder directly mvn -Dmaven.repo.local=/PATH_TO_M2_FOLDER/repository/ -f parent.pom install

The setup is as follows: You will have to add properties in the pom.xml to act as proxies for your environment variables. In the command line scenario these properties will be filled directly. In the Eclipse scenario the properties have to be filled by the user settings.xml by defining a profile.

Say you have a property an environment variable $FOO.

Use a profile in your maven settings.xml and define fooProperty.

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
    http://maven.apache.org/xsd/settings-1.0.0.xsd">
    <profiles>
        <profile>
            <id>default</id>
            <properties>
                <fooProperty>bar</fooProperty >
            </properties>
        </profile>
    </profiles>
    ...     
  <activeProfiles>
    <activeProfile>default</activeProfile>
  </activeProfiles>
</settings>

Then you just have to point the Eclipse maven plugin to your user settings. For CLI to work you have to define the fooProperty a second time in your pom.xml: ${env.foo}

Apparently the eclipse maven plugin prioritises properties from the settings.xml higher than properties in the pom.xml. Unfortunately I have not figured out a way of defining the property in the settings.xml via an environment variable.

When using the variable in the pom.xml only use the proxy property as follows: ... ${fooProperty} ...

I am using * Eclipse Mars: Release (4.5.0) * Embedded Maven: 3.3.3/1.6.0.20150526-2031 * OSX El Capitan: 10.11.4 (15E65)

like image 160
systemkern Avatar answered Oct 11 '22 16:10

systemkern


Go to Run Configuration, Goto Enviroment tab, add variable and its value than value of variable will be resolved

like image 21
zaffargachal Avatar answered Oct 11 '22 16:10

zaffargachal