Skip to content

Commit 726aa63

Browse files
[GVC-17314] Fix build inputs
1 parent d887306 commit 726aa63

File tree

2 files changed

+94
-2
lines changed

2 files changed

+94
-2
lines changed

src/main/java/org/sonarqube/gradle/ScanProperties.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,44 @@
1717
* License along with this program; if not, write to the Free Software
1818
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
1919
*/
20+
2021
package org.sonarqube.gradle;
2122

2223
public final class ScanProperties {
2324
public static final String SKIP = "sonar.skip";
2425
public static final String PROJECT_SOURCE_DIRS = "sonar.sources";
2526
public static final String PROJECT_TEST_DIRS = "sonar.tests";
27+
public static final String LIBRARIES = "sonar.libraries";
28+
29+
public static final String GRADLE_CACHE = "sonar.gradle.cache";
30+
31+
32+
/**
33+
* Should the given property be excluded from the gradle cache input?
34+
*/
35+
public static boolean excludePropertyFromCache(String key) {
36+
37+
// Will be covered by input files
38+
if (key.endsWith(".libraries") || key.endsWith(".binaries")) {
39+
return true;
40+
}
41+
42+
switch (key) {
43+
case "sonar.kotlin.gradleProjectRoot":
44+
case "sonar.projectBaseDir":
45+
case "sonar.working.directory":
46+
case "sonar.java.jdkHome":
47+
48+
// Shall be included in the input files
49+
case ScanProperties.PROJECT_SOURCE_DIRS:
50+
case ScanProperties.PROJECT_TEST_DIRS:
51+
return true;
52+
default:
53+
return false;
54+
}
55+
}
56+
57+
2658
private ScanProperties() {
2759
/* This is a utility class with constants */
2860
}

src/main/java/org/sonarqube/gradle/SonarTask.java

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,22 @@
2020
package org.sonarqube.gradle;
2121

2222
import java.io.BufferedReader;
23+
import java.io.File;
2324
import java.io.IOException;
2425
import java.io.InputStream;
2526
import java.io.InputStreamReader;
2627
import java.nio.charset.StandardCharsets;
28+
import java.util.ArrayList;
2729
import java.util.Collections;
2830
import java.util.HashMap;
31+
import java.util.List;
2932
import java.util.Map;
3033
import org.gradle.api.internal.ConventionTask;
3134
import org.gradle.api.logging.LogLevel;
3235
import org.gradle.api.logging.Logger;
3336
import org.gradle.api.logging.Logging;
3437
import org.gradle.api.provider.Provider;
35-
import org.gradle.api.tasks.Input;
38+
import org.gradle.api.tasks.InputFiles;
3639
import org.gradle.api.tasks.Internal;
3740
import org.gradle.api.tasks.TaskAction;
3841
import org.gradle.util.GradleVersion;
@@ -189,13 +192,70 @@ private static boolean isSkippedWithProperty(Map<String, String> properties) {
189192
* @return The String key/value pairs to be passed to the SonarQube Scanner.
190193
* {@code null} values are not permitted.
191194
*/
192-
@Input
195+
@Internal
193196
public Provider<Map<String, String>> getProperties() {
194197
return properties;
195198
}
196199

197200
void setProperties(Provider<Map<String, String>> properties) {
198201
this.properties = properties;
202+
203+
getOutputs().cacheIf(t -> properties.map(this::isCacheEnabled).get());
204+
getOutputs().file("build/sonar/report-task.txt");
205+
206+
// Remove properties that should be excluded from the Gradle build cache
207+
getInputs().property("properties", properties.map(props -> {
208+
Map<String, String> filtered = new HashMap<>();
209+
for (Map.Entry<String, String> entry : props.entrySet()) {
210+
if (!ScanProperties.excludePropertyFromCache(entry.getKey())) {
211+
filtered.put(entry.getKey(), entry.getValue());
212+
}
213+
}
214+
return filtered;
215+
}));
216+
}
217+
218+
219+
@InputFiles
220+
public Provider<List<File>> getInputFiles () {
221+
return properties.map(props -> {
222+
ArrayList<File> files = new ArrayList<>();
223+
files.addAll(parseInputs(props, ScanProperties.PROJECT_SOURCE_DIRS));
224+
files.addAll(parseInputs(props, ScanProperties.LIBRARIES));
225+
for (String key : props.keySet()) {
226+
if (key.endsWith(".reportPaths")) {
227+
files.addAll(parseInputs(props, key));
228+
}
229+
}
230+
return files;
231+
});
232+
}
233+
234+
private List<File> parseInputs(Map<String, String> props, String key) {
235+
String sources = props.get(key);
236+
if (sources == null) {
237+
return List.of();
238+
}
239+
ArrayList<File> files = new ArrayList<>();
240+
for (String source : sources.split(",")) {
241+
if (source.isEmpty()) {
242+
continue;
243+
}
244+
245+
File file = new File(source);
246+
if(file.isDirectory()) {
247+
files.addAll(getProject().fileTree(file).getFiles());
248+
} else {
249+
files.add(file);
250+
}
251+
}
252+
return files;
253+
}
254+
255+
private boolean isCacheEnabled(Map<String, String> properties) {
256+
return properties
257+
.getOrDefault(ScanProperties.GRADLE_CACHE, "true")
258+
.equals("true");
199259
}
200260

201261
/**

0 commit comments

Comments
 (0)