Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/main/java/org/sonarqube/gradle/ScanProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,44 @@
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/

package org.sonarqube.gradle;

public final class ScanProperties {
public static final String SKIP = "sonar.skip";
public static final String PROJECT_SOURCE_DIRS = "sonar.sources";
public static final String PROJECT_TEST_DIRS = "sonar.tests";
public static final String LIBRARIES = "sonar.libraries";

public static final String GRADLE_CACHE = "sonar.gradle.cache";


/**
* Should the given property be excluded from the gradle cache input?
*/
public static boolean excludePropertyFromCache(String key) {

// Will be covered by input files
if (key.endsWith(".libraries") || key.endsWith(".binaries")) {
return true;
}

switch (key) {
case "sonar.kotlin.gradleProjectRoot":
case "sonar.projectBaseDir":
case "sonar.working.directory":
case "sonar.java.jdkHome":

// Shall be included in the input files
case ScanProperties.PROJECT_SOURCE_DIRS:
case ScanProperties.PROJECT_TEST_DIRS:
return true;
default:
return false;
}
}


private ScanProperties() {
/* This is a utility class with constants */
}
Expand Down
67 changes: 65 additions & 2 deletions src/main/java/org/sonarqube/gradle/SonarTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,25 @@
package org.sonarqube.gradle;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.gradle.api.internal.ConventionTask;
import org.gradle.api.logging.LogLevel;
import org.gradle.api.logging.Logger;
import org.gradle.api.logging.Logging;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.PathSensitive;
import org.gradle.api.tasks.PathSensitivity;
import org.gradle.api.tasks.TaskAction;
import org.gradle.util.GradleVersion;
import org.sonarsource.scanner.lib.ScannerEngineBootstrapResult;
Expand Down Expand Up @@ -189,13 +194,71 @@ private static boolean isSkippedWithProperty(Map<String, String> properties) {
* @return The String key/value pairs to be passed to the SonarQube Scanner.
* {@code null} values are not permitted.
*/
@Input
@Internal
public Provider<Map<String, String>> getProperties() {
return properties;
}

void setProperties(Provider<Map<String, String>> properties) {
this.properties = properties;

getOutputs().cacheIf(t -> properties.map(this::isCacheEnabled).get());
getOutputs().file("build/sonar/report-task.txt");

// Remove properties that should be excluded from the Gradle build cache
getInputs().property("properties", properties.map(props -> {
Map<String, String> filtered = new HashMap<>();
for (Map.Entry<String, String> entry : props.entrySet()) {
if (!ScanProperties.excludePropertyFromCache(entry.getKey())) {
filtered.put(entry.getKey(), entry.getValue());
}
}
return filtered;
}));
}


@InputFiles
@PathSensitive(PathSensitivity.RELATIVE)
public Provider<List<File>> getInputFiles () {
return properties.map(props -> {
ArrayList<File> files = new ArrayList<>();
files.addAll(parseInputs(props, ScanProperties.PROJECT_SOURCE_DIRS));
files.addAll(parseInputs(props, ScanProperties.LIBRARIES));
for (String key : props.keySet()) {
if (key.endsWith(".reportPaths")) {
files.addAll(parseInputs(props, key));
}
}
return files;
});
}

private List<File> parseInputs(Map<String, String> props, String key) {
String sources = props.get(key);
if (sources == null) {
return List.of();
}
ArrayList<File> files = new ArrayList<>();
for (String source : sources.split(",")) {
if (source.isEmpty()) {
continue;
}

File file = getProject().file(source);
if(file.isDirectory()) {
files.addAll(getProject().fileTree(file).getFiles());
} else {
files.add(file);
}
}
return files;
}

private boolean isCacheEnabled(Map<String, String> properties) {
return properties
.getOrDefault(ScanProperties.GRADLE_CACHE, "true")
.equals("true");
}

/**
Expand Down