Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ static void assertThatPluginDoesNotCreateGradleDeprecationWarnings(String text)
}

static boolean isUnexpectedWarning(String line){
if(line.contains("SonarResolverTask.java:143") || line.contains("SonarResolverTask.java:146")){
if(line.contains("SonarResolverTask.java:108") || line.contains("SonarResolverTask.java:111")){
// These warnings are expected until we properly support Gradle 9
return false;
}
Expand Down

This file was deleted.

32 changes: 16 additions & 16 deletions src/main/java/org/sonarqube/gradle/SonarQubePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,13 @@
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.provider.MapProperty;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.TaskContainer;
import org.gradle.testing.jacoco.plugins.JacocoPlugin;
import org.gradle.testing.jacoco.tasks.JacocoReport;
import org.gradle.util.GradleVersion;

import static org.sonarqube.gradle.SonarUtils.capitalize;
import static org.sonarqube.gradle.SonarUtils.getMainClassPath;
import static org.sonarqube.gradle.SonarUtils.getTestClassPath;
import static org.sonarqube.gradle.SonarUtils.isAndroidProject;

/**
Expand Down Expand Up @@ -108,7 +107,7 @@ public void apply(Project project) {
private static List<File> registerAndConfigureResolverTasks(Project topLevelProject) {
List<File> resolverFiles = new ArrayList<>();
topLevelProject.getAllprojects().forEach(target ->
target.getTasks().register(SonarResolverTask.TASK_NAME, getCompatibleTaskType(GradleVersion.current()), task -> {
target.getTasks().register(SonarResolverTask.TASK_NAME, SonarResolverTask.class, task -> {
Provider<Boolean> skipProject = target.provider(() -> isSkipped(target));

task.setDescription(SonarResolverTask.TASK_DESCRIPTION);
Expand All @@ -119,10 +118,10 @@ private static List<File> registerAndConfigureResolverTasks(Project topLevelProj
}
task.setProjectName(SonarUtils.constructPrefixedProjectName(target.getPath()));

FileCollection mainClassPath = getMainClassPath(target);
task.setCompileClasspath(mainClassPath);
FileCollection testClassPath = getTestClassPath(target);
task.setTestCompileClasspath(testClassPath);
Provider<FileCollection> compile = target.provider(() -> querySourceSet(target, SourceSet.MAIN_SOURCE_SET_NAME));
Provider<FileCollection> test = target.provider(() -> querySourceSet(target, SourceSet.TEST_SOURCE_SET_NAME));
task.setCompileClasspath(compile);
task.setTestCompileClasspath(test);

if (isAndroidProject(target)) {
setAndroidLibrariesProperties(target, task);
Expand All @@ -140,6 +139,16 @@ private static List<File> registerAndConfigureResolverTasks(Project topLevelProj
return resolverFiles;
}

@Nullable
private static FileCollection querySourceSet(Project project, String sourceSetName) {
var sourceSets = SonarUtils.getSourceSets(project);
if (sourceSets == null) {
return null;
}
var set = sourceSets.findByName(sourceSetName);
return set == null ? null : set.getCompileClasspath();
}

private static void setAndroidLibrariesProperties(Project target, SonarResolverTask task) {
AndroidUtils.LibrariesAndTestLibraries libraries = AndroidUtils.LibrariesAndTestLibraries.ofProject(target);
task.setMainLibraries(libraries.getMainLibraries());
Expand All @@ -153,15 +162,6 @@ private static void setJavaLibrariesProperties(Project target, SonarResolverTask
task.setTestLibraries(libraries);
}

private static Class<? extends SonarResolverTask> getCompatibleTaskType(GradleVersion version) {
if (version.compareTo(GradleVersion.version("8.5.0")) >= 0) {
return BuildFeaturesEnabledResolverTask.class;
} else if (version.compareTo(GradleVersion.version("7.6.1")) >= 0) {
return StartParameterBasedTask.class;
}
return SonarResolverTask.class;
}

private static void addExtensions(Project project, String name, Map<String, ActionBroadcast<SonarProperties>> actionBroadcastMap) {
project.getAllprojects().forEach(p -> {
LOGGER.debug("Adding " + name + " extension to " + p);
Expand Down
33 changes: 12 additions & 21 deletions src/main/java/org/sonarqube/gradle/SonarResolverTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ public abstract class SonarResolverTask extends DefaultTask {

private String projectName;
private boolean isTopLevelProject;
private FileCollection compileClasspath;
private FileCollection testCompileClasspath;
private FileCollection mainLibraries;
private FileCollection testLibraries;
private Provider<FileCollection> compileClasspath;
private Provider<FileCollection> testCompileClasspath;
private File outputDirectory;
private Provider<Boolean> skipProject;

Expand All @@ -71,19 +71,19 @@ public void setTopLevelProject(boolean topLevelProject) {

@Internal
FileCollection getCompileClasspath() {
return this.compileClasspath;
return this.compileClasspath.get();
}

public void setCompileClasspath(FileCollection compileClasspath) {
public void setCompileClasspath(Provider<FileCollection> compileClasspath) {
this.compileClasspath = compileClasspath;
}

@Internal
FileCollection getTestCompileClasspath() {
Provider<FileCollection> getTestCompileClasspath() {
return this.testCompileClasspath;
}

public void setTestCompileClasspath(FileCollection testCompileClasspath) {
public void setTestCompileClasspath(Provider<FileCollection> testCompileClasspath) {
this.testCompileClasspath = testCompileClasspath;
}

Expand Down Expand Up @@ -137,23 +137,18 @@ void run() throws IOException {
LOGGER.info("Resolving properties for " + displayName + ".");
}

// If we failed to initialize class paths at configuration time AND the configuration cache is not active/requested,
// we attempt to rebuild them using the source sets.
if (compileClasspath == null && configurationCacheIsDisabled()) {
compileClasspath = SonarUtils.getMainClassPath(getProject());
}
if (testCompileClasspath == null && configurationCacheIsDisabled()) {
testCompileClasspath = SonarUtils.getTestClassPath(getProject());
}
var mainClasspath = this.compileClasspath.getOrNull();
var testClasspath = testCompileClasspath.getOrNull();

List<String> compileClasspathFilenames = SonarUtils.exists(compileClasspath == null ? Collections.emptyList() : compileClasspath)
List<String> compileClasspathFilenames = SonarUtils.exists(mainClasspath == null ? Collections.emptyList() : mainClasspath)
.stream()
.map(File::getAbsolutePath)
.collect(Collectors.toList());
List<String> testCompileClasspathFilenames = SonarUtils.exists(testCompileClasspath == null ? Collections.emptyList() : testCompileClasspath)
List<String> testCompileClasspathFilenames = SonarUtils.exists(testClasspath == null ? Collections.emptyList() : testClasspath)
.stream()
.map(File::getAbsolutePath)
.collect(Collectors.toList());

List<String> mainLibrariesFilenames = SonarUtils.exists(getMainLibraries() == null ? Collections.emptyList() : getMainLibraries())
.stream()
.map(File::getAbsolutePath)
Expand All @@ -162,6 +157,7 @@ void run() throws IOException {
.stream()
.map(File::getAbsolutePath)
.collect(Collectors.toList());

ProjectProperties projectProperties = new ProjectProperties(
getProjectName(),
isTopLevelProject(),
Expand All @@ -179,9 +175,4 @@ void run() throws IOException {
LOGGER.info("Resolved properties for " + displayName + " and wrote them to " + getOutputFile() + ".");
}
}

public boolean configurationCacheIsDisabled() {
return true;
}

}
34 changes: 1 addition & 33 deletions src/main/java/org/sonarqube/gradle/SonarUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,9 @@
import java.util.stream.StreamSupport;
import javax.annotation.Nullable;
import org.gradle.api.Project;
import org.gradle.api.file.FileCollection;
import org.gradle.api.internal.plugins.DslObject;
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.util.GradleVersion;

Expand Down Expand Up @@ -96,6 +94,7 @@ static boolean isAndroidProject(Project project) {
* @param project The (sub-)project under analysis
* @return A container with the "main" and "test" source sets
*/
@Nullable
static SourceSetContainer getSourceSets(Project project) {
GradleVersion gradleVersion = GradleVersion.version(project.getGradle().getGradleVersion());
if (isCompatibleWithJavaPluginExtension(gradleVersion)) {
Expand Down Expand Up @@ -123,37 +122,6 @@ private static SourceSetContainer getSourceSetsGradleLegacy(Project project) {
return javaPluginConvention.getSourceSets();
}

@Nullable
public static FileCollection getMainClassPath(Project project) {
return getClassPath(project, "main");
}

@Nullable
public static FileCollection getTestClassPath(Project project) {
return getClassPath(project, "test");
}

@Nullable
public static FileCollection getClassPath(Project project, String sourceSetName) {
SourceSetContainer sourceSets = SonarUtils.getSourceSets(project);
return getClassPathFromSourceSets(sourceSetName, sourceSets);
}

public static @Nullable FileCollection getClassPathFromSourceSets(String sourceSetName, @Nullable SourceSetContainer sourceSets) {
if (sourceSets == null) {
return null;
}
SourceSet sourceSet = sourceSets.findByName(sourceSetName);
if (sourceSet == null) {
return null;
}
FileCollection compileClasspath = sourceSet.getCompileClasspath();
if (compileClasspath == null) {
return null;
}
return compileClasspath;
}

static boolean isCompatibleWithJavaPluginExtension(GradleVersion version) {
return version.getBaseVersion().compareTo(GradleVersion.version("7.0")) >= 0;
}
Expand Down
31 changes: 0 additions & 31 deletions src/main/java/org/sonarqube/gradle/StartParameterBasedTask.java

This file was deleted.

16 changes: 0 additions & 16 deletions src/test/java/org/sonarqube/gradle/SonarUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,14 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.SourceSetContainer;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.when;

class SonarUtilsTest {

@Test
void test_get_class_path_from_sourcesets(){
SourceSetContainer sourceSetsMissingMain = Mockito.mock(SourceSetContainer.class);
when(sourceSetsMissingMain.findByName("main")).thenReturn(null);
assertThat(SonarUtils.getClassPathFromSourceSets("main", sourceSetsMissingMain)).isNull();

SourceSetContainer sourceSets = Mockito.mock(SourceSetContainer.class);
SourceSet sourceSet = Mockito.mock(SourceSet.class);
when(sourceSets.findByName("main")).thenReturn(sourceSet);
when(sourceSet.getCompileClasspath()).thenReturn(null);
assertThat(SonarUtils.getClassPathFromSourceSets("main", sourceSets)).isNull();
}

@Test
void test_construct_prefixed_project_name() {
assertThat(SonarUtils.constructPrefixedProjectName(":module:submodule")).isEqualTo(":module.:module:submodule");
Expand Down
Loading