-
Notifications
You must be signed in to change notification settings - Fork 135
Add Gradle 7 support #1824
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
Add Gradle 7 support #1824
Changes from 3 commits
bf1ecb1
bb125ec
94d7fff
2dd814a
a5805b4
fa89626
6245a97
6c00125
b7040f7
6522db5
00b4ba6
56aba78
96b5464
80a5f55
8e6de8d
787ce15
3982ae8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| type: improvement | ||
| improvement: | ||
| description: Add Gradle 7 support | ||
| links: | ||
| - https://github.com/palantir/gradle-baseline/pull/1824 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -50,11 +50,6 @@ public void apply(Project project) { | |
| proj.getPluginManager().apply(BaselineTestHeap.class); | ||
| proj.getPluginManager().apply(BaselineJavaParameters.class); | ||
| proj.getPluginManager().apply(BaselineImmutables.class); | ||
|
|
||
| // TODO(dsanduleac): enable this when people's idea{} blocks no longer reference things like | ||
| // configurations.integrationTestCompile | ||
| // proj.getPluginManager().apply(BaselineFixGradleJava.class); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lol |
||
|
|
||
| }); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -89,15 +89,13 @@ private static void configureSourceSet( | |
| TaskProvider<CheckImplicitDependenciesParentTask> checkImplicitDependencies) { | ||
| Configuration implementation = | ||
| project.getConfigurations().getByName(sourceSet.getImplementationConfigurationName()); | ||
| Configuration compile = project.getConfigurations().getByName(sourceSet.getCompileConfigurationName()); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This break is hard to work around. Ideally we would still get the compile configuration as an optional if present. However with Gradle 7, the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we just reimplement that logic while we support Gradle 6?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, its actually quite the simple method. Added it for now. |
||
| Configuration compileClasspath = | ||
| project.getConfigurations().getByName(sourceSet.getCompileClasspathConfigurationName()); | ||
|
|
||
| Configuration explicitCompile = project.getConfigurations() | ||
| .create("baseline-exact-dependencies-" + sourceSet.getName(), conf -> { | ||
| conf.setDescription(String.format( | ||
| "Tracks the explicit (not inherited) dependencies added to either %s or %s", | ||
| compile.toString(), implementation.toString())); | ||
| "Tracks the explicit (not inherited) dependencies added to either %s", implementation)); | ||
| conf.setVisible(false); | ||
| conf.setCanBeConsumed(false); | ||
|
|
||
|
|
@@ -137,18 +135,11 @@ private static void configureSourceSet( | |
| // configurations (belonging to our source set) | ||
| project.afterEvaluate(p -> { | ||
| Configuration implCopy = implementation.copy(); | ||
| Configuration compileCopy = compile.copy(); | ||
| // Ensure it's not resolvable, otherwise plugins that resolve all configurations might have | ||
| // a bad time resolving this with GCV, if you have direct dependencies without corresponding entries in | ||
| // versions.props, but instead rely on getting a version for them from the lock file. | ||
| compileCopy.setCanBeResolved(false); | ||
| compileCopy.setCanBeConsumed(false); | ||
| // Without these, explicitCompile will successfully resolve 0 files and you'll waste 1 hour trying | ||
| // to figure out why. | ||
| project.getConfigurations().add(implCopy); | ||
| project.getConfigurations().add(compileCopy); | ||
|
|
||
| explicitCompile.extendsFrom(implCopy, compileCopy); | ||
| explicitCompile.extendsFrom(implCopy); | ||
| }); | ||
|
|
||
| explicitCompile.withDependencies(deps -> { | ||
|
|
||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -56,8 +56,7 @@ private ReleaseFlagProvider(JavaCompile javaCompile) { | |
|
|
||
| @Override | ||
| public Iterable<String> asArguments() { | ||
| JavaVersion jdkVersion = | ||
| JavaVersion.toVersion(javaCompile.getToolChain().getVersion()); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The I replaced it with
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does something like: Optional<JavaVersion> taskTarget = Optional.ofNullable(javaCompile
.getProject()
.getExtensions()
.getByType(JavaPluginExtension.class)
.getToolchain()
.getLanguageVersion()
.getOrNull())
.map(JavaLanguageVersion::asInt)
.map(JavaVersion::toVersion);replace it exactly? Not sure if it's an exact replacement, but tests seem to pass at least.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think what we can do is use Only thing is that the new |
||
| JavaVersion jdkVersion = JavaVersion.toVersion(javaCompile.getTargetCompatibility()); | ||
| if (!supportsReleaseFlag(jdkVersion)) { | ||
| log.debug( | ||
| "BaselineReleaseCompatibility is a no-op for {} in {} as {} doesn't support --release", | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ | |
| package com.palantir.baseline.tasks; | ||
|
|
||
| import java.io.IOException; | ||
| import java.lang.reflect.Method; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.StandardOpenOption; | ||
|
|
@@ -28,6 +29,8 @@ | |
| import org.gradle.api.Task; | ||
| import org.gradle.api.model.ObjectFactory; | ||
| import org.gradle.api.plugins.JavaPluginConvention; | ||
| import org.gradle.api.plugins.JavaPluginExtension; | ||
| import org.gradle.api.plugins.internal.DefaultJavaPluginExtension; | ||
| import org.gradle.api.provider.Property; | ||
| import org.gradle.api.publish.PublishingExtension; | ||
| import org.gradle.api.specs.Spec; | ||
|
|
@@ -92,12 +95,7 @@ public final void setShouldFix(boolean value) { | |
|
|
||
| @TaskAction | ||
| public final void taskAction() throws IOException { | ||
| // We're doing this naughty casting because we need access to the `getRawSourceCompatibility` method. | ||
| org.gradle.api.plugins.internal.DefaultJavaPluginConvention convention = | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The internal API got moved to a different class. Naively, we could fix this with reflection based on the runtime Gradle version but that doesn't sound sustainable. Maybe this is a good time to lean into toolchains?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. doesn't java plugin extension exist since gradle 4.10? Maybe we can standardise on that?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The extension exists since then but the internal method I'll do a bit more digging. Still hoping we find a non-internal way of figuring out if sourceCompat is explicitly set. Another alternative would be to actually look into projects gradle files and fail of sourceCompat is not set, similar to the baseline error-prone rules. |
||
| (org.gradle.api.plugins.internal.DefaultJavaPluginConvention) | ||
| getProject().getConvention().getPlugin(JavaPluginConvention.class); | ||
|
|
||
| if (convention.getRawSourceCompatibility() != null) { | ||
| if (getRawSourceCompat() != null) { | ||
| // In theory, users could configure the fancy new 'java toolchain' as an alternative to explicit | ||
| // sourceCompatibility, but there's no method to access this yet (as of Gradle 6.8). | ||
| return; | ||
|
|
@@ -127,4 +125,24 @@ public final void taskAction() throws IOException { | |
| JavaVersion.current(), | ||
| getPath())); | ||
| } | ||
|
|
||
| private JavaVersion getRawSourceCompat() { | ||
| // TODO(fwindheuser): Remove internal api usage. Maybe through adopting toolchains? | ||
| // We're doing this naughty casting because we need access to the `getRawSourceCompatibility` method. | ||
| if (GradleVersion.current().compareTo(GradleVersion.version("7.0")) < 0) { | ||
| org.gradle.api.plugins.internal.DefaultJavaPluginConvention convention = | ||
| (org.gradle.api.plugins.internal.DefaultJavaPluginConvention) | ||
| getProject().getConvention().getPlugin(JavaPluginConvention.class); | ||
| return convention.getRawSourceCompatibility(); | ||
| } | ||
|
|
||
| DefaultJavaPluginExtension extension = | ||
| (DefaultJavaPluginExtension) getProject().getExtensions().getByType(JavaPluginExtension.class); | ||
| try { | ||
| Method rawSourceCompat = DefaultJavaPluginExtension.class.getMethod("getRawSourceCompatibility"); | ||
|
||
| return (JavaVersion) rawSourceCompat.invoke(extension); | ||
| } catch (Exception e) { | ||
| throw new RuntimeException("Error calling Gradle 7 getRawSourceCompatibility", e); | ||
| } | ||
| } | ||
| } | ||
This file was deleted.
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.
Do we require a new minimum gradle version now? If so, should probably make it a break.
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 we decide with this one then we would require Gradle 6.7.
If we avoid this compile dependency e.g. through reflection, I don't think we introduce a new min version.