本文最后更新于 2024-01-26,文章内容可能已经过时。

环境:

  • centos7.9
  • maven-3.6.3
  • jdk8
  • jdk11
  • jenkins2.4

背景:

  • A项目需要使用jdk8打包,运行在jdk8 环境上
  • B项目需要使用jdk11打包,运行在jdk11 环境上
  • jenkins 运行在jdk11环境上

需求:

需要在 jenkins 上 使用maven3.6.3 同时打包 A 和 B 项目

解决方法:

  • 机器上部署jdk8,jdk11
[root@localhost ~]# cd /home/application/

[root@localhost application]# ls -l
total 2
drwxr-xr-x  8 root  root    96 May 10 14:53 jdk-11.0.2
drwxr-xr-x  7 10143 10143  245 Mar 12  2020 jdk1.8.0_251
  • 机器上部署maven3.6.3-A,maven3.6.3-B
[root@localhost ~]# cd /home/application/
[root@localhost application]# ls -l
total 2
drwxr-xr-x  6 root  root    99 May 11 21:29 maven-3.6.3-A
drwxr-xr-x  6 root  root    99 May 11 21:29 maven-3.6.3-B
  • 修改mvn 二进制可执行脚本,分别指定各自jdk版本的JAVA_HOME
#在mvn脚本的开头,加上一行指定JAVA_HOME路径

[root@localhost bin]# head -10 /home/application/maven-3.6.3-A/bin/mvn
export JAVA_HOME=/home/application/jdk1.8.0_251
#!/bin/sh

# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at


#添加之后保存。在执行mvn -v 命令
[root@localhost bin]# /home/application/maven-3.6.3-A/bin/mvn -v
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /home/application/maven-3.6.3
Java version: 1.8.0_251, vendor: Oracle Corporation, runtime: /home/application/jdk1.8.0_251/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-1160.el7.x86_64", arch: "amd64", family: "unix"


#在mvn脚本的开头,加上一行指定JAVA_HOME路径

[root@localhost bin]# head -10 /home/application/maven-3.6.3-B/bin/mvn
export JAVA_HOME=/home/application/jdk-11.0.2
#!/bin/sh

# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at


#添加之后保存。在执行mvn -v 命令
[root@localhost bin]# /home/application/maven-3.6.3-B/bin/mvn -v
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /home/application/maven-3.6.3-B
Java version: 11.0.2, vendor: Oracle Corporation, runtime: /home/application/jdk-11.0.2
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-1160.el7.x86_64", arch: "amd64", family: "unix"