Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using multiple JDK on the same machine for maven

Tags:

maven

I have to build java projects for different java versions. I'm using maven. I would like to specify all JDK locations in one configuration file (probably settings.xml) and then maven should choose correct one based on maven-compiler-plugin configuration specified in the pom and use compiler and standard library from this JDK to build project. Is it possible? I understand that I can use something like

JAVA_HOME=jdk6path mvn package

but it's not convenient.

like image 459
vbezhenar Avatar asked Aug 04 '26 05:08

vbezhenar


1 Answers

Yes its possible. In settings.xml define properties like this:

              <properties> 
                <java.home.1.4>C:\Program Files\Java\jdk1.4</java.home.1.4> 
                <java.home.5>C:\Program Files\Java\jdk1.5</java.home.5> 
                <java.home.6>C:\Program Files\Java\jdk1.6</java.home.6> 
              </properties> 

Then in your POM file specify the properties

<compiler.source>1.6</compiler.source>
<compiler.target>1.6</compiler.target>
<compiler.compilerVersion>1.6</compiler.compilerVersion>
<compiler.jdk>${java.home.6}</compiler.jdk>

Then int he Maven compiler plugin pass in the executable:

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${compilerPluginVersion}</version>
                <configuration>
                    <source>${compiler.source}</source>
                    <target>${compiler.target}</target>
                    <compilerVersion>${compiler.compilerVersion}</compilerVersion>
                    <executable>${compiler.executable}</executable>
                </configuration>
            </plugin>

You then control which is used by the properties. You can also set up different profiles in the POM with different property values to allow you to run the build with different versions without any change required to the pom.

like image 164
cowls Avatar answered Aug 07 '26 04:08

cowls



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!