Skip to content

Commit

Permalink
Stop using JavaPluginConvention for Gradle 7.1+ (#299)
Browse files Browse the repository at this point in the history
The org.gradle.api.plugins.JavaPluginConvention type has been deprecated. This is scheduled to be removed in Gradle 9.0. Consult the upgrading guide for further information: https://docs.gradle.org/8.7/userguide/upgrading_version_8.html#java_convention_deprecation

Co-authored-by: Tim te Beek <[email protected]>
  • Loading branch information
quaff and timtebeek authored May 19, 2024
1 parent 672d7cb commit 6121f79
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 13 deletions.
18 changes: 13 additions & 5 deletions plugin/src/main/java/org/openrewrite/gradle/RewritePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.plugins.JavaBasePlugin;
import org.gradle.api.plugins.JavaPluginConvention;
import org.gradle.api.plugins.JavaPluginExtension;
import org.gradle.api.plugins.quality.CheckstyleExtension;
import org.gradle.api.plugins.quality.CheckstylePlugin;
import org.gradle.api.tasks.SourceSetContainer;

import java.io.File;
import java.util.Comparator;
Expand Down Expand Up @@ -98,10 +100,16 @@ private static void configureProject(Project project, RewriteExtension extension
}

//Collect Java metadata for each project (used for Java Provenance)
//Using the older javaConvention because we need to support older versions of gradle.
@SuppressWarnings("deprecation")
JavaPluginConvention javaConvention = project.getConvention().getPlugin(JavaPluginConvention.class);
javaConvention.getSourceSets().all(sourceSet -> {
SourceSetContainer sourceSets;
if (project.getGradle().getGradleVersion().compareTo("7.1") >= 0) {
sourceSets = project.getExtensions().getByType(JavaPluginExtension.class).getSourceSets();
} else {
//Using the older javaConvention because we need to support older versions of gradle.
@SuppressWarnings("deprecation")
JavaPluginConvention javaConvention = project.getConvention().getPlugin(JavaPluginConvention.class);
sourceSets = javaConvention.getSourceSets();
}
sourceSets.all(sourceSet -> {
// This is intended to ensure that any Groovy/Kotlin/etc. and dependent project sources are available
Task compileTask = project.getTasks().getByPath(sourceSet.getCompileJavaTaskName());
rewriteRun.dependsOn(compileTask);
Expand All @@ -111,7 +119,7 @@ private static void configureProject(Project project, RewriteExtension extension
// Detect SourceSets which overlap other sourceSets and disable the compilation task of the overlapping
// source set. Some plugins will create source sets not intended to be compiled for their own purposes.
Set<String> sourceDirs = new HashSet<>();
project.afterEvaluate(unused -> javaConvention.getSourceSets().stream()
project.afterEvaluate(unused -> sourceSets.stream()
.sorted(Comparator.comparingInt(sourceSet -> {
if ("main".equals(sourceSet.getName())) {
return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
import org.gradle.api.logging.Logging;
import org.gradle.api.plugins.GroovyPlugin;
import org.gradle.api.plugins.JavaPluginConvention;
import org.gradle.api.plugins.JavaPluginExtension;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.SourceSetContainer;
import org.gradle.api.tasks.compile.CompileOptions;
import org.gradle.api.tasks.compile.JavaCompile;
import org.gradle.internal.service.ServiceRegistry;
Expand Down Expand Up @@ -282,10 +284,9 @@ private static StringBuilder repeat(int repeat) {
public Collection<Path> listSources() {
// Use a sorted collection so that gradle input detection isn't thrown off by ordering
Set<Path> result = new TreeSet<>(omniParser(emptySet(), project).acceptedPaths(baseDir, project.getProjectDir().toPath()));
//noinspection deprecation
JavaPluginConvention javaConvention = project.getConvention().findPlugin(JavaPluginConvention.class);
if (javaConvention != null) {
for (SourceSet sourceSet : javaConvention.getSourceSets()) {
SourceSetContainer sourceSets = findSourceSetContainer(project);
if (sourceSets != null) {
for (SourceSet sourceSet : sourceSets) {
sourceSet.getAllSource().getFiles().stream()
.map(File::toPath)
.map(Path::toAbsolutePath)
Expand Down Expand Up @@ -663,11 +664,10 @@ public Stream<SourceFile> parse(Project subproject, Set<Path> alreadyParsed, Exe
logger.lifecycle("Scanning sources in project {}", subproject.getPath());
List<NamedStyles> styles = getStyles();
logger.lifecycle("Using active styles {}", styles.stream().map(NamedStyles::getName).collect(toList()));
@SuppressWarnings("deprecation")
JavaPluginConvention javaConvention = subproject.getConvention().findPlugin(JavaPluginConvention.class);
SourceSetContainer sourceSetContainer = findSourceSetContainer(subproject);
List<SourceSet> sourceSets;
List<Marker> projectProvenance;
if (javaConvention == null) {
if (sourceSetContainer == null) {
projectProvenance = sharedProvenance;
sourceSets = emptyList();
} else {
Expand All @@ -676,7 +676,7 @@ public Stream<SourceFile> parse(Project subproject, Set<Path> alreadyParsed, Exe
new JavaProject.Publication(subproject.getGroup().toString(),
subproject.getName(),
subproject.getVersion().toString())));
sourceSets = javaConvention.getSourceSets().stream()
sourceSets = sourceSetContainer.stream()
.sorted(Comparator.comparingInt(sourceSet -> {
if ("main".equals(sourceSet.getName())) {
return 0;
Expand Down Expand Up @@ -1324,4 +1324,23 @@ private void logRecipe(RecipeDescriptor rd, String prefix) {
logRecipe(rChild, prefix + " ");
}
}

@Nullable
private SourceSetContainer findSourceSetContainer(Project project) {
SourceSetContainer sourceSets = null;
if (project.getGradle().getGradleVersion().compareTo("7.1") >= 0) {
JavaPluginExtension javaPluginExtension = project.getExtensions().findByType(JavaPluginExtension.class);
if (javaPluginExtension != null) {
sourceSets = javaPluginExtension.getSourceSets();
}
} else {
//Using the older javaConvention because we need to support older versions of gradle.
@SuppressWarnings("deprecation")
JavaPluginConvention javaConvention = project.getConvention().findPlugin(JavaPluginConvention.class);
if (javaConvention != null) {
sourceSets = javaConvention.getSourceSets();
}
}
return sourceSets;
}
}

0 comments on commit 6121f79

Please sign in to comment.