Skip to content

Commit

Permalink
SCANGRADLE-170 Upgrade sonar-scanner-java-library to version 3.1.1.261
Browse files Browse the repository at this point in the history
This library upgrade brings some changes
* Incompatibilites with Gradle 8.1 and 8.2
* Default to https://sonarcloud.io as sonar.host
  • Loading branch information
dorian-burihabwa-sonarsource committed Nov 4, 2024
1 parent aae606f commit def8e0b
Show file tree
Hide file tree
Showing 11 changed files with 157 additions and 57 deletions.
15 changes: 10 additions & 5 deletions .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,19 @@ gradle_qa_task:
eks_container:
<<: *CONTAINER_DEFINITION_17
cpu: 2
memory: 2G
memory: 4G
env:
matrix:
- GRADLE_VERSION: 7.5.1
ANDROID_GRADLE_VERSION: 7.1.0
- GRADLE_VERSION: 8.2.1
ANDROID_GRADLE_VERSION: 8.1.1
# TODO The combination of AGP 8.3.0 and Gradle 8.3.0 should be restored as part of SCANGRADLE-162
#- GRADLE_VERSION: 8.4
# ANDROID_GRADLE_VERSION: 8.3.0
- GRADLE_VERSION: 8.10
- GRADLE_VERSION: 8.4
- GRADLE_VERSION: 8.3
- GRADLE_VERSION: 8.0
- GRADLE_VERSION: 7.6.4
#- GRADLE_VERSION: 7.5.1
# ANDROID_GRADLE_VERSION: 7.1.0
script:
- ./cirrus/cirrus-qa.sh

Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ repositories {
}

