JENKINS: ERROR when I try to use an older JDK for a specific maven project

6.4k views Asked by At

I am using the Jenkins version 2.73-1.1 on a CentOS Linux release 7.3.1611 server.

There are 3 different versions of JDK on the server:

[root @ jenkins java] # ll
total 12
lrwxrwxrwx. 1 root root 16 27 Apr 16.25 default -> / usr / java / latest
drwxr-xr-x. 8 root root 4096 27 Mar 2013 jdk1.6.0_45
drwxr-xr-x. Root root 4096 11 Apr 2015 jdk1.7.0_80
drwxr-xr-x. 9 root root 4096 27 Apr 16.25 jdk1.8.0_131
lrwxrwxrwx. 1 root root 22 27 apr 16.25 latest -> /usr/java/jdk1.8.0_131

As shown in the pictures below, Jenkins is using the jdk1.8.0_131 version and in my project I specified to use JDK7.

Jenkins setup Image 1

Jenkins setup Image 2

When I try to compile my project (mvn clean deploy), the build fails and I see this ERROR in log:

[Vodafone] $ /usr/java/jdk1.7.0_80/bin/java -cp /var/lib/jenkins/plugins/maven-plugin/WEB-INF/lib/maven3-agent-1.11.jar:/opt/apache-maven-3.0.4/boot/plexus-classworlds-2.4.jar org.jvnet.hudson.maven3.agent.Maven3Main /opt/apache-maven-3.0.4 /var/cache/jenkins/war/WEB-INF/lib/remoting-3.10.jar /var/lib/jenkins/plugins/maven-plugin/WEB-INF/lib/maven3-interceptor-1.11.jar /var/lib/jenkins/plugins/maven-plugin/WEB-INF/lib/maven3-interceptor-commons-1.11.jar 46349 <===[JENKINS REMOTING CAPACITY]===>channel started ERROR: ================================================================================ ERROR: Invalid project setup: jenkins/security/MasterToSlaveCallable : Unsupported major.minor version 52.0 ERROR: [JENKINS-18403][JENKINS-28294] JDK 'JAVA7' not supported to run Maven projects. ERROR: Maven projects have to be launched with a Java version greater or equal to the minimum version required by the master. ERROR: Use the Maven JDK Toolchains (plugin) to build your maven project with an older JDK. ERROR: Retrying with slave Java and setting compile/test properties to point to /usr/java/jdk1.7.0_80. ERROR: ================================================================================ Established TCP socket on 38129 [Vodafone] $ /usr/java/jdk1.8.0_131/jre/bin/java -cp /var/lib/jenkins/plugins/maven-plugin/WEB-INF/lib/maven3-agent-1.11.jar:/opt/apache-maven-3.0.4/boot/plexus-classworlds-2.4.jar org.jvnet.hudson.maven3.agent.Maven3Main /opt/apache-maven-3.0.4 /var/cache/jenkins/war/WEB-INF/lib/remoting-3.10.jar /var/lib/jenkins/plugins/maven-plugin/WEB-INF/lib/maven3-interceptor-1.11.jar /var/lib/jenkins/plugins/maven-plugin/WEB-INF/lib/maven3-interceptor-commons-1.11.jar 38129 <===[JENKINS REMOTING CAPACITY]===>channel started

Why Jenkins doesn't use jdk 7?

2

There are 2 answers

0
Kayvan Tehrani On

As it is documented on the maven project plugin of Jenkins home page:

Jenkins >= 2.54 requires Java 8 thus Maven jobs must be launched with Java >= 8

Luckily the best workaround is just mentioned in your stack trace:

... Use the Maven JDK Toolchains (plugin) to build your maven project with an older JDK...

In order to compile your project using java 7 and launch a successful Jenkins job using the current Jenkins version, I would recommend you use toolchains plugin in 2 simple steps:

  • Add toolchains.xml file to your .m2 directory both on developers system and Jenkins server

     <toolchains>
       <toolchain>
         <type>jdk</type>
         <provides>
           <version>1.7</version>
           <vendor>openjdk</vendor>
         </provides>
         <configuration>
           <jdkHome>/usr/lib/jvm/java-7-openjdk-amd64</jdkHome>
         </configuration>
       </toolchain>
       <toolchain>
         <type>jdk</type>
         <provides>
           <version>1.8</version>
           <vendor>openjdk</vendor>
         </provides>
         <configuration>
           <jdkHome>/usr/lib/jvm/java-8-openjdk-amd64</jdkHome>
         </configuration>
       </toolchain> 
       [...]
     </toolchains>
    
  • Add toolchain plugin to your project pom file

     <project>
       <build>
         <plugins>
           <plugin>
             <groupId>org.apache.maven.plugins</groupId>
             <artifactId>maven-toolchains-plugin</artifactId>
             <version>1.1</version>
             <executions>
               <execution>
                 <goals>
                   <goal>toolchain</goal>
                 </goals>
                 </execution>
               </executions>
               <configuration>
                 <toolchains>
                   <jdk>
                     <version>1.7</version>  
                     <vendor>openjdk</vendor>
                   </jdk>
                 </toolchains>
               </configuration>
           </plugin>
         </plugins>
       </build>
     </project>
    

Finally, config your Jenkins job to use jdk 8, the toolchain will take care of compiling your project with jdk 7.

0
Marco On

Try configuring a Freestyle Job in Jenkins. You will need to select the "Invoke top-level Maven targets" build. I was having a similar problem. This worked for me.