From ddd1ee237ca36b4751988e594554e915e0009696 Mon Sep 17 00:00:00 2001 From: Konstantin Gribov Date: Fri, 22 Apr 2022 07:50:55 +0300 Subject: [PATCH] Get SourceSetContainer from extensions instead of legacy JavaPluginConvention Signed-off-by: Konstantin Gribov --- .../java/io/quarkus/gradle/QuarkusPlugin.java | 7 +- .../gradle/tasks/QuarkusGenerateCode.java | 67 +++++++++---------- .../gradle/tasks/QuarkusGradleUtils.java | 12 ++-- .../gradle/QuarkusExtensionPlugin.java | 6 +- .../GradleApplicationModelBuilder.java | 42 ++++++------ .../tooling/dependency/DependencyUtils.java | 10 +-- 6 files changed, 69 insertions(+), 75 deletions(-) diff --git a/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/QuarkusPlugin.java b/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/QuarkusPlugin.java index 8a42717850df93..a4810ac14ba336 100644 --- a/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/QuarkusPlugin.java +++ b/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/QuarkusPlugin.java @@ -21,7 +21,6 @@ import org.gradle.api.artifacts.ProjectDependency; import org.gradle.api.plugins.BasePlugin; import org.gradle.api.plugins.JavaPlugin; -import org.gradle.api.plugins.JavaPluginConvention; import org.gradle.api.tasks.SourceSet; import org.gradle.api.tasks.SourceSetContainer; import org.gradle.api.tasks.TaskContainer; @@ -245,8 +244,7 @@ public void execute(Task test) { quarkusBuild.configure( task -> task.dependsOn(classesTask, resourcesTask, tasks.named(JavaPlugin.JAR_TASK_NAME))); - SourceSetContainer sourceSets = project.getConvention().getPlugin(JavaPluginConvention.class) - .getSourceSets(); + SourceSetContainer sourceSets = project.getExtensions().getByType(SourceSetContainer.class); SourceSet mainSourceSet = sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME); SourceSet testSourceSet = sourceSets.getByName(SourceSet.TEST_SOURCE_SET_NAME); @@ -384,8 +382,7 @@ private void afterEvaluate(Project project) { .sourceSetExtension(); if (sourceSetExtension.extraNativeTest() != null) { - SourceSetContainer sourceSets = project.getConvention().getPlugin(JavaPluginConvention.class) - .getSourceSets(); + SourceSetContainer sourceSets = project.getExtensions().getByType(SourceSetContainer.class); SourceSet nativeTestSourceSets = sourceSets.getByName(NATIVE_TEST_SOURCE_SET_NAME); nativeTestSourceSets.setCompileClasspath( nativeTestSourceSets.getCompileClasspath() diff --git a/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/tasks/QuarkusGenerateCode.java b/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/tasks/QuarkusGenerateCode.java index 8a3889f0424f04..3396e25626cace 100644 --- a/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/tasks/QuarkusGenerateCode.java +++ b/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/tasks/QuarkusGenerateCode.java @@ -16,11 +16,11 @@ import org.gradle.api.GradleException; import org.gradle.api.artifacts.Configuration; -import org.gradle.api.plugins.JavaPluginConvention; import org.gradle.api.tasks.CompileClasspath; import org.gradle.api.tasks.InputFiles; import org.gradle.api.tasks.OutputDirectory; import org.gradle.api.tasks.SourceSet; +import org.gradle.api.tasks.SourceSetContainer; import org.gradle.api.tasks.TaskAction; import io.quarkus.bootstrap.BootstrapException; @@ -108,42 +108,39 @@ public void prepareQuarkus() { .setIsolateDeployment(true) .build().bootstrap()) { - final JavaPluginConvention javaConvention = getProject().getConvention().findPlugin(JavaPluginConvention.class); - if (javaConvention != null) { - final String generateSourcesDir = test ? QUARKUS_TEST_GENERATED_SOURCES : QUARKUS_GENERATED_SOURCES; - final SourceSet generatedSources = javaConvention.getSourceSets().findByName(generateSourcesDir); - List paths = new ArrayList<>(); - generatedSources.getOutput() - .filter(f -> f.getName().equals(generateSourcesDir)) - .forEach(f -> paths.add(f.toPath())); - if (paths.isEmpty()) { - throw new GradleException("Failed to create quarkus-generated-sources"); - } - - getLogger().debug("Will trigger preparing sources for source directory: {} buildDir: {}", - sourcesDirectories, getProject().getBuildDir().getAbsolutePath()); - - QuarkusClassLoader deploymentClassLoader = appCreationContext.createDeploymentClassLoader(); - Class codeGenerator = deploymentClassLoader.loadClass(CodeGenerator.class.getName()); - - Optional initAndRun = Arrays.stream(codeGenerator.getMethods()) - .filter(m -> m.getName().equals(INIT_AND_RUN)) - .findAny(); - if (initAndRun.isEmpty()) { - throw new GradleException("Failed to find " + INIT_AND_RUN + " method in " + CodeGenerator.class.getName()); - } - - initAndRun.get().invoke(null, deploymentClassLoader, - PathList.from(sourcesDirectories), - paths.get(0), - buildDir, - sourceRegistrar, - appCreationContext.getApplicationModel(), - realProperties, - launchMode.name(), - test); + SourceSetContainer sourceSets = getProject().getExtensions().getByType(SourceSetContainer.class); + final String generateSourcesDir = test ? QUARKUS_TEST_GENERATED_SOURCES : QUARKUS_GENERATED_SOURCES; + final SourceSet generatedSources = sourceSets.findByName(generateSourcesDir); + List paths = new ArrayList<>(); + generatedSources.getOutput() + .filter(f -> f.getName().equals(generateSourcesDir)) + .forEach(f -> paths.add(f.toPath())); + if (paths.isEmpty()) { + throw new GradleException("Failed to create quarkus-generated-sources"); + } + + getLogger().debug("Will trigger preparing sources for source directory: {} buildDir: {}", + sourcesDirectories, getProject().getBuildDir().getAbsolutePath()); + QuarkusClassLoader deploymentClassLoader = appCreationContext.createDeploymentClassLoader(); + Class codeGenerator = deploymentClassLoader.loadClass(CodeGenerator.class.getName()); + + Optional initAndRun = Arrays.stream(codeGenerator.getMethods()) + .filter(m -> m.getName().equals(INIT_AND_RUN)) + .findAny(); + if (initAndRun.isEmpty()) { + throw new GradleException("Failed to find " + INIT_AND_RUN + " method in " + CodeGenerator.class.getName()); } + + initAndRun.get().invoke(null, deploymentClassLoader, + PathList.from(sourcesDirectories), + paths.get(0), + buildDir, + sourceRegistrar, + appCreationContext.getApplicationModel(), + realProperties, + launchMode.name(), + test); } catch (BootstrapException | IllegalAccessException | InvocationTargetException | ClassNotFoundException e) { throw new GradleException("Failed to generate sources in the QuarkusPrepare task", e); } diff --git a/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/tasks/QuarkusGradleUtils.java b/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/tasks/QuarkusGradleUtils.java index c180773de6c512..fc38292cf7498a 100644 --- a/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/tasks/QuarkusGradleUtils.java +++ b/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/tasks/QuarkusGradleUtils.java @@ -12,8 +12,8 @@ import org.gradle.api.Project; import org.gradle.api.file.FileCollection; -import org.gradle.api.plugins.JavaPluginConvention; import org.gradle.api.tasks.SourceSet; +import org.gradle.api.tasks.SourceSetContainer; import io.quarkus.bootstrap.util.IoUtils; @@ -21,12 +21,12 @@ public class QuarkusGradleUtils { private static final String ERROR_COLLECTING_PROJECT_CLASSES = "Failed to collect project's classes in a temporary dir"; + public static SourceSetContainer getSourceSets(Project project) { + return project.getExtensions().getByType(SourceSetContainer.class); + } + public static SourceSet getSourceSet(Project project, String sourceSetName) { - final JavaPluginConvention javaConvention = project.getConvention().findPlugin(JavaPluginConvention.class); - if (javaConvention == null) { - throw new IllegalArgumentException("The project does not include the Java plugin"); - } - return javaConvention.getSourceSets().getByName(sourceSetName); + return getSourceSets(project).getByName(sourceSetName); } public static String getClassesDir(SourceSet sourceSet, File tmpDir, boolean test) { diff --git a/devtools/gradle/gradle-extension-plugin/src/main/java/io/quarkus/extension/gradle/QuarkusExtensionPlugin.java b/devtools/gradle/gradle-extension-plugin/src/main/java/io/quarkus/extension/gradle/QuarkusExtensionPlugin.java index 9c0d267119c664..f769f0be0d418a 100644 --- a/devtools/gradle/gradle-extension-plugin/src/main/java/io/quarkus/extension/gradle/QuarkusExtensionPlugin.java +++ b/devtools/gradle/gradle-extension-plugin/src/main/java/io/quarkus/extension/gradle/QuarkusExtensionPlugin.java @@ -12,8 +12,8 @@ import org.gradle.api.artifacts.ModuleVersionIdentifier; import org.gradle.api.artifacts.ResolvedArtifact; import org.gradle.api.plugins.JavaPlugin; -import org.gradle.api.plugins.JavaPluginConvention; import org.gradle.api.tasks.SourceSet; +import org.gradle.api.tasks.SourceSetContainer; import org.gradle.api.tasks.TaskContainer; import org.gradle.api.tasks.TaskProvider; import org.gradle.api.tasks.testing.Test; @@ -49,8 +49,8 @@ public void apply(Project project) { private void registerTasks(Project project, QuarkusExtensionConfiguration quarkusExt) { TaskContainer tasks = project.getTasks(); - JavaPluginConvention convention = project.getConvention().getPlugin(JavaPluginConvention.class); - SourceSet mainSourceSet = convention.getSourceSets().getByName(SourceSet.MAIN_SOURCE_SET_NAME); + SourceSetContainer sourceSets = project.getExtensions().getByType(SourceSetContainer.class); + SourceSet mainSourceSet = sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME); Configuration runtimeModuleClasspath = project.getConfigurations() .getByName(JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME); diff --git a/devtools/gradle/gradle-model/src/main/java/io/quarkus/gradle/tooling/GradleApplicationModelBuilder.java b/devtools/gradle/gradle-model/src/main/java/io/quarkus/gradle/tooling/GradleApplicationModelBuilder.java index 6bfd7177c63af1..ccebec9c535f9e 100644 --- a/devtools/gradle/gradle-model/src/main/java/io/quarkus/gradle/tooling/GradleApplicationModelBuilder.java +++ b/devtools/gradle/gradle-model/src/main/java/io/quarkus/gradle/tooling/GradleApplicationModelBuilder.java @@ -14,6 +14,7 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Properties; import java.util.Set; @@ -26,8 +27,8 @@ import org.gradle.api.file.FileCollection; import org.gradle.api.file.FileTree; import org.gradle.api.initialization.IncludedBuild; -import org.gradle.api.plugins.JavaPluginConvention; import org.gradle.api.tasks.SourceSet; +import org.gradle.api.tasks.SourceSetContainer; import org.gradle.api.tasks.compile.AbstractCompile; import org.gradle.language.jvm.tasks.ProcessResources; import org.gradle.tooling.provider.model.ParameterizedToolingModelBuilder; @@ -130,20 +131,17 @@ public static ResolvedDependency getProjectArtifact(Project project, boolean wor .setArtifactId(project.getName()) .setVersion(project.getVersion().toString()); - final JavaPluginConvention javaConvention = project.getConvention().findPlugin(JavaPluginConvention.class); - if (javaConvention == null) { - throw new GradleException("Failed to locate Java plugin extension in " + project.getPath()); - } + SourceSetContainer sourceSets = project.getExtensions().getByType(SourceSetContainer.class); final WorkspaceModule.Mutable mainModule = WorkspaceModule.builder() .setModuleId(new GAV(appArtifact.getGroupId(), appArtifact.getArtifactId(), appArtifact.getVersion())) .setModuleDir(project.getProjectDir().toPath()) .setBuildDir(project.getBuildDir().toPath()) .setBuildFile(project.getBuildFile().toPath()); - initProjectModule(project, mainModule, javaConvention.getSourceSets().findByName(SourceSet.MAIN_SOURCE_SET_NAME), + initProjectModule(project, mainModule, sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME), SourceSet.MAIN_SOURCE_SET_NAME, ArtifactSources.MAIN); if (workspaceDiscovery) { - initProjectModule(project, mainModule, javaConvention.getSourceSets().findByName(SourceSet.TEST_SOURCE_SET_NAME), + initProjectModule(project, mainModule, sourceSets.getByName(SourceSet.TEST_SOURCE_SET_NAME), SourceSet.TEST_SOURCE_SET_NAME, ArtifactSources.TEST); } @@ -173,11 +171,12 @@ private void collectExtensionDependencies(Project project, Configuration deploym final ResolvedConfiguration rc = deploymentConfiguration.getResolvedConfiguration(); for (ResolvedArtifact a : rc.getResolvedArtifacts()) { if (a.getId().getComponentIdentifier() instanceof ProjectComponentIdentifier) { - final Project projectDep = project.getRootProject().findProject( - ((ProjectComponentIdentifier) a.getId().getComponentIdentifier()).getProjectPath()); - final JavaPluginConvention javaExtension = projectDep == null ? null - : projectDep.getConvention().findPlugin(JavaPluginConvention.class); - SourceSet mainSourceSet = javaExtension.getSourceSets().getByName(SourceSet.MAIN_SOURCE_SET_NAME); + var componentIdentifier = (ProjectComponentIdentifier) a.getId().getComponentIdentifier(); + final Project projectDep = project.getRootProject().findProject(componentIdentifier.getProjectPath()); + Objects.requireNonNull(projectDep, "project " + componentIdentifier.getProjectPath() + " should exist"); + SourceSetContainer sourceSets = projectDep.getExtensions().getByType(SourceSetContainer.class); + + SourceSet mainSourceSet = sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME); final ResolvedDependencyBuilder dep = appDependencies.computeIfAbsent( toAppDependenciesKey(a.getModuleVersion().getId().getGroup(), a.getName(), a.getClassifier()), k -> toDependency(a, mainSourceSet)); @@ -277,8 +276,8 @@ private void collectDependencies(org.gradle.api.artifacts.ResolvedDependency res final Project projectDep = project.getRootProject().findProject( ((ProjectComponentIdentifier) a.getId().getComponentIdentifier()).getProjectPath()); - final JavaPluginConvention javaExtension = projectDep == null ? null - : projectDep.getConvention().findPlugin(JavaPluginConvention.class); + SourceSetContainer sourceSets = projectDep == null ? null + : projectDep.getExtensions().findByType(SourceSetContainer.class); final String classifier = a.getClassifier(); if (classifier == null || classifier.isEmpty()) { @@ -287,22 +286,22 @@ private void collectDependencies(org.gradle.api.artifacts.ResolvedDependency res final PathList.Builder pathBuilder = PathList.builder(); addSubstitutedProject(pathBuilder, includedBuild.getProjectDir()); paths = pathBuilder.build(); - } else if (javaExtension != null) { + } else if (sourceSets != null) { final PathList.Builder pathBuilder = PathList.builder(); projectModule = initProjectModuleAndBuildPaths(projectDep, a, modelBuilder, depBuilder, - javaExtension, pathBuilder, SourceSet.MAIN_SOURCE_SET_NAME, false); + pathBuilder, SourceSet.MAIN_SOURCE_SET_NAME, false); paths = pathBuilder.build(); } - } else if (javaExtension != null) { + } else if (sourceSets != null) { if ("test".equals(classifier)) { final PathList.Builder pathBuilder = PathList.builder(); projectModule = initProjectModuleAndBuildPaths(projectDep, a, modelBuilder, depBuilder, - javaExtension, pathBuilder, SourceSet.TEST_SOURCE_SET_NAME, true); + pathBuilder, SourceSet.TEST_SOURCE_SET_NAME, true); paths = pathBuilder.build(); } else if ("test-fixtures".equals(classifier)) { final PathList.Builder pathBuilder = PathList.builder(); projectModule = initProjectModuleAndBuildPaths(projectDep, a, modelBuilder, depBuilder, - javaExtension, pathBuilder, "testFixtures", true); + pathBuilder, "testFixtures", true); paths = pathBuilder.build(); } } @@ -343,7 +342,7 @@ private static String toNonNullClassifier(String resolvedClassifier) { private WorkspaceModule.Mutable initProjectModuleAndBuildPaths(final Project project, ResolvedArtifact resolvedArtifact, ApplicationModelBuilder appModel, final ResolvedDependencyBuilder appDep, - final JavaPluginConvention javaExt, PathList.Builder buildPaths, String sourceName, boolean test) { + PathList.Builder buildPaths, String sourceName, boolean test) { appDep.setWorkspaceModule().setReloadable(); @@ -355,7 +354,8 @@ private WorkspaceModule.Mutable initProjectModuleAndBuildPaths(final Project pro .setBuildFile(project.getBuildFile().toPath()); final String classifier = toNonNullClassifier(resolvedArtifact.getClassifier()); - initProjectModule(project, projectModule, javaExt.getSourceSets().findByName(sourceName), sourceName, classifier); + SourceSetContainer sourceSets = project.getExtensions().getByType(SourceSetContainer.class); + initProjectModule(project, projectModule, sourceSets.findByName(sourceName), sourceName, classifier); collectDestinationDirs(projectModule.getSources(classifier).getSourceDirs(), buildPaths); collectDestinationDirs(projectModule.getSources(classifier).getResourceDirs(), buildPaths); diff --git a/devtools/gradle/gradle-model/src/main/java/io/quarkus/gradle/tooling/dependency/DependencyUtils.java b/devtools/gradle/gradle-model/src/main/java/io/quarkus/gradle/tooling/dependency/DependencyUtils.java index c643889fe82cff..5d0f37b1793e3c 100644 --- a/devtools/gradle/gradle-model/src/main/java/io/quarkus/gradle/tooling/dependency/DependencyUtils.java +++ b/devtools/gradle/gradle-model/src/main/java/io/quarkus/gradle/tooling/dependency/DependencyUtils.java @@ -22,8 +22,8 @@ import org.gradle.api.artifacts.component.ProjectComponentIdentifier; import org.gradle.api.artifacts.dsl.DependencyHandler; import org.gradle.api.capabilities.Capability; -import org.gradle.api.plugins.JavaPluginConvention; import org.gradle.api.tasks.SourceSet; +import org.gradle.api.tasks.SourceSetContainer; import io.quarkus.bootstrap.BootstrapConstants; import io.quarkus.bootstrap.util.BootstrapUtils; @@ -80,10 +80,10 @@ public static ExtensionDependency getExtensionInfoOrNull(Project project, Resolv if (artifact.getId().getComponentIdentifier() instanceof ProjectComponentIdentifier) { final Project projectDep = project.getRootProject().findProject( ((ProjectComponentIdentifier) artifact.getId().getComponentIdentifier()).getProjectPath()); - final JavaPluginConvention javaExtension = projectDep == null ? null - : projectDep.getConvention().findPlugin(JavaPluginConvention.class); - if (javaExtension != null) { - SourceSet mainSourceSet = javaExtension.getSourceSets().getByName(SourceSet.MAIN_SOURCE_SET_NAME); + SourceSetContainer sourceSets = projectDep == null ? null + : projectDep.getExtensions().findByType(SourceSetContainer.class); + if (sourceSets != null) { + SourceSet mainSourceSet = sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME); File resourcesDir = mainSourceSet.getOutput().getResourcesDir(); Path descriptorPath = resourcesDir.toPath().resolve(BootstrapConstants.DESCRIPTOR_PATH); if (Files.exists(descriptorPath)) {