OpenJDK11 was released in September 2018. If you use Java for enterprise projects, you probably have JDK11. Also, I've heard some people who are already using JDK8 but want to adopt JDK11 for some new projects.
I'm using JDK8 this time, but when I started to verify with JDK11, I wondered what kind of build settings would be good if I wanted to support (build) both JDK8 and JDK11 in Gradle build. .. It should be noted that changes to the JDK, modification of the application due to version upgrade, tuning, etc. are necessary items, but they are excluded from this article. I just want to focus on how to dynamically switch the JDK at build time.
sourceCompatibility
, targetCompatibility
of build.gradle
I think that the JDK version is often specified as a fixed value in build.gradle
. (Or listed in gradle.properties
)
The image is as follows.
build.gradle
...
sourceCompatibility = 1.8
targetCompatibility = 1.8
...
Or if you put the value in gradle.properties
build.gradle
...
sourceCompatibility = javaVersion
targetCompatibility = javaVersion
...
gradle.properties
...
javaVersion=1.8
...
It is an image.
I want to switch dynamically when the build is executed, so the definition of build.gradle
is
build.gradle
...
sourceCompatibility = javaVersion
targetCompatibility = javaVersion
...
And define it as a variable.
JAVA_HOME
in the parameters at build timeYou can specify the parameter with -Pkey = value
of gradle
. You can use this to specify the JDK version at build time, such as gradle build -PjavaVersion = 1.11
. You also need to specify the JDK used by Gradle. This can be specified with the ʻorg.gradle.java.home` parameter.
Therefore
gradle build -PjavaVersion=1.11 -Dorg.gradle.java.home="C:\java\11 --info"
Isn't it good to do like this?
The project structure is as follows.
C:
└─src
└─main
├─java
│ └─sample
│ └─gradle
│ └─multiversion
│ Sample.java
└─resources
build.gradle
plugins {
id 'java-library'
}
repositories {
jcenter()
}
dependencies {
}
sourceCompatibility = javaVersion
targetCompatibility = javaVersion
Build with JDK11.
>gradle build -PjavaVersion=1.11 -Dorg.gradle.java.home="C:\java\11"
BUILD SUCCESSFUL in 1s
2 actionable tasks: 2 executed
Check the version built with javap -v
for the actually compiled class file.
As you can see from the execution result below, you can see that it is built with major version: 55
and JDK11.
C:\workspace\sample-gradle-multiversion>javap -v build\classes\java\main\sample\gradle\multiversion\Sample.class
Classfile /C:/workspace/sample-gradle-multiversion/build/classes/java/main/sample/gradle/multiversion/Sample.class
Last modified 2019/07/12; size 576 bytes
MD5 checksum 580cd2384cb91a31c14e09339617ba75
Compiled from "Sample.java"
public class sample.gradle.multiversion.Sample
minor version: 0
major version: 55
flags: (0x0021) ACC_PUBLIC, ACC_SUPER
this_class: #5 // sample/gradle/multiversion/Sample
super_class: #6 // java/lang/Object
interfaces: 0, fields: 0, methods: 2, attributes: 1
Constant pool:
#1 = Methodref #6.#20 // java/lang/Object."<init>":()V
#2 = Fieldref #21.#22 // java/lang/System.out:Ljava/io/PrintStream;
#3 = String #23 // Hello world.
#4 = Methodref #24.#25 // java/io/PrintStream.println:(Ljava/lang/String;)V
#5 = Class #26 // sample/gradle/multiversion/Sample
#6 = Class #27 // java/lang/Object
#7 = Utf8 <init>
#8 = Utf8 ()V
#9 = Utf8 Code
#10 = Utf8 LineNumberTable
#11 = Utf8 LocalVariableTable
#12 = Utf8 this
#13 = Utf8 Lsample/gradle/multiversion/Sample;
#14 = Utf8 main
#15 = Utf8 ([Ljava/lang/String;)V
#16 = Utf8 args
#17 = Utf8 [Ljava/lang/String;
#18 = Utf8 SourceFile
#19 = Utf8 Sample.java
#20 = NameAndType #7:#8 // "<init>":()V
#21 = Class #28 // java/lang/System
#22 = NameAndType #29:#30 // out:Ljava/io/PrintStream;
#23 = Utf8 Hello world.
#24 = Class #31 // java/io/PrintStream
#25 = NameAndType #32:#33 // println:(Ljava/lang/String;)V
#26 = Utf8 sample/gradle/multiversion/Sample
#27 = Utf8 java/lang/Object
#28 = Utf8 java/lang/System
#29 = Utf8 out
#30 = Utf8 Ljava/io/PrintStream;
#31 = Utf8 java/io/PrintStream
#32 = Utf8 println
#33 = Utf8 (Ljava/lang/String;)V
{
public sample.gradle.multiversion.Sample();
descriptor: ()V
flags: (0x0001) ACC_PUBLIC
Code:
stack=1, locals=1, args_size=1
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
LineNumberTable:
line 6: 0
LocalVariableTable:
Start Length Slot Name Signature
0 5 0 this Lsample/gradle/multiversion/Sample;
public static void main(java.lang.String[]);
descriptor: ([Ljava/lang/String;)V
flags: (0x0009) ACC_PUBLIC, ACC_STATIC
Code:
stack=2, locals=1, args_size=1
0: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
3: ldc #3 // String Hello world.
5: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
8: return
LineNumberTable:
line 10: 0
line 12: 8
LocalVariableTable:
Start Length Slot Name Signature
0 9 0 args [Ljava/lang/String;
}
SourceFile: "Sample.java"
Keep gradle clean
to remove the class files built with JDK 11.
C:\workspace\sample-gradle-multiversion>gradle clean -PjavaVersion=1.11
BUILD SUCCESSFUL in 1s
1 actionable task: 1 executed
Then build with JDK8.
C:\workspace\sample-gradle-multiversion>gradle build -PjavaVersion=1.8 -Dorg.gradle.java.home="C:\java\8"
BUILD SUCCESSFUL in 1s
2 actionable tasks: 2 executed
Check the class file. You can see that it was built with major version: 52
and JDK8.
C:\workspace\sample-gradle-multiversion>javap -v build\classes\java\main\sample\gradle\multiversion\Sample.class
Classfile /C:/workspace/sample-gradle-multiversion/build/classes/java/main/sample/gradle/multiversion/Sample.class
Last modified 2019/07/12; size 576 bytes
MD5 checksum ea23dd24700da60b773d93f8ff2f9d77
Compiled from "Sample.java"
public class sample.gradle.multiversion.Sample
minor version: 0
major version: 52
flags: (0x0021) ACC_PUBLIC, ACC_SUPER
this_class: #5 // sample/gradle/multiversion/Sample
super_class: #6 // java/lang/Object
interfaces: 0, fields: 0, methods: 2, attributes: 1
Constant pool:
#1 = Methodref #6.#20 // java/lang/Object."<init>":()V
#2 = Fieldref #21.#22 // java/lang/System.out:Ljava/io/PrintStream;
#3 = String #23 // Hello world.
#4 = Methodref #24.#25 // java/io/PrintStream.println:(Ljava/lang/String;)V
#5 = Class #26 // sample/gradle/multiversion/Sample
#6 = Class #27 // java/lang/Object
#7 = Utf8 <init>
#8 = Utf8 ()V
#9 = Utf8 Code
#10 = Utf8 LineNumberTable
#11 = Utf8 LocalVariableTable
#12 = Utf8 this
#13 = Utf8 Lsample/gradle/multiversion/Sample;
#14 = Utf8 main
#15 = Utf8 ([Ljava/lang/String;)V
#16 = Utf8 args
#17 = Utf8 [Ljava/lang/String;
#18 = Utf8 SourceFile
#19 = Utf8 Sample.java
#20 = NameAndType #7:#8 // "<init>":()V
#21 = Class #28 // java/lang/System
#22 = NameAndType #29:#30 // out:Ljava/io/PrintStream;
#23 = Utf8 Hello world.
#24 = Class #31 // java/io/PrintStream
#25 = NameAndType #32:#33 // println:(Ljava/lang/String;)V
#26 = Utf8 sample/gradle/multiversion/Sample
#27 = Utf8 java/lang/Object
#28 = Utf8 java/lang/System
#29 = Utf8 out
#30 = Utf8 Ljava/io/PrintStream;
#31 = Utf8 java/io/PrintStream
#32 = Utf8 println
#33 = Utf8 (Ljava/lang/String;)V
{
public sample.gradle.multiversion.Sample();
descriptor: ()V
flags: (0x0001) ACC_PUBLIC
Code:
stack=1, locals=1, args_size=1
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
LineNumberTable:
line 6: 0
LocalVariableTable:
Start Length Slot Name Signature
0 5 0 this Lsample/gradle/multiversion/Sample;
public static void main(java.lang.String[]);
descriptor: ([Ljava/lang/String;)V
flags: (0x0009) ACC_PUBLIC, ACC_STATIC
Code:
stack=2, locals=1, args_size=1
0: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
3: ldc #3 // String Hello world.
5: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
8: return
LineNumberTable:
line 10: 0
line 12: 8
LocalVariableTable:
Start Length Slot Name Signature
0 9 0 args [Ljava/lang/String;
}
SourceFile: "Sample.java"
sourceCompatibility
, targetCompatibility
of build.gradle
-P
and JAVA_HOME
with the -Dorg.gradle.java.home
Recommended Posts