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 8a42717850df9..1cac66c5e7342 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 @@ -4,7 +4,6 @@ import java.nio.file.Path; import java.util.HashSet; import java.util.Set; -import java.util.function.Consumer; import java.util.stream.Collectors; import javax.inject.Inject; @@ -21,7 +20,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; @@ -166,24 +164,9 @@ private void registerTasks(Project project, QuarkusPluginExtension quarkusExt) { configureBuildNativeTask(project); - final Consumer configureTestTask = t -> { - // Quarkus test configuration action which should be executed before any Quarkus test - // Use anonymous classes in order to leverage task avoidance. - t.doFirst(new Action() { - @Override - public void execute(Task test) { - quarkusExt.beforeTest(t); - } - }); - // also make each task use the JUnit platform since it's the only supported test environment - t.useJUnitPlatform(); - // quarkusBuild is expected to run after the project has passed the tests - quarkusBuild.configure(task -> task.shouldRunAfter(t)); - }; - project.getPlugins().withType( BasePlugin.class, - basePlugin -> tasks.getByName(BasePlugin.ASSEMBLE_TASK_NAME).dependsOn(quarkusBuild)); + basePlugin -> tasks.named(BasePlugin.ASSEMBLE_TASK_NAME, task -> task.dependsOn(quarkusBuild))); project.getPlugins().withType( JavaPlugin.class, javaPlugin -> { @@ -245,8 +228,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); @@ -295,8 +277,20 @@ public void execute(Task test) { testNative.setClasspath(nativeTestSourceSet.getRuntimeClasspath()); }); - tasks.withType(Test.class).forEach(configureTestTask); - tasks.withType(Test.class).whenTaskAdded(configureTestTask::accept); + tasks.withType(Test.class).configureEach(t -> { + // Quarkus test configuration action which should be executed before any Quarkus test + // Use anonymous classes in order to leverage task avoidance. + t.doFirst(new Action() { + @Override + public void execute(Task task) { + quarkusExt.beforeTest(t); + } + }); + // also make each task use the JUnit platform since it's the only supported test environment + t.useJUnitPlatform(); + }); + // quarkusBuild is expected to run after the project has passed the tests + quarkusBuild.configure(task -> task.shouldRunAfter(tasks.withType(Test.class))); SourceSet generatedSourceSet = sourceSets.create(QuarkusGenerateCode.QUARKUS_GENERATED_SOURCES); SourceSet generatedTestSourceSet = sourceSets.create(QuarkusGenerateCode.QUARKUS_TEST_GENERATED_SOURCES); @@ -384,8 +378,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 8a3889f0424f0..3396e25626cac 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 c180773de6c51..fc38292cf7498 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 9c0d267119c66..28353026924ee 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); @@ -65,7 +65,7 @@ private void registerTasks(Project project, QuarkusExtensionConfiguration quarku javaPlugin -> { tasks.named(JavaPlugin.PROCESS_RESOURCES_TASK_NAME, task -> task.finalizedBy(extensionDescriptorTask)); tasks.named(JavaPlugin.COMPILE_JAVA_TASK_NAME, task -> task.dependsOn(extensionDescriptorTask)); - tasks.withType(Test.class, test -> test.useJUnitPlatform()); + tasks.withType(Test.class).configureEach(Test::useJUnitPlatform); addAnnotationProcessorDependency(project); }); @@ -84,7 +84,7 @@ private void registerTasks(Project project, QuarkusExtensionConfiguration quarku task.setDeploymentModuleClasspath(deploymentModuleClasspath); }); - deploymentProject.getTasks().withType(Test.class, test -> { + deploymentProject.getTasks().withType(Test.class).configureEach(test -> { test.useJUnitPlatform(); test.doFirst(task -> { final Map props = test.getSystemProperties(); 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 6bfd7177c63af..ccebec9c535f9 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 c643889fe82cf..5d0f37b1793e3 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)) {