From f1361e89bb0e09cdd0c9186154dbb3b8680f556b Mon Sep 17 00:00:00 2001 From: Janos Gyerik Date: Mon, 13 Mar 2017 17:22:57 +0100 Subject: [PATCH] SQSCANNER-38 self-contained distributions for Linux, Windows and Mac OS X (#26) --- it/pom.xml | 33 ++- .../com/sonar/runner/it/DistributionTest.java | 72 +++++++ .../runner/it/SonarScannerTestSuite.java | 2 +- pom.xml | 189 +++++++++++++++++- src/main/assembly/bin/sonar-runner | 10 +- src/main/assembly/bin/sonar-scanner | 13 +- src/main/assembly/bin/sonar-scanner.bat | 9 +- src/main/assembly/dist-linux.xml | 58 ++++++ src/main/assembly/dist-macosx.xml | 57 ++++++ src/main/assembly/dist-windows.xml | 55 +++++ src/main/assembly/filter-dist.properties | 1 + src/main/assembly/filter-noarch.properties | 1 + assembly.xml => src/main/assembly/noarch.xml | 0 travis.sh | 2 +- 14 files changed, 490 insertions(+), 12 deletions(-) create mode 100644 it/src/test/java/com/sonar/runner/it/DistributionTest.java create mode 100644 src/main/assembly/dist-linux.xml create mode 100644 src/main/assembly/dist-macosx.xml create mode 100644 src/main/assembly/dist-windows.xml create mode 100644 src/main/assembly/filter-dist.properties create mode 100644 src/main/assembly/filter-noarch.properties rename assembly.xml => src/main/assembly/noarch.xml (100%) diff --git a/it/pom.xml b/it/pom.xml index e5e66aa..f84b78c 100644 --- a/it/pom.xml +++ b/it/pom.xml @@ -31,7 +31,7 @@ org.sonarsource.orchestrator sonar-orchestrator - 3.13 + 3.15.0.974 junit @@ -115,6 +115,7 @@ + org.apache.maven.plugins maven-dependency-plugin @@ -130,6 +131,36 @@ org.sonarsource.scanner.cli:sonar-scanner-cli:${scanner.version}:zip + + get-linux-scanner + initialize + + get + + + org.sonarsource.scanner.cli:sonar-scanner-cli:${scanner.version}:zip:linux + + + + get-windows-scanner + initialize + + get + + + org.sonarsource.scanner.cli:sonar-scanner-cli:${scanner.version}:zip:windows + + + + get-macosx-scanner + initialize + + get + + + org.sonarsource.scanner.cli:sonar-scanner-cli:${scanner.version}:zip:macosx + + diff --git a/it/src/test/java/com/sonar/runner/it/DistributionTest.java b/it/src/test/java/com/sonar/runner/it/DistributionTest.java new file mode 100644 index 0000000..c38a1fa --- /dev/null +++ b/it/src/test/java/com/sonar/runner/it/DistributionTest.java @@ -0,0 +1,72 @@ +/* + * SonarSource :: IT :: SonarQube Scanner + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact 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 02110-1301, USA. + */ +package com.sonar.runner.it; + +import com.sonar.orchestrator.build.BuildFailureException; +import com.sonar.orchestrator.build.SonarScanner; +import com.sonar.orchestrator.locator.ResourceLocation; +import java.io.File; +import java.io.IOException; +import java.util.Map; +import org.junit.After; +import org.junit.Test; +import org.sonarqube.ws.WsMeasures.Measure; + +import static java.lang.Integer.parseInt; +import static org.assertj.core.api.Assertions.assertThat; + +public class DistributionTest extends ScannerTestCase { + + @After + public void cleanup() { + orchestrator.resetData(); + } + + @Test + public void should_succeed_with_self_contained_jre_despite_rubbish_java_home() throws IOException, InterruptedException { + String projectKey = "java:basedir-with-source"; + orchestrator.getServer().restoreProfile(ResourceLocation.create("/sonar-way-profile.xml")); + orchestrator.getServer().provisionProject(projectKey, "Basedir with source"); + orchestrator.getServer().associateProjectToQualityProfile(projectKey, "java", "sonar-way"); + + File projectDir = new File("projects/basedir-with-source"); + SonarScanner build = newScanner(projectDir, "sonar.projectKey", projectKey) + .setEnvironmentVariable("JAVA_HOME", "nonexistent") + .useNative(); + orchestrator.executeBuild(build, true); + + Map projectMeasures = getMeasures(projectKey, "files", "ncloc"); + assertThat(parseInt(projectMeasures.get("files").getValue())).isEqualTo(1); + assertThat(parseInt(projectMeasures.get("ncloc").getValue())).isGreaterThan(1); + } + + @Test(expected = BuildFailureException.class) + public void should_fail_without_self_contained_jre_when_rubbish_java_home() throws IOException, InterruptedException { + String projectKey = "java:basedir-with-source"; + orchestrator.getServer().restoreProfile(ResourceLocation.create("/sonar-way-profile.xml")); + orchestrator.getServer().provisionProject(projectKey, "Basedir with source"); + orchestrator.getServer().associateProjectToQualityProfile(projectKey, "java", "sonar-way"); + + File projectDir = new File("projects/basedir-with-source"); + SonarScanner build = newScanner(projectDir, "sonar.projectKey", projectKey) + .setEnvironmentVariable("JAVA_HOME", "nonexistent"); + orchestrator.executeBuild(build, true); + } +} diff --git a/it/src/test/java/com/sonar/runner/it/SonarScannerTestSuite.java b/it/src/test/java/com/sonar/runner/it/SonarScannerTestSuite.java index 890fe17..c43446f 100644 --- a/it/src/test/java/com/sonar/runner/it/SonarScannerTestSuite.java +++ b/it/src/test/java/com/sonar/runner/it/SonarScannerTestSuite.java @@ -26,7 +26,7 @@ import org.junit.runners.Suite; import org.junit.runners.Suite.SuiteClasses; @RunWith(Suite.class) -@SuiteClasses({JavaTest.class, MultimoduleTest.class}) +@SuiteClasses({JavaTest.class, MultimoduleTest.class, DistributionTest.class}) public class SonarScannerTestSuite { @ClassRule diff --git a/pom.xml b/pom.xml index 4ca465a..31ce39d 100644 --- a/pom.xml +++ b/pom.xml @@ -44,6 +44,14 @@ sonar-scanner-cli + + + ${project.build.directory}/unpack + ${project.build.finalName}.jar + 8u121 + jre1.8.0_121 + jre1.8.0_121 + jre1.8.0_121.jre/Contents/Home @@ -129,6 +137,7 @@ maven-assembly-plugin + cli package single @@ -138,8 +147,11 @@ false \ - ${project.basedir}/assembly.xml + src/main/assembly/noarch.xml + + src/main/assembly/filter-noarch.properties + @@ -179,5 +191,180 @@ it + + + dist-linux + + + + org.apache.maven.plugins + maven-dependency-plugin + 3.0.0 + + + unpack-linux + package + + unpack + + + + + com.oracle + jre + ${jre.version} + linux-x64 + tar.gz + true + + + ${unpack.dir}/linux + + + + + + + maven-assembly-plugin + + + assemble-linux + package + + single + + + sonar-scanner-${project.version} + \ + + src/main/assembly/dist-linux.xml + + + src/main/assembly/filter-dist.properties + + + + + + + + + + + dist-windows + + + + org.apache.maven.plugins + maven-dependency-plugin + 3.0.0 + + + unpack-windows + package + + unpack + + + + + com.oracle + jre + ${jre.version} + windows-x64 + tar.gz + true + + + ${unpack.dir}/windows + + + + + + + maven-assembly-plugin + + + assemble-windows + package + + single + + + sonar-scanner-${project.version} + \ + + src/main/assembly/dist-windows.xml + + + src/main/assembly/filter-dist.properties + + + + + + + + + + + dist-macosx + + + + org.apache.maven.plugins + maven-dependency-plugin + 3.0.0 + + + unpack-macosx + package + + unpack + + + + + + + com.oracle + jre + ${jre.version} + macosx-x64 + tar.gz + true + + + ${unpack.dir}/macosx + + + + + maven-assembly-plugin + + + assemble-macosx + package + + single + + + sonar-scanner-${project.version} + \ + + src/main/assembly/dist-macosx.xml + + + src/main/assembly/filter-dist.properties + + + + + + + + + diff --git a/src/main/assembly/bin/sonar-runner b/src/main/assembly/bin/sonar-runner index 14e3a58..a116ea4 100755 --- a/src/main/assembly/bin/sonar-runner +++ b/src/main/assembly/bin/sonar-runner @@ -36,9 +36,12 @@ sonar_runner_home=$(dirname "$script_path")/.. # make it fully qualified sonar_runner_home=$(cd "$sonar_runner_home" && pwd) -# check that the sonar_runner_home has been correctly set -if [ ! -f "$sonar_runner_home/lib/sonar-scanner-cli-${project.version}.jar" ] ; then - echo '$sonar_runner_home' does not point to a valid installation directory: $sonar_runner_home +jar_file=$sonar_runner_home/lib/sonar-scanner-cli-${project.version}.jar + +# check that sonar_runner_home has been correctly set +if [ ! -f "$jar_file" ] ; then + echo "File does not exist: $jar_file" + echo "'$sonar_runner_home' does not point to a valid installation directory: $sonar_runner_home" exit 1 fi @@ -57,7 +60,6 @@ then fi fi -jar_file=${sonar_runner_home}/lib/sonar-scanner-cli-${project.version}.jar project_home=$(pwd) #echo "Info: Using sonar-runner at $sonar_runner_home" diff --git a/src/main/assembly/bin/sonar-scanner b/src/main/assembly/bin/sonar-scanner index 38f0095..b0c19f3 100755 --- a/src/main/assembly/bin/sonar-scanner +++ b/src/main/assembly/bin/sonar-scanner @@ -35,12 +35,20 @@ sonar_scanner_home=$(dirname "$script_path")/.. # make it fully qualified sonar_scanner_home=$(cd "$sonar_scanner_home" && pwd -P) +jar_file=$sonar_scanner_home/lib/sonar-scanner-cli-${project.version}.jar + # check that sonar_scanner_home has been correctly set -if [ ! -f "$sonar_scanner_home/lib/sonar-scanner-cli-${project.version}.jar" ] ; then - echo '$sonar_scanner_home' does not point to a valid installation directory: $sonar_scanner_home +if [ ! -f "$jar_file" ] ; then + echo "File does not exist: $jar_file" + echo "'$sonar_scanner_home' does not point to a valid installation directory: $sonar_scanner_home" exit 1 fi +use_embedded_jre=${use_embedded_jre} +if [ "$use_embedded_jre" = true ]; then + export JAVA_HOME=$sonar_scanner_home/lib/jre +fi + if [ -n "$JAVA_HOME" ] then java_cmd="$JAVA_HOME/bin/java" @@ -48,7 +56,6 @@ else java_cmd="$(which java)" fi -jar_file=${sonar_scanner_home}/lib/sonar-scanner-cli-${project.version}.jar project_home=$(pwd) #echo "Info: Using sonar-scanner at $sonar_scanner_home" diff --git a/src/main/assembly/bin/sonar-scanner.bat b/src/main/assembly/bin/sonar-scanner.bat index bd4c920..833dcbb 100644 --- a/src/main/assembly/bin/sonar-scanner.bat +++ b/src/main/assembly/bin/sonar-scanner.bat @@ -13,8 +13,16 @@ set ERROR_CODE=0 @REM set local scope for the variables with windows NT shell @setlocal +set SONAR_SCANNER_HOME=%~dp0.. + @REM ==== START VALIDATION ==== @REM *** JAVA EXEC VALIDATION *** + +set use_embedded_jre=${use_embedded_jre} +if "%use_embedded_jre%" == "true" ( + set JAVA_HOME=%SONAR_SCANNER_HOME%\lib\jre +) + if not "%JAVA_HOME%" == "" goto foundJavaHome for %%i in (java.exe) do set JAVA_EXEC=%%~$PATH:i @@ -47,7 +55,6 @@ goto error set JAVA_EXEC="%JAVA_HOME%\bin\java.exe" :OkJava -set SONAR_SCANNER_HOME=%~dp0.. goto run diff --git a/src/main/assembly/dist-linux.xml b/src/main/assembly/dist-linux.xml new file mode 100644 index 0000000..d4d1923 --- /dev/null +++ b/src/main/assembly/dist-linux.xml @@ -0,0 +1,58 @@ + + + linux + + zip + + true + + + + + ${unpack.dir}/linux/${jre.dirname.linux} + lib/jre + + bin/** + man/** + plugin/** + + + + + + ${unpack.dir}/linux/${jre.dirname.linux}/bin + lib/jre/bin + + java + + 0755 + + + + + ${project.build.directory} + lib + 0644 + + ${scanner.jar} + + + + src/main/assembly/conf + conf + 0644 + unix + + + src/main/assembly/bin + bin + 0755 + unix + true + + sonar-scanner + sonar-scanner-debug + + + + diff --git a/src/main/assembly/dist-macosx.xml b/src/main/assembly/dist-macosx.xml new file mode 100644 index 0000000..65ad78e --- /dev/null +++ b/src/main/assembly/dist-macosx.xml @@ -0,0 +1,57 @@ + + + macosx + + zip + + true + + + + + ${unpack.dir}/macosx/${jre.dirname.macosx} + lib/jre + + bin/** + man/** + + + + + + ${unpack.dir}/macosx/${jre.dirname.macosx}/bin + lib/jre/bin + + java + + 0755 + + + + + ${project.build.directory} + lib + 0644 + + ${scanner.jar} + + + + src/main/assembly/conf + conf + 0644 + unix + + + src/main/assembly/bin + bin + 0755 + unix + true + + sonar-scanner + sonar-scanner-debug + + + + diff --git a/src/main/assembly/dist-windows.xml b/src/main/assembly/dist-windows.xml new file mode 100644 index 0000000..132859b --- /dev/null +++ b/src/main/assembly/dist-windows.xml @@ -0,0 +1,55 @@ + + + windows + + zip + + true + + + + + ${unpack.dir}/windows/${jre.dirname.windows} + lib/jre + + bin/** + man/** + plugin/** + + + + + + ${unpack.dir}/windows/${jre.dirname.windows}/bin + lib/jre/bin + 0755 + + + + + ${project.build.directory} + lib + 0644 + + ${scanner.jar} + + + + src/main/assembly/conf + conf + 0644 + unix + + + src/main/assembly/bin + bin + 0755 + dos + true + + sonar-scanner.bat + sonar-scanner-debug.bat + + + + diff --git a/src/main/assembly/filter-dist.properties b/src/main/assembly/filter-dist.properties new file mode 100644 index 0000000..b7bbc34 --- /dev/null +++ b/src/main/assembly/filter-dist.properties @@ -0,0 +1 @@ +use_embedded_jre=true diff --git a/src/main/assembly/filter-noarch.properties b/src/main/assembly/filter-noarch.properties new file mode 100644 index 0000000..b1c9e92 --- /dev/null +++ b/src/main/assembly/filter-noarch.properties @@ -0,0 +1 @@ +use_embedded_jre=false diff --git a/assembly.xml b/src/main/assembly/noarch.xml similarity index 100% rename from assembly.xml rename to src/main/assembly/noarch.xml diff --git a/travis.sh b/travis.sh index 1920484..81cd758 100755 --- a/travis.sh +++ b/travis.sh @@ -11,5 +11,5 @@ configureTravis export DEPLOY_PULL_REQUEST=true -regular_mvn_build_deploy_analyze +regular_mvn_build_deploy_analyze -Pdist-linux,dist-windows,dist-macosx -- 2.39.5