You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@maven.apache.org by "Terrien Jean-Yves (Jira)" <ji...@apache.org> on 2021/05/21 08:32:00 UTC

[jira] [Commented] (MNG-7141) different environment variables like JAVA_HOME depending on the project.

    [ https://issues.apache.org/jira/browse/MNG-7141?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17349079#comment-17349079 ] 

Terrien Jean-Yves commented on MNG-7141:
----------------------------------------

Hi,
I found a solution which does not involve modifying the maven scripts.

Here is the problem:
On a machine we have an installation of maven (3.6.3)
Several JDKs java6, java8, java11
- the project 1 which is an old project in maintenance which only works in java6. to compile it with maven 3.6.3 you have to use the minimum jdk8 because some maven plugins do not work in java6. But we cannot use the jdk11 because this jdk does not allow compilation in java6 (it is no longer supported)
- the project 2 which is in java8
- the project 3 in java11
etc.
To be able to compile all these projects on the same machine, you must define the JAVA_HOME environment variable each time.
Because otherwise all use the system default JDK.

The maven script has provided that 2 Users on the same machine can have a different definition via the ~/.mavenrc file
But nothing is planned to do this on each project.

Here is the solution I implemented and which works well.
in the file
~/.mavenrc
JAVA12=/path/to/jdk12
JAVA11=/path/to/jdk11
JAVA9=$ {JAVA11}
JAVA8=/path/to/jdk8
JAVA7=$ {JAVA8}
JAVA6=/path/to/jdk6

JAVA_HOME=${JAVA11}
if [-f ".mvn/mavenrc"]; then
     . ".mvn/mavenrc"
fi

This way if an .mvn/mavenrc file is present in the project folder it can redefine the jdk to use, but also other variables. If it is not there, the default jdk defined by the User is used.
If the project is compiled on a machine which does not have this ~/.mavenrc file in the User's folder, this is the basic operation of maven.

That works well.

So I think we can close the subject.
Unless you think it's interesting to generalize the optional presence of a mavenrc file in the project.

> different environment variables like JAVA_HOME depending on the project.
> ------------------------------------------------------------------------
>
>                 Key: MNG-7141
>                 URL: https://issues.apache.org/jira/browse/MNG-7141
>             Project: Maven
>          Issue Type: Improvement
>          Components: maven wrapper
>            Reporter: Terrien Jean-Yves
>            Priority: Major
>
> Hello,
> A suggestion :
> Add in script just after
> {code:sh}
>    if [-f "$ HOME/.mavenrc"]; then
>      . "$ HOME/.mavenrc"
>    fi
>   # <==== this to load local mavenrc
>    if [-f ".mvn/mavenrc"]; then
>      . ".mvn/mavenrc"
>    fi
> {code}
> This allows to have different environment variables like JAVA_HOME depending on the project.
> Currently it is possible to choose the version of maven in the properties file but not the version of java.
> In all projects, the maven command is run with the same version of java because it depends on the user's environment and not the project.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)