dependencies {
implementation("org.sonarsource.scanner.api:sonar-scanner-api:2.16.2.588")
implementation("org.sonarsource.scanner.lib:sonar-scanner-java-library:3.1.1.261")
compileOnly("com.google.code.findbugs:jsr305:3.0.2")
compileOnly("com.android.tools.build:gradle:8.1.1")
compileOnly("org.jetbrains.kotlin:kotlin-gradle-plugin:1.8.21")
Expand Down
6 changes: 6 additions & 0 deletions integrationTests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@
<version>3.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jspecify</groupId>
<artifactId>jspecify</artifactId>
<version>1.0.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<profiles>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.stream.Stream;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.jspecify.annotations.Nullable;
import org.junit.Rule;
import org.junit.rules.TemporaryFolder;

Expand Down Expand Up @@ -79,16 +81,12 @@ protected Properties runGradlewSonarSimulationModeWithEnv(String project, Map<St
protected Properties runGradlewSonarSimulationModeWithEnv(String project, String exeRelativePath, Map<String, String> env, String... args) throws Exception {
File out = temp.newFile();
String[] newArgs = Stream.concat(
Stream.of("-Dsonar.scanner.dumpToFile=" + out.getAbsolutePath()),
Stream.of("-Dsonar.scanner.internal.dumpToFile=" + out.getAbsolutePath()),
Arrays.stream(args))
.toArray(String[]::new);
runGradlewSonarWithEnv(project, exeRelativePath, env, newArgs);
RunResult result = runGradlewSonarWithEnv(project, exeRelativePath, env, newArgs);

Properties props = new Properties();
try (FileReader fr = new FileReader(out)) {
props.load(fr);
}
return props;
return result.getDumpedProperties().get();
}

protected RunResult runGradlewSonarWithEnv(String project, Map<String, String> env, String... args) throws Exception {
Expand Down Expand Up @@ -152,7 +150,7 @@ protected RunResult runGradlewWithEnvQuietly(String project, String exeRelativeP

String output = FileUtils.readFileToString(outputFile, StandardCharsets.UTF_8);

return new RunResult(output, p.exitValue());
return new RunResult(output, p.exitValue(), getDumpedProperties(command));
}

private static int getJavaVersion() {
Expand All @@ -168,13 +166,34 @@ private static int getJavaVersion() {
return Integer.parseInt(version);
}

@Nullable
private static Properties getDumpedProperties(List<String> command) throws IOException {
for (String part : command) {
if (part.trim().startsWith("-Dsonar.scanner.internal.dumpToFile=")) {
File dumpFile = new File(part.split("=")[1]);
return loadProperties(dumpFile);
}
}
return null;
}

private static Properties loadProperties(File out) throws IOException {
Properties props = new Properties();
try (FileReader fr = new FileReader(out)) {
props.load(fr);
}
return props;
}

protected static class RunResult {
private final String log;
private final int exitValue;
private final Properties dumpedProperties;

RunResult(String log, int exitValue) {
RunResult(String log, int exitValue, @Nullable Properties dumpedProperties) {
this.log = log;
this.exitValue = exitValue;
this.dumpedProperties = dumpedProperties;
}

public String getLog() {
Expand All @@ -184,5 +203,9 @@ public String getLog() {
public int getExitValue() {
return exitValue;
}

public Optional<Properties> getDumpedProperties() {
return Optional.ofNullable(dumpedProperties);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@
*/
package org.sonarqube.gradle;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.junit.Assume;
import org.junit.Ignore;
import org.junit.Test;

import static java.util.Arrays.stream;
Expand Down Expand Up @@ -65,19 +67,25 @@ public void testDebugModeEnabled() throws Exception {

@Test
public void testSetLogLevel() throws Exception {
RunResult runResult = runGradlewSonarWithEnv("/java-gradle-log-level", emptyMap(), "-Dsonar.scanner.dumpToFile=asd");
File output = temp.newFile("will-not-be-used-for-this-test.txt");
String args = String.format(
"-Dsonar.scanner.internal.dumpToFile=%s",
output.getAbsolutePath()
);
RunResult runResult = runGradlewSonarWithEnv("/java-gradle-log-level", emptyMap(), args);
// This is a debug log entry
assertThat(runResult.getLog()).contains("Work directory:");
assertThat(runResult.getLog()).contains(":sonar");
}

@Ignore("TODO SCANGRADLE-159: sonar.scanner.skip does not prevent reaching to the server!")
@Test
public void testSkip() throws Exception {
Map<String, String> env = new HashMap<>();
env.put("SONARQUBE_SCANNER_PARAMS", "{\"sonar.scanner.skip\" : \"true\" }");
RunResult result = runGradlewSonarWithEnv("/java-gradle-simple", env);

System.out.println(result.getLog());
assertThat(result.getExitValue()).isEqualTo(0);
assertThat(result.getExitValue()).isZero();
assertThat(result.getLog()).contains("Sonar Scanner analysis skipped");
}

Expand All @@ -90,7 +98,7 @@ public void testHostUrlInEnv() throws Exception {
System.out.println(result.getLog());
assertThat(result.getExitValue()).isEqualTo(1);
assertThat(result.getLog()).contains("java.net.UnknownHostException");
assertThat(result.getLog()).contains("SonarQube server [http://host-in-env] can not be reached");
assertThat(result.getLog()).contains("Call to URL [http://host-in-env/api/server/version] failed");

}

Expand Down Expand Up @@ -199,7 +207,7 @@ public void testMultimoduleProjectWithSourceInRoot() throws Exception {
@Test
public void testFlatProjectStructure() throws Exception {
Properties props = runGradlewSonarSimulationModeWithEnv("/multi-module-flat", "build", emptyMap());
assertThat(Paths.get(props.getProperty("sonar.projectBaseDir")).getFileName().toString()).isEqualTo("multi-module-flat");
assertThat(Paths.get(props.getProperty("sonar.projectBaseDir")).getFileName()).hasToString("multi-module-flat");
}

@Test
Expand Down Expand Up @@ -248,8 +256,10 @@ public void testJaCoCoProperties() throws Exception {
public void testProjectWithConfigurationCacheDoubleExecutionsShouldWork() throws Exception {
Assume.assumeTrue("Tests only applies to version 6.5.0 or greater", getGradleVersion().isGreaterThanOrEqualTo("6.5.0"));

runGradlewSonarWithEnv("/java-gradle-simple", emptyMap(), "-Dsonar.scanner.dumpToFile=asd", "--configuration-cache");
RunResult runResult = runGradlewSonarWithEnv("/java-gradle-simple", emptyMap(), "-Dsonar.scanner.dumpToFile=asd", "--configuration-cache");
String dumpProperty = String.format("-Dsonar.scanner.internal.dumpToFile=%s", temp.newFile().getAbsolutePath());

runGradlewSonarWithEnv("/java-gradle-simple", emptyMap(), dumpProperty, "--configuration-cache");
RunResult runResult = runGradlewSonarWithEnv("/java-gradle-simple", emptyMap(), dumpProperty, "--configuration-cache");

assertThat(runResult.getLog()).doesNotContain("no properties configured, was it skipped in all projects?");
assertThat(runResult.getLog()).contains("BUILD SUCCESSFUL");
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/org/sonarqube/gradle/ScanProperties.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* SonarQube Scanner for Gradle
* Copyright (C) 2015-2024 SonarSource
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* 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";
private ScanProperties() {
/* This is a utility class with constants */
}
}
9 changes: 4 additions & 5 deletions src/main/java/org/sonarqube/gradle/SonarPropertyComputer.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@
import org.gradle.testing.jacoco.tasks.JacocoReport;
import org.gradle.util.GradleVersion;
import org.sonarqube.gradle.SonarUtils.InputFileType;
import org.sonarsource.scanner.api.ScanProperties;
import org.sonarsource.scanner.api.Utils;
import org.sonarsource.scanner.lib.EnvironmentConfig;

import static java.util.stream.Collectors.groupingBy;
import static org.sonarqube.gradle.SonarUtils.appendProp;
Expand Down Expand Up @@ -191,7 +190,7 @@ private boolean shouldApplyScanAll(Project project, Map<String, Object> properti
var sonarProps = new SonarProperties(new HashMap<>());
actionBroadcastMap.get(project.getPath()).execute(sonarProps);

boolean sourcesOrTestsAlreadySet = Stream.of(System.getProperties().keySet(), Utils.loadEnvironmentProperties(System.getenv()).keySet(), sonarProps.getProperties().keySet())
boolean sourcesOrTestsAlreadySet = Stream.of(System.getProperties().keySet(), EnvironmentConfig.load(System.getenv()).keySet(), sonarProps.getProperties().keySet())
.flatMap(Collection::stream)
.map(String.class::cast)
.anyMatch(k -> ScanProperties.PROJECT_SOURCE_DIRS.endsWith(k) || ScanProperties.PROJECT_TEST_DIRS.endsWith(k));
Expand Down Expand Up @@ -337,8 +336,8 @@ private static void configureSourceEncoding(Project project, final Map<String, O
}

private static void addEnvironmentProperties(Map<String, Object> properties) {
for (Map.Entry<Object, Object> e : Utils.loadEnvironmentProperties(System.getenv()).entrySet()) {
properties.put(e.getKey().toString(), e.getValue().toString());
for (Map.Entry<String, String> e : EnvironmentConfig.load(System.getenv()).entrySet()) {
properties.put(e.getKey(), e.getValue());
}
}

Expand Down
18 changes: 11 additions & 7 deletions src/main/java/org/sonarqube/gradle/SonarTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.TaskAction;
import org.gradle.util.GradleVersion;
import org.sonarsource.scanner.api.EmbeddedScanner;
import org.sonarsource.scanner.api.LogOutput;
import org.sonarsource.scanner.api.ScanProperties;
import org.sonarsource.scanner.lib.ScannerEngineBootstrapper;
import org.sonarsource.scanner.lib.ScannerEngineFacade;
import org.sonarsource.scanner.lib.internal.batch.LogOutput;

/**
* Analyses one or more projects with the <a href="http://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+Gradle">SonarQube Scanner</a>.
Expand Down Expand Up @@ -125,10 +125,14 @@ public void run() {
return;
}

EmbeddedScanner scanner = EmbeddedScanner.create("ScannerGradle", getPluginVersion() + "/" + GradleVersion.current(), getLogOutput())
.addGlobalProperties(mapProperties);
scanner.start();
scanner.execute(new HashMap<>());
ScannerEngineBootstrapper scanner = ScannerEngineBootstrapper
.create("ScannerGradle", getPluginVersion() + "/" + GradleVersion.current())
.addBootstrapProperties(mapProperties);
try (ScannerEngineFacade engineFacade = scanner.bootstrap()) {
engineFacade.analyze(new HashMap<>());
} catch (Exception e) {
throw new RuntimeException(e);
}
}

private String getPluginVersion() {
Expand Down
1 change: 0 additions & 1 deletion src/main/java/org/sonarqube/gradle/SonarUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import java.util.stream.StreamSupport;
import javax.annotation.Nullable;
import org.gradle.api.Project;
import org.sonarsource.scanner.api.ScanProperties;

public class SonarUtils {

Expand Down
Loading

0 comments on commit def8e0b

Please sign in to comment.