-
Notifications
You must be signed in to change notification settings - Fork 40.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow the project to be built with Java 16 #25171
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ffb0dbe
Support running builds with custom toolchain version
dreis2211 289f889
Set custom toolchain value inside build scans
dreis2211 da50fce
Update copyright of changed file
dreis2211 534aab1
Extract toolchain support into its own plugin
dreis2211 File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
buildSrc/src/main/java/org/springframework/boot/build/toolchain/ToolchainExtension.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright 2012-2021 the original author or authors. | ||
* | ||
* Licensed 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 | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.boot.build.toolchain; | ||
|
||
import java.util.Optional; | ||
|
||
import org.gradle.api.Project; | ||
import org.gradle.jvm.toolchain.JavaLanguageVersion; | ||
|
||
/** | ||
* DSL extension for {@link ToolchainPlugin}. | ||
* | ||
* @author Christoph Dreis | ||
*/ | ||
public class ToolchainExtension { | ||
|
||
private final Project project; | ||
|
||
private int maximumCompatibleJavaVersion; | ||
|
||
public ToolchainExtension(Project project) { | ||
this.project = project; | ||
} | ||
|
||
public void setMaximumCompatibleJavaVersion(int maximumVersion) { | ||
this.maximumCompatibleJavaVersion = maximumVersion; | ||
} | ||
|
||
public Optional<JavaLanguageVersion> getToolchainVersion() { | ||
String toolchainVersion = (String) this.project.findProperty("toolchainVersion"); | ||
if (toolchainVersion == null) { | ||
return Optional.empty(); | ||
} | ||
int version = Integer.parseInt(toolchainVersion); | ||
return getJavaLanguageVersion(version); | ||
} | ||
|
||
public boolean isJavaVersionSupported() { | ||
Optional<JavaLanguageVersion> maximumVersion = getJavaLanguageVersion(this.maximumCompatibleJavaVersion); | ||
if (!maximumVersion.isPresent()) { | ||
return true; | ||
} | ||
Optional<JavaLanguageVersion> toolchainVersion = getToolchainVersion(); | ||
return toolchainVersion.isPresent() && maximumVersion.get().canCompileOrRun(toolchainVersion.get()); | ||
} | ||
|
||
private Optional<JavaLanguageVersion> getJavaLanguageVersion(int version) { | ||
if (version >= 8) { | ||
return Optional.of(JavaLanguageVersion.of(version)); | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
} |
116 changes: 116 additions & 0 deletions
116
buildSrc/src/main/java/org/springframework/boot/build/toolchain/ToolchainPlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
* Copyright 2012-2021 the original author or authors. | ||
* | ||
* Licensed 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 | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.boot.build.toolchain; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import org.gradle.api.Action; | ||
import org.gradle.api.Plugin; | ||
import org.gradle.api.Project; | ||
import org.gradle.api.tasks.compile.JavaCompile; | ||
import org.gradle.api.tasks.javadoc.Javadoc; | ||
import org.gradle.api.tasks.testing.Test; | ||
import org.gradle.jvm.toolchain.JavaLanguageVersion; | ||
import org.gradle.jvm.toolchain.JavaToolchainService; | ||
import org.gradle.jvm.toolchain.JavaToolchainSpec; | ||
|
||
/** | ||
* {@link Plugin} for customizing Gradle's toolchain support. | ||
* | ||
* @author Christoph Dreis | ||
*/ | ||
public class ToolchainPlugin implements Plugin<Project> { | ||
|
||
@Override | ||
public void apply(Project project) { | ||
configureToolchain(project); | ||
} | ||
|
||
private void configureToolchain(Project project) { | ||
ToolchainExtension toolchain = project.getExtensions().create("toolchain", ToolchainExtension.class, project); | ||
project.afterEvaluate((evaluated) -> { | ||
Optional<JavaLanguageVersion> toolchainVersion = toolchain.getToolchainVersion(); | ||
if (toolchainVersion.isPresent()) { | ||
if (!toolchain.isJavaVersionSupported()) { | ||
disableToolchainTasks(project); | ||
} | ||
else { | ||
configureJavaCompileToolchain(project, toolchain); | ||
configureJavadocToolchain(project, toolchain); | ||
configureTestToolchain(project, toolchain); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
private void disableToolchainTasks(Project project) { | ||
project.getTasks().withType(JavaCompile.class, (task) -> task.setEnabled(false)); | ||
project.getTasks().withType(Javadoc.class, (task) -> task.setEnabled(false)); | ||
project.getTasks().withType(Test.class, (task) -> task.setEnabled(false)); | ||
} | ||
|
||
private void configureJavaCompileToolchain(Project project, ToolchainExtension toolchain) { | ||
project.getTasks().withType(JavaCompile.class, (compile) -> { | ||
withOptionalJavaToolchain(toolchain).ifPresent((action) -> { | ||
JavaToolchainService service = getJavaToolchainService(project); | ||
compile.getJavaCompiler().set(service.compilerFor(action)); | ||
compile.getOptions().setFork(true); | ||
// See https://github.com/gradle/gradle/issues/15538 | ||
List<String> forkArgs = Arrays.asList("--add-opens", | ||
"jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED"); | ||
compile.getOptions().getForkOptions().getJvmArgs().addAll(forkArgs); | ||
}); | ||
}); | ||
} | ||
|
||
private void configureJavadocToolchain(Project project, ToolchainExtension toolchain) { | ||
project.getTasks().withType(Javadoc.class, (javadoc) -> { | ||
withOptionalJavaToolchain(toolchain).ifPresent((action) -> { | ||
JavaToolchainService service = getJavaToolchainService(project); | ||
javadoc.getJavadocTool().set(service.javadocToolFor(action)); | ||
}); | ||
}); | ||
} | ||
|
||
private void configureTestToolchain(Project project, ToolchainExtension toolchain) { | ||
project.getTasks().withType(Test.class, (test) -> { | ||
withOptionalJavaToolchain(toolchain).ifPresent((action) -> { | ||
JavaToolchainService service = getJavaToolchainService(project); | ||
test.getJavaLauncher().set(service.launcherFor(action)); | ||
// See https://github.com/spring-projects/spring-ldap/issues/570 | ||
List<String> arguments = Arrays.asList("--add-exports=java.naming/com.sun.jndi.ldap=ALL-UNNAMED", | ||
"--illegal-access=warn"); | ||
test.jvmArgs(arguments); | ||
}); | ||
}); | ||
} | ||
|
||
private JavaToolchainService getJavaToolchainService(Project project) { | ||
return project.getExtensions().getByType(JavaToolchainService.class); | ||
} | ||
|
||
private Optional<Action<JavaToolchainSpec>> withOptionalJavaToolchain(ToolchainExtension toolchain) { | ||
return toolchain.getToolchainVersion().map((toolchainVersion) -> { | ||
Action<JavaToolchainSpec> action = (javaToolchainSpec) -> javaToolchainSpec.getLanguageVersion() | ||
.convention(toolchainVersion); | ||
return Optional.of(action); | ||
}).orElse(Optional.empty()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...t/spring-boot-test/src/test/java/org/springframework/boot/test/json/JsonbTesterTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea/hope is that the build scans show the actual toolchain, but we'll see. Couldn't test this really.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's a scan against
2.4.x
with-PtoolchainVersion=16
: https://ge.spring.io/s/yadj75gujofi6There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder why 2.4.x is targeted here? I guess we would need Groovy 3 as well in the 2.4.x mainline to make this work. See #24946
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Support for Java 16 starts with Spring Framework 5.3 which is what Spring Boot 2.4+ uses. We need a GA release of Spring Boot that folks using Java 15 could rely on once 16 is out (given that 15 will be effectively EOL at that time).
As for Groovy 3, perhaps we could upgrade to it on the Java 16 build?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I would need a little help on getting this to work on 2.4.x - I currently don't see a good way to do this sort of conditional dependency upgrade
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, this would only happen for the actual pipelines e.g. via
-x :spring-boot-project:spring-boot-cli:test
. I wouldn't know what I should update in this PR - cluttering every CLI test with@DisabledForJreRange
seems like an overkill.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rather find a way to disable tests for the CLI module altogether with Java 16. When we had a Maven build we used profiles to disable Surefire. I guess Gradle should have a similar feature.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you know one that is also aware of the toolchain version, that would be great. We could also use that for the gradle plugin then. I just don't know how we could exclude a whole module based on the toolchain version - apart from coming up with our own way. Let me know if I should experiment around that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we'll have to come up with our own way. Alternatively, we could use
@DisabledForJreRange
all over the place. One advantage of that is the tests are disabled no matter how they're launched.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm currently writing a custom plugin that lets you specify the maximum compatible java version inside the build.gradle for that particular submodule. The plan is that this information is consumed in order to dynamically disable the things that run with the optional toolchain.