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.

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
cupakob
  • 8,411
  • 24
  • 67
  • 76

2 Answers2

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

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • 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. – Anonymoose Aug 20 '11 at 18:46
  • @Anonymoose that might be right, probably you should ask a follow-up question at http://superuser.com to determine the exact command necessary (if there is one). I'm not a shell guru. – Sean Patrick Floyd Aug 20 '11 at 20:04
  • 6
    I don't understand why answers that are incorrect are a) voted up and b) accepted. – Anonymoose Mar 13 '12 at 09:06
  • 1
    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. – Sean Patrick Floyd Oct 20 '12 at 20:47
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)

Community
  • 1
  • 1
Martin Modrák
  • 746
  • 8
  • 17