目 录CONTENT

文章目录

jenkins 调用maven打包,指定jdk版本不生效(已解决)

Seven
2024-02-24 / 0 评论 / 1 点赞 / 1154 阅读 / 10345 字 / 正在检测是否收录...

前言:

公司一个java项目,现在准备使用容器化ci/cd。java使用jdk为1.8。安装最新版jenkins 2.440.1仅支持jdk 17。下面就进行了踩坑流程。服务器系统使用的是ubunt 22.04 live-server。

按照官方说明,安装好各个部件。

系统默认jdk选择:因为项目基于jdk1.8那么系统默认就是1.8如下:

java version "1.8.0_241"
Java(TM) SE Runtime Environment (build 1.8.0_241-b07)
Java HotSpot(TM) 64-Bit Server VM (build 25.241-b07, mixed mode)

然后是jenkins需要的jdk17

java version "17.0.10" 2024-01-16 LTS
Java(TM) SE Runtime Environment (build 17.0.10+11-LTS-240)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.10+11-LTS-240, mixed mode, sharing)

环境变量配置根据项目选择jdk1.8

export JENKINS_HOME=/data/jenkins
JAVA_HOME=/usr/local/jdk1.8.0_241
CLASSPATH=$JAVA_HOME/lib/
NODE_HOME=/usr/local/node-v20.11.1
MAVEN_HOME=/usr/local/apache-maven-3.6.1
export PATH=$PATH:$JAVA_HOME/bin:$CLASSPATH:$NODE_HOME/bin:$MAVEN_HOME/bin

maven安装项目开发使用版本3.6.1

Apache Maven 3.6.1 (d66c9c0b3152b2e69ee9bac180bb8fcc8e6af555; 2019-04-05T03:00:29+08:00)
Maven home: /usr/local/apache-maven-3.6.1
Java version: 1.8.0_241, vendor: Oracle Corporation, runtime: /usr/local/jdk1.8.0_241/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "5.15.0-94-generic", arch: "amd64", family: "unix"

拉代码,进行打包测试

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  45.801 s
[INFO] Finished at: 2024-02-24T10:20:38+08:00
[INFO] ------------------------------------------------------------------------

非常完美。
到此,看起来一切是那么的正常。

使用jenkins进行ci/cd自动化。

因为jenkins使用jdk17,所以配置指定jdk17启动jenkins
修改文件:vim /usr/lib/systemd/system/jenkins.service
添加配置

Environment="JENKINS_HOME=/data/jenkins"
WorkingDirectory=/data/jenkins
Environment="JAVA_HOME=/usr/local/jdk-17.0.10"
Environment="JENKINS_JAVA_CMD=/usr/local/jdk-17.0.10/bin/java"

启动jenkins.

jenkins.service - Jenkins Continuous Integration Server
     Loaded: loaded (/lib/systemd/system/jenkins.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2024-02-24 10:50:33 CST; 36min ago
   Main PID: 954034 (java)
      Tasks: 62 (limit: 18973)
     Memory: 3.1G
        CPU: 3min 34.029s
     CGroup: /system.slice/jenkins.service
             └─954034 /usr/local/jdk-17.0.10/bin/java -Djava.awt.headless=true -jar /usr/share/java/jenkins.war --webroot=/var/cache/jenkins/war --httpPort=8080

编写流水线脚本
拉代码–》打包–》构建镜像–》测试服务器docker run
测试流水线脚本
打包maven抛错。

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project lmkj-pbb-common: Fatal error compiling: java.lang.IllegalAccessError: class lombok.javac.apt.LombokProcessor (in unnamed module @0x7ab8c972) cannot access class com.sun.tools.javac.processing.JavacProcessingEnvironment (in module jdk.compiler) because module jdk.compiler does not export com.sun.tools.javac.processing to unnamed module @0x7ab8c972 -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
[ERROR] 
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR]   mvn <goals> -rf :lmkj-pbb-common

再次手动去服务器使用命令打包–结果正常。
难道是账户问题或者是jenkins 使用jdk版本不对。
测试jenkins jdk版本:

[Pipeline] sh
+ java -version
java version "1.8.0_241"
Java(TM) SE Runtime Environment (build 1.8.0_241-b07)
Java HotSpot(TM) 64-Bit Server VM (build 25.241-b07, mixed mode)
[Pipeline] }

没有问题,版本正常。

