Skip to content

Commit fab6a1c

Browse files
committed
Unify configuration of tasks with explicit/implicit Java versions
So that we always set the test release version, in particular.
1 parent 967ab4f commit fab6a1c

File tree

4 files changed

+103
-125
lines changed

4 files changed

+103
-125
lines changed

local-build-plugins/src/main/java/org/hibernate/orm/toolchains/JavaModulePlugin.java

Lines changed: 74 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,10 @@
1818
import org.gradle.api.plugins.JavaPluginExtension;
1919
import org.gradle.api.tasks.SourceSet;
2020
import org.gradle.api.tasks.SourceSetContainer;
21-
import org.gradle.api.tasks.compile.CompileOptions;
2221
import org.gradle.api.tasks.compile.ForkOptions;
2322
import org.gradle.api.tasks.compile.JavaCompile;
2423
import org.gradle.api.tasks.javadoc.Javadoc;
2524
import org.gradle.api.tasks.testing.Test;
26-
import org.gradle.jvm.toolchain.JavaLanguageVersion;
2725
import org.gradle.jvm.toolchain.JavaToolchainService;
2826

2927
/**
@@ -54,72 +52,65 @@ public void apply(Project project) {
5452

5553
final SourceSetContainer sourceSets = javaPluginExtension.getSourceSets();
5654
final SourceSet mainSourceSet = sourceSets.getByName( SourceSet.MAIN_SOURCE_SET_NAME );
57-
final SourceSet testSourceSet = sourceSets.getByName( SourceSet.TEST_SOURCE_SET_NAME );
5855

59-
final JavaCompile mainCompileTask = (JavaCompile) project.getTasks().getByName( mainSourceSet.getCompileJavaTaskName() );
60-
final JavaCompile testCompileTask = (JavaCompile) project.getTasks().getByName( testSourceSet.getCompileJavaTaskName() );
61-
final Test testTask = (Test) project.getTasks().findByName( testSourceSet.getName() );
62-
63-
if ( !jdkVersionsConfig.isExplicitlyConfigured() ) {
64-
mainCompileTask.setSourceCompatibility( jdkVersionsConfig.getMainReleaseVersion().toString() );
65-
mainCompileTask.setTargetCompatibility( jdkVersionsConfig.getMainReleaseVersion().toString() );
66-
67-
testCompileTask.setSourceCompatibility( jdkVersionsConfig.getTestReleaseVersion().toString() );
68-
testCompileTask.setTargetCompatibility( jdkVersionsConfig.getTestReleaseVersion().toString() );
69-
}
70-
else {
56+
if ( jdkVersionsConfig.getMain().isExplicit() ) {
7157
javaPluginExtension.getToolchain().getLanguageVersion().set( jdkVersionsConfig.getMainCompilerVersion() );
72-
73-
configureCompileTasks( project );
74-
configureTestTasks( project );
75-
configureJavadocTasks( project, mainSourceSet );
76-
77-
configureCompileTask( mainCompileTask, jdkVersionsConfig.getMainReleaseVersion() );
78-
configureCompileTask( testCompileTask, jdkVersionsConfig.getTestReleaseVersion() );
79-
80-
testCompileTask.getJavaCompiler().set(
81-
toolchainService.compilerFor( javaToolchainSpec -> {
82-
javaToolchainSpec.getLanguageVersion().set( jdkVersionsConfig.getTestCompilerVersion() );
83-
} )
84-
);
85-
if ( testTask != null ) {
86-
testTask.getJavaLauncher().set(
87-
toolchainService.launcherFor( javaToolchainSpec -> {
88-
javaToolchainSpec.getLanguageVersion().set( jdkVersionsConfig.getTestLauncherVersion() );
89-
} )
90-
);
91-
}
9258
}
93-
}
9459

95-
private void configureCompileTask(JavaCompile compileTask, JavaLanguageVersion releaseVersion) {
96-
final CompileOptions compileTaskOptions = compileTask.getOptions();
97-
compileTaskOptions.getRelease().set( releaseVersion.asInt() );
60+
configureCompileTasks( project, mainSourceSet, jdkVersionsConfig );
61+
configureTestTasks( project, jdkVersionsConfig );
62+
configureJavadocTasks( project, mainSourceSet, jdkVersionsConfig );
9863
}
9964

100-
private void configureCompileTasks(Project project) {
65+
private void configureCompileTasks(Project project, SourceSet mainSourceSet, JdkVersionConfig jdkVersionsConfig) {
10166
project.getTasks().withType( JavaCompile.class ).configureEach( new Action<JavaCompile>() {
10267
@Override
10368
public void execute(JavaCompile compileTask) {
10469
addJvmArgs( compileTask,
10570
project.property( "toolchain.compiler.jvmargs" ).toString().split( " " )
10671
);
107-
compileTask.doFirst(
108-
new Action<Task>() {
109-
@Override
110-
public void execute(Task task) {
111-
project.getLogger().lifecycle(
112-
"Compiling with '{}'",
113-
compileTask.getJavaCompiler().get().getMetadata().getInstallationPath()
114-
);
72+
if ( compileTask.getName().equals( mainSourceSet.getCompileJavaTaskName() ) ) {
73+
compileTask.getOptions().getRelease().set( jdkVersionsConfig.getMainReleaseVersion().asInt() );
74+
if ( jdkVersionsConfig.getMain().isExplicit() ) {
75+
compileTask.getJavaCompiler().set(
76+
toolchainService.compilerFor( javaToolchainSpec -> {
77+
javaToolchainSpec.getLanguageVersion()
78+
.set( jdkVersionsConfig.getMainCompilerVersion() );
79+
} )
80+
);
81+
}
82+
}
83+
// Assume all non-main compile tasks are test compile tasks,
84+
// because that's currently true,
85+
// and there is no way to automatically determine whether a custom compile task if for main code or tests.
86+
else {
87+
compileTask.getOptions().getRelease().set( jdkVersionsConfig.getTestReleaseVersion().asInt() );
88+
if ( jdkVersionsConfig.getTest().isExplicit() ) {
89+
compileTask.getJavaCompiler().set(
90+
toolchainService.compilerFor( javaToolchainSpec -> {
91+
javaToolchainSpec.getLanguageVersion().set( jdkVersionsConfig.getTestCompilerVersion() );
92+
} )
93+
);
94+
}
95+
}
96+
if ( jdkVersionsConfig.isExplicit() ) {
97+
compileTask.doFirst(
98+
new Action<Task>() {
99+
@Override
100+
public void execute(Task task) {
101+
project.getLogger().lifecycle(
102+
"Compiling with '{}'",
103+
compileTask.getJavaCompiler().get().getMetadata().getInstallationPath()
104+
);
105+
}
115106
}
116-
}
117-
);
107+
);
108+
}
118109
}
119110
} );
120111
}
121112

122-
private void configureTestTasks(Project project) {
113+
private void configureTestTasks(Project project, JdkVersionConfig jdkVersionsConfig) {
123114
project.getTasks().withType( Test.class ).configureEach( new Action<Test>() {
124115
@Override
125116
public void execute(Test testTask) {
@@ -135,33 +126,45 @@ public void execute(Test testTask) {
135126
)
136127
);
137128
}
138-
testTask.doFirst(
139-
new Action<Task>() {
140-
@Override
141-
public void execute(Task task) {
142-
project.getLogger().lifecycle(
143-
"Testing with '{}'",
144-
testTask.getJavaLauncher().get().getMetadata().getInstallationPath()
145-
);
129+
if ( jdkVersionsConfig.getTest().isExplicit() ) {
130+
testTask.getJavaLauncher().set(
131+
toolchainService.launcherFor( javaToolchainSpec -> {
132+
javaToolchainSpec.getLanguageVersion()
133+
.set( jdkVersionsConfig.getTestLauncherVersion() );
134+
} )
135+
);
136+
}
137+
if ( jdkVersionsConfig.isExplicit() ) {
138+
testTask.doFirst(
139+
new Action<Task>() {
140+
@Override
141+
public void execute(Task task) {
142+
project.getLogger().lifecycle(
143+
"Testing with '{}'",
144+
testTask.getJavaLauncher().get().getMetadata().getInstallationPath()
145+
);
146+
}
146147
}
147-
}
148-
);
148+
);
149+
}
149150
}
150151
} );
151152
}
152153

153-
private void configureJavadocTasks(Project project, SourceSet mainSourceSet) {
154+
private void configureJavadocTasks(Project project, SourceSet mainSourceSet, JdkVersionConfig jdkVersionsConfig) {
154155
project.getTasks().named( mainSourceSet.getJavadocTaskName(), Javadoc.class, (task) -> {
155156
task.getOptions().setJFlags( javadocFlags( project ) );
156-
task.doFirst( new Action<Task>() {
157-
@Override
158-
public void execute(Task t) {
159-
project.getLogger().lifecycle(
160-
"Generating javadoc with '{}'",
161-
task.getJavadocTool().get().getMetadata().getInstallationPath()
162-
);
163-
}
164-
} );
157+
if ( jdkVersionsConfig.isExplicit() ) {
158+
task.doFirst( new Action<Task>() {
159+
@Override
160+
public void execute(Task t) {
161+
project.getLogger().lifecycle(
162+
"Generating javadoc with '{}'",
163+
task.getJavadocTool().get().getMetadata().getInstallationPath()
164+
);
165+
}
166+
} );
167+
}
165168
} );
166169
}
167170

local-build-plugins/src/main/java/org/hibernate/orm/toolchains/JdkVersionConfig.java

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -31,37 +31,27 @@ public class JdkVersionConfig {
3131
public static final String MAIN_JDK_VERSION = "main.jdk.version";
3232
public static final String TEST_JDK_VERSION = "test.jdk.version";
3333

34-
private final boolean explicit;
3534
private final JavaLanguageVersion baseline;
3635
private final JavaLanguageVersion min;
3736
private final JavaLanguageVersion max;
3837
private final MainJdks main;
3938
private final TestJdks test;
4039

4140
public JdkVersionConfig(
42-
boolean explicit,
4341
JavaLanguageVersion baseline,
4442
JavaLanguageVersion min,
4543
JavaLanguageVersion max,
46-
JavaLanguageVersion mainCompilerVersion,
47-
JavaLanguageVersion mainReleaseVersion,
48-
JavaLanguageVersion testCompileVersion,
49-
JavaLanguageVersion testReleaseVersion,
50-
JavaLanguageVersion testLauncherVersion) {
51-
this.explicit = explicit;
44+
MainJdks main,
45+
TestJdks test) {
5246
this.baseline = baseline;
5347
this.min = min;
5448
this.max = max;
55-
this.main = new MainJdks( mainCompilerVersion, mainReleaseVersion );
56-
this.test = new TestJdks( testCompileVersion, testReleaseVersion, testLauncherVersion );
57-
}
58-
59-
public boolean isExplicitlyConfigured() {
60-
return explicit;
49+
this.main = main;
50+
this.test = test;
6151
}
6252

6353
public boolean isExplicit() {
64-
return explicit;
54+
return main.isExplicit() || test.isExplicit();
6555
}
6656

6757
public JavaLanguageVersion getBaseline() {
@@ -174,15 +164,11 @@ public static JdkVersionConfig createVersionConfig(
174164
testLauncherVersion = testCompilerVersion;
175165

176166
return new JdkVersionConfig(
177-
true,
178167
baselineJdkVersion,
179168
minSupportedJdkVersion,
180169
maxSupportedJdkVersion,
181-
mainCompilerVersion,
182-
mainReleaseVersion,
183-
testCompilerVersion,
184-
testReleaseVersion,
185-
testLauncherVersion
170+
new MainJdks( mainCompilerVersion, mainReleaseVersion, explicitMainVersion != null ),
171+
new TestJdks( testCompilerVersion, testReleaseVersion, testLauncherVersion, explicitTestVersion != null )
186172
);
187173
}
188174
else {
@@ -206,15 +192,11 @@ public static JdkVersionConfig createVersionConfig(
206192
}
207193

208194
return new JdkVersionConfig(
209-
false,
210195
baselineJdkVersion,
211196
minSupportedJdkVersion,
212197
maxSupportedJdkVersion,
213-
gradleJdkVersion,
214-
baselineJdkVersion,
215-
gradleJdkVersion,
216-
baselineJdkVersion,
217-
gradleJdkVersion
198+
new MainJdks( gradleJdkVersion, baselineJdkVersion, false ),
199+
new TestJdks( gradleJdkVersion, baselineJdkVersion, gradleJdkVersion, false )
218200
);
219201
}
220202
}
@@ -251,10 +233,12 @@ public static JavaLanguageVersion extractVersion(Project project, String propert
251233
public static class MainJdks implements JdkVersionCombo {
252234
private final JavaLanguageVersion compilerVersion;
253235
private final JavaLanguageVersion releaseVersion;
236+
private final boolean explicit;
254237

255-
public MainJdks(JavaLanguageVersion compilerVersion, JavaLanguageVersion releaseVersion) {
238+
public MainJdks(JavaLanguageVersion compilerVersion, JavaLanguageVersion releaseVersion, boolean explicit) {
256239
this.compilerVersion = compilerVersion;
257240
this.releaseVersion = releaseVersion;
241+
this.explicit = explicit;
258242
}
259243

260244
public JavaLanguageVersion getCompiler() {
@@ -266,24 +250,31 @@ public JavaLanguageVersion getRelease() {
266250
return releaseVersion;
267251
}
268252

253+
@Override
254+
public boolean isExplicit() {
255+
return explicit;
256+
}
257+
269258
@Override
270259
public String toString() {
271-
return "[compiler: " + compilerVersion + ", release:" + releaseVersion + "]";
260+
return "[compiler: " + compilerVersion + ", release:" + releaseVersion + ", explicit: " + explicit + "]";
272261
}
273262
}
274263

275264
public static class TestJdks implements JdkVersionCombo {
276265
private final JavaLanguageVersion compilerVersion;
277266
private final JavaLanguageVersion releaseVersion;
278267
private final JavaLanguageVersion launcherVersion;
268+
private final boolean explicit;
279269

280270
public TestJdks(
281271
JavaLanguageVersion compilerVersion,
282272
JavaLanguageVersion releaseVersion,
283-
JavaLanguageVersion launcherVersion) {
273+
JavaLanguageVersion launcherVersion, boolean explicit) {
284274
this.compilerVersion = compilerVersion;
285275
this.releaseVersion = releaseVersion;
286276
this.launcherVersion = launcherVersion;
277+
this.explicit = explicit;
287278
}
288279

289280
@Override
@@ -300,14 +291,20 @@ public JavaLanguageVersion getLauncher() {
300291
return launcherVersion;
301292
}
302293

294+
@Override
295+
public boolean isExplicit() {
296+
return explicit;
297+
}
298+
303299
@Override
304300
public String toString() {
305-
return "[compiler: " + compilerVersion + ", release:" + releaseVersion + ", launcher: " + launcherVersion + "]";
301+
return "[compiler: " + compilerVersion + ", release:" + releaseVersion + ", launcher: " + launcherVersion + ", explicit: " + explicit + "]";
306302
}
307303
}
308304

309-
public interface JdkVersionCombo {
305+
public interface JdkVersionCombo {
310306
JavaLanguageVersion getCompiler();
311307
JavaLanguageVersion getRelease();
308+
boolean isExplicit();
312309
}
313310
}

local-build-plugins/src/main/java/org/hibernate/orm/toolchains/JdkVersionSettingsPlugin.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,11 @@ public void apply(Settings settings) {
5656
settings.getGradle().projectsLoaded( new Action<Gradle>() {
5757
@Override
5858
public void execute(Gradle gradle) {
59-
final String implicitExplicitString = jdkVersionConfig.isExplicit() ? "explicit" : "implicit";
60-
6159
System.out.println(
6260
"Java versions for main code: " + jdkVersionConfig.getMain()
63-
+ " (" + implicitExplicitString + ")"
6461
);
6562
System.out.println(
6663
"Java versions for test code: " + jdkVersionConfig.getTest()
67-
+ " (" + implicitExplicitString + ")"
6864
);
6965
}
7066
} );

0 commit comments

Comments
 (0)