Skip to content

Commit

Permalink
Get SourceSetContainer from extensions instead of legacy JavaPluginCo…
Browse files Browse the repository at this point in the history
…nvention

Signed-off-by: Konstantin Gribov <[email protected]>
  • Loading branch information
grossws committed Jul 9, 2022
1 parent 60fd34f commit ddd1ee2
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Path> 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<Method> 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<Path> 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<Method> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@

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;

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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()) {
Expand All @@ -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();
}
}
Expand Down Expand Up @@ -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();

Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand Down

0 comments on commit ddd1ee2

Please sign in to comment.