Using multiple JDK on the same machine for maven - 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.

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.

Related

I want maven to use JRE system Library [JavaSE 1.8] always while creating new maven quickstart project

I have jdk/jre 1.8 installed on my windows system. In Eclipse whenever I create a quick-start maven project, the project gets created with JRE System Library[J2SE 1.5]. I am fed up of this problem tried to change the execution environment by doing right click on above JRE library-properties and changing the execution environment to 1.8 but again if I update this maven project, the JRE system library goes back again to J2SE 1.5, I also tried changing the compiler compliance to 1.8 which was earlier set to java 10 and also did adding the source and target tag with version 1.8 inside plugin tag of pom.xml but this was also a temporary solution. As when I create a new maven project it sets the default JRE system Library[J2SE 1.5]. Please Provide me with a permanent fix where whenever i create a new maven project, it should be created with JRE System Library[JavaSE 1.8].
Add the following to pom.xml to configure which JDK version you would want to use:
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>
or the followings:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
It is because when you do Maven --> Update Project in Eclipse , Eclipse 's maven plugin will use what you define in pom.xml to update the setting of the eclipse project. If you don't specify above settings , the default is to use JDK 1.5 (before maven-compiler-plugin version 3.8.0) or JDK 1.6 (3.8.0+)
Basically I want maven to use JDK 1.8 by default. So that i wont need
to change pom.xml contents again and again while making a new project
If you do not want to change any settings related JDK version in pom.xml after creating a project , you may consider to create your own archetype to fully customize what pom.xml looks like after creating an project. Or a more simple way is to search some Java8 archetype created by others and simply use it such as this.
Refer this for how to create a Maven project with an specific archetype using Eclipse. You may probably need to click "Add Archetype" to configure the require information when you use that archetype for the first time. For example , for this archetype, you have to configure the following for the 1st time:
archetypeGroupId=pl.org.miki
archetypeArtifactId=java8-quickstart-archetype
archetypeVersion=1.0.0

Confusion on maven compiler plugin

I am quite confused about maven compiler plugin and what it does. I have a project that has several modules. In my top pom.xml I have a section
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
My understanding is this specifies the JDK compiler version used to compile the code, and this section get inherited by all the modules. What I don't get is in my IntelliJ IDEA I can still specify project JDK in the settings and it seems like that setting overrides this. When I run maven install in the IDE I can confirm that it is using javac from JDK 8 to compile. So what does this section do exactly?
You are correct; maven-compiler settings should be inherited by child modules.
I don't know about IntelliJ, but I can tell you that Eclipse picks&chooses whatever it wants from maven config, and for all the rest it uses its own settings.
Therefore, I'd expect IntelliJ may do something similar?
The simplest way to test this is to run a mvn clean install via command line, and see which "wins". If you get artifact compiled with 1.8 then it means you're missing something in Maven config which causes those settings not to propagate to children. If you get artifact compiled with 1.7 then it is IntelliJ who does it and not maven-compiler-plugin.

About maven plugin version tag

We all know, when we use plugin or dependency in maven pom.xml, we must give the GAV(groupId, artifactId, version).That maven can know what plugin or dependency you want.
eg:
<!-- generate source plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.4</version>
<!-- ...others config -->
</plugin>
but, if i write like that:
eg:
<!-- generate source plugin -->
<plugin>
<artifactId>maven-source-plugin</artifactId>
<!-- ...others config -->
</plugin>
that is right!why?Maven says we must give GAV?Why that is right when i only give A?
So I want know if I don't give G and V,that maven will use what G or V?
The reason you don't have to specify the version is because it gets inherited from the Maven super POM (http://maven.apache.org/guides/introduction/introduction-to-the-pom.html). This super POM has a definition for the maven-source-plugin that specifies the version to be used.
If you declare other dependencies or plugins that are not declared in the super POM, then the version is required. Note however, that it is a good practise to explicitly declare the versions of the plugins you want to use in your own pom.xml to make your build more reproducible.
You can leave out the group id in this case, because maven will seach for org.apache.maven.plugins as groupId if no groupId was specified. So this will only work for the official Maven plugins that use this group id.

Configure Maven to look at local directory while running tests

We are using JBehave and while running test we need to add a local folder and jars to the classpath while running the tests.
The problem is the local folder might vary from system to system. We want the tests to run by looking at the jars installed on that system and the resources defined on that system.
How to add a dependency to maven that could change from system to system?
You can use environment variables in your pom.xml using ${env.VARIABLE_NAME}. If you have the path to your local folder in the pom, you could replace it by a variable. If you do so you have to set that variable on every system you execute the maven job on. I have found some guides for linux and windows on how to do that. Hope this fits your problem.
The test are executed by the maven-surefire-plugin. The plugin has only one goal surefire:test and this goal supports the configuration of additionalClasspathElements.
You can find a usage example here. The example configuration on this page looks like this:
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.2</version>
<configuration>
<additionalClasspathElements>
<additionalClasspathElement>path/to/additional/resources</additionalClasspathElement>
<additionalClasspathElement>path/to/additional/jar</additionalClasspathElement>
</additionalClasspathElements>
</configuration>
</plugin>
</plugins>
</build>
[...]
</project>
I would go this way. To change the local folder location for each system you can use environment variables or different maven profiles.

read Maven variable from properties file using profile

I want to read a maven variable for configure a build plugin from a properties file. It's not needed in and further project files e.g. context files.
1) made a profile (it works, can use mvn ... -P private)
<profile>
<id>private</id>
<properties>
<env>private</env>
</properties>
</profile>
2) created the filter file with this content (it works)
foo.path=/home/foo/path
3) try to configure the plugin (does not work)
<build>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>foo-plugin</artifactId>
<version>${foo-plugin.version}</version>
<configuration>
<!--<fooPath>home/foo/path></fooPath> that works -->
<fooPath>${foo.path}</fooPath> <!--works not -->
</configuration>
...
</build>
Thx a lot
The name of your property is 'env' but you don't use env anywhere in your configuration.
When Maven docs mention "filter files" they usually mean a file used when processing resources (i.e. copying resources from /src/main/resources to target/classes). As far as I know the properties in those files aren't used for plugin configuration out-of-the-box. I have used the Codehaus properties-maven-plugin:read-project-properties goal do do what you are attempting. Make sure you bind the goal to the lifecycle before any plugins that need the properties for config.
Also, see this answer; you may load properties used to configure other plugins, but not to configure core Maven project elements.

Resources