更改jenkins启动用户为root

服务器再次测试-正常。

jenkins再次打包–》再次报错。

网上一堆说什么pom.xml文件要指定jdk版本。

修改完后再次测试–依然报错。
就是jdk版本问题确定,决定不使用jenkins调用maven.写shell脚本进行mvn打包测试。
服务器上测试脚本。完美。没有问题。
jenkins调用脚本。再次测试,–》问题依旧。

wtf。(王德发)

jenkins上一定有什么问题

各种爬文,没有解决。
先抽干烟想想。
突然想到,会不会是maven调用jdk使用了jdk17.
先打印服务器上maven情况:

root@ubuntu:/usr/local/jdk-17.0.10/bin# mvn -v
Apache Maven 3.6.1 (d66c9c0b3152b2e69ee9bac180bb8fcc8e6af555; 2019-04-05T03:00:29+08:00)
Maven home: /usr/local/apache-maven-3.6.1
Java version: 1.8.0_241, vendor: Oracle Corporation, runtime: /usr/local/jdk1.8.0_241/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "5.15.0-94-generic", arch: "amd64", family: "unix"

打印jenkins maven情况:

+ mvn -v
Apache Maven 3.6.1 (d66c9c0b3152b2e69ee9bac180bb8fcc8e6af555; 2019-04-05T03:00:29+08:00)
Maven home: /usr/local/apache-maven-3.6.1
Java version: 17.0.10, vendor: Oracle Corporation, runtime: /usr/local/jdk-17.0.10
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "5.15.0-94-generic", arch: "amd64", family: "unix"

确定问题。
在网上找了一堆指定maven使用jdk版本方法:

1、修改maven配置文件settings.xml

添加内容:

    <profile>
            <id>jdk-1.8</id>
            <activation>
                    <activeByDefault>true</activeByDefault>
                    <jdk>1.8</jdk>
            </activation>
            <properties>
                    <maven.compiler.source>1.8</maven.compiler.source>
                    <maven.compiler.target>1.8</maven.compiler.target>
                    <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
            </properties>
    </profile>

测试,问题依旧。
2、jenkins 指定使用jdk版本:
截屏2024-02-24 11.50.07
再测试–》解决不了。
3、修改jenkins主目录下config.xml文件
我在上一步配置了后这个文件已经配置为jdk1.8
所以应该不是这里的问题。
4、maven配置文件添加 java_home配置:

    <profile>
            <id>jdk-1.8</id>
            <activation>
                    <activeByDefault>true</activeByDefault>
                    <jdk>1.8</jdk>
            </activation>
            <properties>
                    <JAVA_HOME>/usr/local/jdk1.8.0_241</JAVA_HOME>
                    <JRE_HOME>/usr/local/jdk1.8.0_241/jre</JRE_HOME>
                    <maven.compiler.source>1.8</maven.compiler.source>
                    <maven.compiler.target>1.8</maven.compiler.target>
                    <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
            </properties>
    </profile>

再次测试,问题依然。
5、最终在大神那里找到了答案。
指写mvn使用jdk版本:

#在mvn二进制文件中,会获取当前环境的$JAVA_HOME环境
mvn文件增加自己安装的jdk路径即可
[root@iZ3ic060uyx26md9jijcexZ bin]# vi mvn
JAVA_HOME=/usr/local/jdk1.8.0_241

再次测试,问题解决。

+ mvn -v
Apache Maven 3.6.1 (d66c9c0b3152b2e69ee9bac180bb8fcc8e6af555; 2019-04-05T03:00:29+08:00)
Maven home: /usr/local/apache-maven-3.6.1
Java version: 1.8.0_241, vendor: Oracle Corporation, runtime: /usr/local/jdk1.8.0_241/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "5.15.0-94-generic", arch: "amd64", family: "unix"
[Pipeline] sh
+ java -version
java version "1.8.0_241"
Java(TM) SE Runtime Environment (build 1.8.0_241-b07)
Java HotSpot(TM) 64-Bit Server VM (build 25.241-b07, mixed mode)
[Pipeline] }
#分隔线-------------------
[INFO] lmkj-builder 0.0.1-SNAPSHOT ........................ SUCCESS [  0.001 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  46.535 s
[INFO] Finished at: 2024-02-24T11:05:11+08:00
[INFO] ------------------------------------------------------------------------
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] echo
One way or another, I have finished
1

评论区