Setting environment variable with maven 2.x
Asked Answered
M

2

8

Is it possible to set environment variable with maven (OS: Linux)?

I already have user-defined properties (in the pom and in profiles.xml)....my problem is, how to execute following from Maven

export GGA_FRE=/path

So will be possible, that every developer can set his own path for the GGA_FRE.

Malocclusion answered 17/8, 2010 at 8:10 Comment(4)
Maven has some env and java variables built-in. Which one (as an example) do you want to set ?Cousins
i want to set my own environment var....for exmple the GGA_FRE.Malocclusion
Why do you need to set them? Use them from outside Maven?Nevadanevai
Yes....and i want, that every developer can set different path for the GGA_FRE.Malocclusion
L
4

This answer is not correct, at least not completely (see comments).
Unfortunately I can't delete it as it has been accepted. Your milage may vary.


Use the exec:exec mojo.

  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.1</version>
    <executions>
      <execution>
        <id>exportVar</id>
        <phase>initialize</phase>
        <goals>
          <goal>exec</goal>
        </goals>
      </execution>
    </executions>
    <configuration>
      <executable>export</executable>
      <arguments>
        <argument>GGA_FRE=${my.path}</argument>
      </arguments>
    </configuration>
  </plugin>

now call it like this mvn install -Dmy.path=/var/users/groucho

Lymphadenitis answered 17/8, 2010 at 14:12 Comment(4)
Can anyone confirm that this works? I've tried it and it doesn't work for me. I think that exec:exec would execute the command in a spawned shell, which would take the changed environment down with it when it exits immediately afterwards.Goings
@Goings that might be right, probably you should ask a follow-up question at superuser.com to determine the exact command necessary (if there is one). I'm not a shell guru.Lymphadenitis
I don't understand why answers that are incorrect are a) voted up and b) accepted.Goings
I said above that the mechanism is as close as you'll get from inside Maven. Figure out the correct shell command and it will work. This is a maven question, not a shell question.Lymphadenitis
O
0

I don't think there is a Java way to set environment variable the way export command does (so that it is avaliable outside of Java). (see for example this question: How do I set environment variables from Java?)

However, you might hack you way around: for example use maven-exec plugin to run a shell script and then set the variable in the script. You might pass a parameter to your script to specify the variable value. (note that I have not tested this)

Onslaught answered 15/2, 2012 at 12:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.