JENKINS:当我尝试为特定的maven项目使用旧的JDK时出错

lo8azlld  于 2023-03-12  发布在  Jenkins
关注(0)|答案(2)|浏览(471)

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?

fkvaft9z

fkvaft9z1#

正如Jenkins主页maven project plugin中所记录的:
Jenkins〉= 2.54需要Java 8,因此必须使用Java〉= 8启动Maven作业
幸运的是,堆栈跟踪中提到了最佳解决方案:
...使用**Maven JDK工具链(插件)**使用较旧的JDK构建maven项目...
为了使用java 7编译您的项目,并使用当前的Jenkins版本成功启动Jenkins作业,我建议您通过2个简单的步骤使用toolchains plugin

  • toolchains.xml文件添加到开发人员系统和Jenkins服务器上的**.m2**目录中
<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>
  • 将工具链插件添加到项目pom文件
<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>

最后,将Jenkins作业配置为使用jdk 8,工具链将负责使用jdk 7编译您的项目。

wxclj1h5

wxclj1h52#

尝试在Jenkins中配置自由式作业。您需要选择“调用顶级Maven目标”构建。我也遇到过类似的问题。这对我很有效。

相关问题