Skip to content

Commit

Permalink
Remove usage of deprecated JavaPluginConvention (#75106) (#75740)
Browse files Browse the repository at this point in the history
this has been deprecated and will be removed in Gradle 8.0. We use
JavaPluginExtension instead from now on.

Co-authored-by: Rene Groeschke <[email protected]>
  • Loading branch information
elasticsearchmachine and breskeby authored Jul 28, 2021
1 parent 3cc15e1 commit 671e76c
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.plugins.JavaBasePlugin;
import org.gradle.api.plugins.JavaPluginConvention;
import org.gradle.api.plugins.JavaPluginExtension;
import org.gradle.api.provider.ProviderFactory;
import org.gradle.api.tasks.SourceSetContainer;
import org.gradle.api.tasks.TaskProvider;
Expand All @@ -29,7 +29,7 @@ public LicenseHeadersPrecommitPlugin(ProviderFactory providerFactory) {
public TaskProvider<? extends Task> createTask(Project project) {
return project.getTasks().register("licenseHeaders", LicenseHeadersTask.class, licenseHeadersTask -> {
project.getPlugins().withType(JavaBasePlugin.class, javaBasePlugin -> {
final SourceSetContainer sourceSets = project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets();
final SourceSetContainer sourceSets = project.getExtensions().getByType(JavaPluginExtension.class).getSourceSets();
licenseHeadersTask.getSourceFolders()
.addAll(providerFactory.provider(() -> sourceSets.stream().map(s -> s.getAllJava()).collect(Collectors.toList())));
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.plugins.JavaPluginConvention;
import org.gradle.api.plugins.JavaPluginExtension;
import org.gradle.api.tasks.TaskProvider;

/**
Expand All @@ -29,7 +29,7 @@ public final void apply(Project project) {
precommit.configure(t -> t.dependsOn(task));
project.getPluginManager().withPlugin("java", p -> {
// We want to get any compilation error before running the pre-commit checks.
project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets().all(sourceSet ->
project.getExtensions().getByType(JavaPluginExtension.class).getSourceSets().all(sourceSet ->
task.configure(t -> t.shouldRunAfter(sourceSet.getClassesTaskName()))
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@
import org.gradle.api.GradleException;
import org.gradle.api.Project;
import org.gradle.api.file.FileTree;
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.util.PatternFilterable;

import javax.annotation.Nullable;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Optional;
import java.util.function.Supplier;

Expand Down Expand Up @@ -85,7 +83,7 @@ public static FileTree getJavaTestAndMainSourceResources(Project project, Action
* @return An Optional that contains the Java test SourceSet if it exists.
*/
public static Optional<SourceSet> getJavaTestSourceSet(Project project) {
return project.getConvention().findPlugin(JavaPluginConvention.class) == null
return project.getExtensions().findByName("java") == null
? Optional.empty()
: Optional.ofNullable(getJavaSourceSets(project).findByName(SourceSet.TEST_SOURCE_SET_NAME));
}
Expand All @@ -95,11 +93,15 @@ public static Optional<SourceSet> getJavaTestSourceSet(Project project) {
* @return An Optional that contains the Java main SourceSet if it exists.
*/
public static Optional<SourceSet> getJavaMainSourceSet(Project project) {
return project.getConvention().findPlugin(JavaPluginConvention.class) == null
return isJavaExtensionAvailable(project)
? Optional.empty()
: Optional.ofNullable(getJavaSourceSets(project).findByName(SourceSet.MAIN_SOURCE_SET_NAME));
}

private static boolean isJavaExtensionAvailable(Project project) {
return project.getExtensions().getByType(JavaPluginExtension.class) == null;
}


public static Object toStringable(Supplier<String> getter) {
return new Object() {
Expand All @@ -111,7 +113,7 @@ public String toString() {
}

public static SourceSetContainer getJavaSourceSets(Project project) {
return project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets();
return project.getExtensions().getByType(JavaPluginExtension.class).getSourceSets();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.gradle.api.artifacts.ProjectDependency;
import org.gradle.api.plugins.JavaBasePlugin;
import org.gradle.api.plugins.JavaPluginConvention;
import org.gradle.api.plugins.JavaPluginExtension;
import org.gradle.api.provider.Provider;
import org.gradle.api.services.BuildService;
import org.gradle.api.services.BuildServiceRegistration;
Expand Down Expand Up @@ -46,7 +47,7 @@ public static <T> Action<T> noop() {
}

public static SourceSetContainer getJavaSourceSets(Project project) {
return project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets();
return project.getExtensions().getByType(JavaPluginExtension.class).getSourceSets();
}

public static void maybeConfigure(TaskContainer tasks, String name, Action<? super Task> config) {
Expand Down

0 comments on commit 671e76c

Please sign in to comment.