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.

4
  • Maven has some env and java variables built-in. Which one (as an example) do you want to set ? Commented Aug 17, 2010 at 8:21
  • i want to set my own environment var....for exmple the GGA_FRE. Commented Aug 17, 2010 at 8:27
  • Why do you need to set them? Use them from outside Maven? Commented Aug 17, 2010 at 8:34
  • Yes....and i want, that every developer can set different path for the GGA_FRE. Commented Aug 17, 2010 at 8:40

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

4 Comments

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 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.
I don't understand why answers that are incorrect are a) voted up and b) accepted.
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.
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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.