Skip to content

Commit 928532a

Browse files
SCANGRADLE-324 Centralize Sonar property names in ScanPropertyNames (#405)
Centralize Sonar property names in ScanProperties Rename ScanProperties to ScanPropertyNames Use ScanPropertyNames constants in SonarTask Add VERBOSE constant to ScanPropertyNames Add Javadoc to ScanPropertyNames class Improve documentation of ProjectProperties and SonarProperties Co-authored-by: Claude <[email protected]>
1 parent 8a73072 commit 928532a

File tree

8 files changed

+221
-90
lines changed

8 files changed

+221
-90
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
*/
7777
class AndroidUtils {
7878
private static final Logger LOGGER = Logging.getLogger(AndroidUtils.class);
79-
private static final String SONAR_ANDROID_LINT_REPORT_PATHS_PROP = "sonar.androidLint.reportPaths";
79+
private static final String SONAR_ANDROID_LINT_REPORT_PATHS_PROP = ScanPropertyNames.ANDROID_LINT_REPORT_PATHS;
8080

8181
private AndroidUtils() {
8282
}
@@ -202,7 +202,7 @@ private static void configureTestReports(Project project, BaseVariant variant, M
202202
.map(d -> d.get().getAsFile())
203203
.filter(file -> file.isDirectory() && file.list().length > 0)
204204
.collect(Collectors.toList());
205-
map.put("sonar.junit.reportPaths", value);
205+
map.put(ScanPropertyNames.JUNIT_REPORT_PATHS, value);
206206
}
207207

208208
private static DirectoryProperty getReportsDirBeforeGradle42(DeviceProviderInstrumentTestTask testTask) {
@@ -353,11 +353,11 @@ private static void populateSonarQubeProps(Map<String, Object> properties, BaseV
353353
: Collections.emptySet();
354354

355355
if (isTest) {
356-
appendProps(properties, "sonar.java.test.binaries", exists(destinationDirs));
356+
appendProps(properties, ScanPropertyNames.JAVA_TEST_BINARIES, exists(destinationDirs));
357357
} else {
358-
appendProps(properties, "sonar.java.binaries", exists(destinationDirs));
358+
appendProps(properties, ScanPropertyNames.JAVA_BINARIES, exists(destinationDirs));
359359
// Populate deprecated properties for backward compatibility
360-
appendProps(properties, "sonar.binaries", exists(destinationDirs));
360+
appendProps(properties, ScanPropertyNames.BINARIES, exists(destinationDirs));
361361
}
362362
}
363363

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,55 @@
2121

2222
import java.util.List;
2323

24+
/**
25+
* An immutable data transfer object that holds resolved dependency information for a Gradle project.
26+
* <p>
27+
* This class is used during the Gradle <strong>execution phase</strong> to transfer resolved classpath
28+
* information between tasks, specifically from {@link SonarResolverTask} to {@link SonarTask}.
29+
* It is an <strong>internal implementation detail</strong> and is not exposed to users.
30+
*
31+
* <p>
32+
* <strong>Purpose:</strong>
33+
* <ul>
34+
* <li>Carries resolved compile and test classpaths after dependency resolution</li>
35+
* <li>Supports Gradle configuration cache by being serializable/deserializable</li>
36+
* <li>Ensures immutability for safe sharing between tasks</li>
37+
* </ul>
38+
*
39+
* <p>
40+
* <strong>Note:</strong> This class is for internal task-to-task communication, not for user configuration.
41+
* For user-facing property configuration, see {@link SonarProperties}.
42+
*/
2443
public class ProjectProperties {
2544

45+
/** The Gradle project name (e.g., ":subproject" for subprojects, "" for root) */
2646
public final String projectName;
47+
48+
/** Whether this project is the root project of the analysis */
2749
public final Boolean isRootProject;
50+
51+
/** Resolved absolute paths of compile classpath dependencies */
2852
public final List<String> compileClasspath;
53+
54+
/** Resolved absolute paths of test compile classpath dependencies */
2955
public final List<String> testCompileClasspath;
56+
57+
/** Filtered main libraries (subset of compileClasspath) for SonarQube analysis */
3058
public final List<String> mainLibraries;
59+
60+
/** Filtered test libraries (subset of testCompileClasspath) for SonarQube analysis */
3161
public final List<String> testLibraries;
3262

63+
/**
64+
* Creates a new immutable ProjectProperties instance.
65+
*
66+
* @param projectName the Gradle project name
67+
* @param isRootProject whether this is the root project
68+
* @param compileClasspath resolved compile classpath as absolute paths
69+
* @param testCompileClasspath resolved test compile classpath as absolute paths
70+
* @param mainLibraries filtered main libraries for analysis
71+
* @param testLibraries filtered test libraries for analysis
72+
*/
3373
public ProjectProperties(String projectName, Boolean isRootProject, List<String> compileClasspath, List<String> testCompileClasspath,
3474
List<String> mainLibraries, List<String> testLibraries) {
3575
this.projectName = projectName;

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

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* SonarQube Scanner for Gradle
3+
* Copyright (C) 2015-2025 SonarSource
4+
* mailto:info AT sonarsource DOT com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public
8+
* License as published by the Free Software Foundation; either
9+
* version 3 of the License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public
17+
* License along with this program; if not, write to the Free Software
18+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
19+
*/
20+
package org.sonarqube.gradle;
21+
22+
/**
23+
* Constants for SonarQube scan property names.
24+
*/
25+
public final class ScanPropertyNames {
26+
// General
27+
public static final String SKIP = "sonar.skip";
28+
public static final String GRADLE_SCAN_ALL = "sonar.gradle.scanAll";
29+
public static final String VERBOSE = "sonar.verbose";
30+
31+
// Project structure
32+
public static final String PROJECT_KEY = "sonar.projectKey";
33+
public static final String MODULE_KEY = "sonar.moduleKey";
34+
public static final String MODULES = "sonar.modules";
35+
public static final String PROJECT_NAME = "sonar.projectName";
36+
public static final String PROJECT_DESCRIPTION = "sonar.projectDescription";
37+
public static final String PROJECT_VERSION = "sonar.projectVersion";
38+
public static final String PROJECT_BASE_DIR = "sonar.projectBaseDir";
39+
public static final String WORKING_DIRECTORY = "sonar.working.directory";
40+
41+
// Sources and tests
42+
public static final String PROJECT_SOURCE_DIRS = "sonar.sources";
43+
public static final String PROJECT_TEST_DIRS = "sonar.tests";
44+
public static final String SOURCE_ENCODING = "sonar.sourceEncoding";
45+
46+
// Java configuration
47+
public static final String JAVA_SOURCE = "sonar.java.source";
48+
public static final String JAVA_TARGET = "sonar.java.target";
49+
public static final String JAVA_ENABLE_PREVIEW = "sonar.java.enablePreview";
50+
public static final String JAVA_JDK_HOME = "sonar.java.jdkHome";
51+
public static final String JAVA_BINARIES = "sonar.java.binaries";
52+
public static final String JAVA_LIBRARIES = "sonar.java.libraries";
53+
public static final String JAVA_TEST_BINARIES = "sonar.java.test.binaries";
54+
public static final String JAVA_TEST_LIBRARIES = "sonar.java.test.libraries";
55+
56+
/** Kept for backward compatibility */
57+
public static final String LIBRARIES = "sonar.libraries";
58+
59+
// Groovy configuration
60+
public static final String GROOVY_BINARIES = "sonar.groovy.binaries";
61+
62+
// Kotlin configuration
63+
public static final String KOTLIN_GRADLE_PROJECT_ROOT = "sonar.kotlin.gradleProjectRoot";
64+
65+
// Test reports
66+
public static final String JUNIT_REPORT_PATHS = "sonar.junit.reportPaths";
67+
68+
/** Kept for backward compatibility */
69+
public static final String JUNIT_REPORTS_PATH = "sonar.junit.reportsPath";
70+
71+
/** Kept for backward compatibility */
72+
public static final String SUREFIRE_REPORTS_PATH = "sonar.surefire.reportsPath";
73+
74+
// Coverage reports
75+
public static final String JACOCO_XML_REPORT_PATHS = "sonar.coverage.jacoco.xmlReportPaths";
76+
77+
// Android
78+
public static final String ANDROID_LINT_REPORT_PATHS = "sonar.androidLint.reportPaths";
79+
80+
/** Kept for backward compatibility */
81+
public static final String BINARIES = "sonar.binaries";
82+
83+
private ScanPropertyNames() {
84+
/* This is a utility class with constants */
85+
}
86+
}

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

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,42 @@
2222
import java.util.Map;
2323

2424
/**
25-
* The Sonar properties for the current Gradle project that are to be passed to the Scanner.
25+
* A mutable wrapper for user-facing SonarQube property configuration in Gradle build scripts.
2626
* <p>
27-
* The {@code properties} map is already populated with the defaults provided by Gradle, and can be further manipulated as necessary.
28-
* Before passing them on to the Scanner, property values are converted to Strings as follows:
27+
* This class is part of the public API and is used during the Gradle <strong>configuration phase</strong>
28+
* to allow users to configure SonarQube analysis properties via the {@code sonarqube { }} DSL block.
29+
* <p>
30+
* The wrapped {@code properties} map is pre-populated with defaults computed from the Gradle project model,
31+
* and can be further manipulated by users through the convenience methods provided.
32+
*
33+
* <p>
34+
* <strong>Usage Example:</strong>
35+
* <pre>{@code
36+
* sonarqube {
37+
* properties {
38+
* property "sonar.projectKey", "my-project"
39+
* properties([
40+
* "sonar.sources": "src",
41+
* "sonar.tests": "test"
42+
* ])
43+
* }
44+
* }
45+
* }</pre>
46+
*
47+
* <p>
48+
* <strong>Property Value Conversion:</strong>
49+
* Before passing properties to the Scanner, values are converted to Strings as follows:
2950
* <ul>
30-
* <li>{@code Iterable}s are recursively converted and joined into a comma-separated String.</li>
31-
* <li>All other values are converted to Strings by calling their {@code toString()} method.</li>
51+
* <li>{@code Iterable}s are recursively converted and joined into a comma-separated String</li>
52+
* <li>All other values are converted to Strings by calling their {@code toString()} method</li>
3253
* </ul>
54+
*
55+
* <p>
56+
* <strong>Note:</strong> This class is used for configuration, not for transferring resolved dependency information.
57+
* For that purpose, see {@link ProjectProperties}.
58+
*
59+
* @see SonarExtension
60+
* @see ProjectProperties
3361
*/
3462
public class SonarProperties {
3563

0 commit comments

Comments
 (0)