jdk version for compiling java code: jenkins vs pom.xml - maven

We are using jdk 6 to compile our code (specified in jenkins), I am trying to specify jdk 7 to compile the projects from now on. I noticed some of the projects have specified jdk 6 in their pom.xml. I am not sure which one takes precedence when jenkins specifies jdk 7 but pom.xml speicifies jdk 6. Any idea which one takes the final precedence to compile the code?

In JDK you can specify the generated class files that target a specified version of the VM. For example if JDK 1.7 is used you can specify the target class file version as 1.7 or lower version.
In maven compiler plugin you can specify the source and target version as follows.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${sourceVersion}</source>
<target>${targetVersion}</target>
</configuration>
</plugin>
</plugins>
</build>
In your case if you want the class to be compiled to java 1.7 then you have to specify ${sourceVersion} to 1.7 and the ${targetVersion} to 1.7
I am not sure which one takes precedence when jenkins specifies jdk 7 but pom.xml speicifies jdk 1.6
If JDK 7 is used in Jenkins and the target version in the pom is 1.6 then the classes will be compiled to jdk 1.6 version.
Check the following links for more information.
http://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html#options

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

Sonarqube-Analysis on Jenkins fails due to PMD Plugin throwing UnsupportedClassVersionError: Unsupported major.minor version 52.0

Java-Version Configuration
Java-Version used for compiling classes with ant: 1.8
pom.xml für Sonarqube-Analysis defines:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
and
<!-- Set the sonar.java.source property to tell PMD which version of Java your source code complies to. The default value is 1.5. -->
<sonar.java.source>1.8</sonar.java.source>
Sonarqube-Version: 4.5.4
Sonarqube-PMD-Plugin-Version: 2.4.1
JDK configured in Jenkins-Job: 1.8
Sonarqube-Server runs using Java 8
There is only Java8 installed on both Jenkins- and Sonarqube-Server.
Jenkins executes the following maven-comand to start the analysis:
mvn.bat -f "D:\Jenkins\workspace\DVLP MW2.0 Coverage und Codeanalyse\sonar-pom.xml" -e -B sonar:sonar -Dsonar.jdbc.driver=com.mysql.jdbc.Driver "-Dsonar.jdbc.url=jdbc:mysql://192.168.58.121:3306/sonar?autoReconnect=true&useUnicode=true&characterEncoding=utf8" -Dsonar.host.url=http://192.168.58.121/sonar
What are we missing?
You should remove property sonar.java.source. SonarQube gets Java Class Version from maven-compiler-plugin configuration. It is not a problem if you compile with JDK7 and run analysis with JDK8 (or do both actions with JDK7/JDK8).

JavaFX project using Maven in NetBeans

In Netbeans 8.0 I develop JavaFX project using Maven. My java version is 1.7 and I have problems with upgrading to java 8. I tried to change <source> and <target> in pom-file to 1.8
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerArguments>
<bootclasspath>${sun.boot.class.path}${path.separator}${java.home}/lib/jfxrt.jar</bootclasspath>
</compilerArguments>
</configuration>
</plugin>
but I get error
Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project ...: Fatal error compiling: invalid target release: 1.8 -> [Help 1]
My JAVA_HOME environment variable set to java 1.8.
When I try to create new JavaFX project like File - New Project - Maven Category - JavaFX Application I still get project with JDK 1.7. How can I force to use JDK 1.8?
First you need to install JDK 1.8, of course.
Then either
in the installation directory set the default JDK location (netbeans_jdkhome property in etc/netbeans.conf )
or
in Tools -> Java Platform: add an entry for JDK 1.8
in the Project properties set the Java Platform to JDK 1.8
I also recommend to use NetBeans 8.x when working with JDK 1.8
you can add in maven tag properties this code
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
and check:
1-Tools -> Java Platform: add an entry for JDK 1.8.
2-the Project properties >> source window >> source/binaryFormat 1.8
Most of time the problem is (2):that netbeans create the JavaFx Maven project with the low jdk 1.7.

Hybrid JDK 1.7 Server 1.6 Client Maven Build of Artifacts

I have a Maven project with both server back-end modules and client front-end modules. The goal is to take advantage of performance improvements in JDK 7 for the server back-end modules but build the client modules in JDK 6 only. The client modules run in Tomcat while the server modules are separate Java applications.
I am aware of the Maven configuration to target specific JDK:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerVersion>1.6</compilerVersion>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
One caveat to this approach is that there are common modules that are used by both the server modules and client modules necessitating two different binaries (JDK 6 and 7) for each module.
How can one compile one set of artifacts for a JDK 6, and another set of artifacts for JDK 7, and a third set of artifacts (used by both) for both JDK 6 and 7 all belonging to the same project? Would it be necessary to divide the modules up into three separate projects?
If you <target> 1.6, the resulting Jar will work perfectly fine in 1.6 and 1.7. No need to compile twice.
There is no perfo benefit (except perhaps in some very obscure cases) from using the 1.7 compiler or targetting 1.7 byte code. All the perfo lives in the JVM. Just make your common code be target 1.6. Unless you yourself can measure a speed advantage of compiling for 1.7, don't bother.
The answer of bmargulies is very pertinent.
Anyway, if you really want to build different versions of some module (1.6 and 1.7) I suggest you to use 2 profiles, profile1 targeting jdk6, profile2 targeting jdk7. So both profiles will redefine the maven-compiler-plugin.
Both profiles can also redefine the maven-jar-plugin with a different classifier so that each module will be available in 2 versions (and use the classifier in your dependencies so that client use jdk6 and server use jdk7).
Here is another question with more details about the way you can do this: How to manage artifactory / maven artifact using different profiles

Maven/Jenkins java.lang.UnsupportedClassVersionError: Unsupported major.minor version 51.0

I have a Jenkins server having JDK & JRE 6 and 7 installed together.
All of the projects are built on 1.6 except one which is 1.7 dependent.
I've configured the maven pom file to use the Java compiler from the JAVA_HOME_7 environment PATH.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<showDeprecation>true</showDeprecation>
<showWarnings>true</showWarnings>
**<executable>${env.JAVA_HOME_7}/bin/javac</executable>**
<fork>true</fork>
<verbose>false</verbose>
</configuration>
</plugin>
During mvn install I'm getting the following error:
java.lang.RuntimeException: There was an error in the forked process
java.lang.UnsupportedClassVersionError: : Unsupported major.minor version 51.0
which I think means that the server is using JRE 1.6.
How to keep the JRE 1.6 together with 1.7 in order to keep the compatibility with the old 1.6 projects and the new 1.7 one?
Many Thanks,
Atanas
You will need to run surefire tests with java 7 too. By default surefire will use same jvm as that running maven - Java6 in your case.
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.13</version>
<configuration>
...
<jvm>${env.JAVA_HOME_7}/bin/java</jvm>
</configuration>
</plugin>
</plugins>
I remember I also struggled to this problem. Follow below steps to resolve the problem
Cause: when multiple JRE is installed then multiple java.exe is also installed to many location of system.
Solution: Modify your environment PATH variable and change the order of java.exe. put location of java.exe on first position like below code
PATH = C:\Program Files\Java\jdk1.6.0\; other;other;other
Change above location according to your use and installation location.
I have encountered this problem more than once, it is because you have more than one versions of jdk(jre) on your system, so just set the JAVA_HOME to the proper jdk you compile your project with and the running would be fine.
Have a look at your target/lib directory, you might have two versions of same jar. For me it was creating the pro

Resources