From e12852cc16f5d328c25e6569759d0eb1ee467835 Mon Sep 17 00:00:00 2001 From: Dimitris Kavvathas Date: Tue, 9 Aug 2022 17:34:22 +0200 Subject: [PATCH] SONAR-17137 add SONAR_JAVA_PATH environmental variable for user-defined Java executable path --- .../src/pages/setup/install-server.md | 11 ++-- .../src/pages/setup/operate-server.md | 14 ++--- .../sonar-docs/src/pages/setup/upgrading.md | 2 +- sonar-application/build.gradle | 10 +++- .../bin/windows-x86-64/SonarService.bat | 32 ++++++++++++ .../bin/windows-x86-64/SonarService.xml | 25 --------- .../bin/windows-x86-64/StartSonar.bat | 49 +++++++++--------- .../SonarServiceWrapper.exe} | Bin .../lib/SonarServiceWrapper.xml | 45 ++++++++++++++++ .../bin/windows-x86-64/lib/find_java.bat | 32 ++++++++++++ 10 files changed, 155 insertions(+), 65 deletions(-) create mode 100644 sonar-application/src/main/assembly/bin/windows-x86-64/SonarService.bat delete mode 100644 sonar-application/src/main/assembly/bin/windows-x86-64/SonarService.xml rename sonar-application/src/main/assembly/bin/windows-x86-64/{SonarService.exe => lib/SonarServiceWrapper.exe} (100%) create mode 100644 sonar-application/src/main/assembly/bin/windows-x86-64/lib/SonarServiceWrapper.xml create mode 100644 sonar-application/src/main/assembly/bin/windows-x86-64/lib/find_java.bat diff --git a/server/sonar-docs/src/pages/setup/install-server.md b/server/sonar-docs/src/pages/setup/install-server.md index ad817e67a5c..dc655a26fb8 100644 --- a/server/sonar-docs/src/pages/setup/install-server.md +++ b/server/sonar-docs/src/pages/setup/install-server.md @@ -157,20 +157,19 @@ You can now browse SonarQube at _http://localhost:9000_ (the default System admi ### Adjusting the Java Installation +By default, the scripts will use the Java executable available in the PATH. If there are multiple versions of Java installed on your server, you may need to explicitly define which version of Java is used. -#### Linux / macOS -To change the Java JVM used by SonarQube, edit _$SONARQUBE-HOME/bin/{platform_OS}/sonar.sh_ and update the following line: +It is possible to overwrite the default Java executable by setting the environmental variable SONAR_JAVA_PATH +#### Linux / MacOS ``` -JAVA_CMD="java" +SONAR_JAVA_PATH="path/to/java_home/bin/java" ``` #### Windows -To change the Java JVM used by SonarQube, edit > _$SONARQUBE-HOME\bin\windows-x86-64\SonarService.xml_ and update the following line: - ``` -java +SONAR_JAVA_PATH="C:\Program Files\java_home\bin\java.exe" ``` ### Advanced Installation Features diff --git a/server/sonar-docs/src/pages/setup/operate-server.md b/server/sonar-docs/src/pages/setup/operate-server.md index 1acf1bb4f23..68b50ea8f7a 100644 --- a/server/sonar-docs/src/pages/setup/operate-server.md +++ b/server/sonar-docs/src/pages/setup/operate-server.md @@ -8,25 +8,25 @@ url: /setup/operate-server/ ### Install or Uninstall SonarQube as a Service ``` -> %SONAR_HOME%\bin\windows-x86-64\SonarService.exe install -> %SONAR_HOME%\bin\windows-x86-64\SonarService.exe uninstall +> %SONAR_HOME%\bin\windows-x86-64\SonarService.bat install +> %SONAR_HOME%\bin\windows-x86-64\SonarService.bat uninstall ``` ### Start or Stop the Service ``` -> %SONAR_HOME%\bin\windows-x86-64\SonarService.exe start +> %SONAR_HOME%\bin\windows-x86-64\SonarService.bat start ``` -**Note:** By default, the service will use the Java executable available on the Windows PATH. This setting can be changed in `%SONAR_HOME%\bin\windows-x86-64\SonarService.xml`, by updating the ``. +**Note:** By default, the service will use the Java executable available on the Windows PATH. This setting can be changed by setting the environmental variable SONAR_JAVA_PATH. See more in [Adjusting the Java Installation](https://docs.sonarqube.org/latest/setup/install-server/). ``` -> %SONAR_HOME%\bin\windows-x86-64\SonarService.exe stop +> %SONAR_HOME%\bin\windows-x86-64\SonarService.bat stop ``` -**Note:** `> %SONAR_HOME%\bin\windows-x86-64\SonarService.exe stop` does a graceful shutdown where no new analysis report processing can start, but the tasks in progress are allowed to finish. The time a stop will take depends on the processing time of the tasks in progress. You'll need to kill all SonarQube processes manually to force a stop. +**Note:** `> %SONAR_HOME%\bin\windows-x86-64\SonarService.bat stop` does a graceful shutdown where no new analysis report processing can start, but the tasks in progress are allowed to finish. The time a stop will take depends on the processing time of the tasks in progress. You'll need to kill all SonarQube processes manually to force a stop. ### Service Status Check if the SonarQube service is running ``` -> %SONAR_HOME%\bin\windows-x86-64\SonarService.exe status +> %SONAR_HOME%\bin\windows-x86-64\SonarService.bat status ``` ## Running SonarQube Manually on Linux diff --git a/server/sonar-docs/src/pages/setup/upgrading.md b/server/sonar-docs/src/pages/setup/upgrading.md index d719f90d0ca..1e179ce6551 100644 --- a/server/sonar-docs/src/pages/setup/upgrading.md +++ b/server/sonar-docs/src/pages/setup/upgrading.md @@ -137,7 +137,7 @@ If you use an external configuration, such as a script or Windows Service to con - For Windows you can update your service by running: ``` > sc delete SonarQube -> $NEW_SONAR_HOME\bin\windows-x86-64\SonarService.exe install +> $NEW_SONAR_HOME\bin\windows-x86-64\SonarService.bat install ``` ### Rebuilding indexes diff --git a/sonar-application/build.gradle b/sonar-application/build.gradle index 1784e591257..d8f39a1363b 100644 --- a/sonar-application/build.gradle +++ b/sonar-application/build.gradle @@ -141,7 +141,7 @@ task zip(type: Zip, dependsOn: [configurations.compileClasspath, tasks.downloadL from(file('src/main/assembly')) { exclude 'conf/sonar.properties' exclude 'conf/wrapper.conf' - exclude 'bin/windows-x86-64/SonarService.xml' + exclude 'bin/windows-x86-64/lib/SonarServiceWrapper.xml' exclude 'bin/windows-x86-64/StartSonar.bat' exclude 'elasticsearch-patch' exclude 'bin/linux-x86-64/sonar.sh' @@ -240,13 +240,19 @@ task zip(type: Zip, dependsOn: [configurations.compileClasspath, tasks.downloadL } into("${archiveDir}/bin/windows-x86-64/") { - from file('src/main/assembly/bin/windows-x86-64/SonarService.xml') from file('src/main/assembly/bin/windows-x86-64/StartSonar.bat') filter(ReplaceTokens, tokens: [ 'sqversion': version ]) } + into("${archiveDir}/bin/windows-x86-64/lib/") { + from file('src/main/assembly/bin/windows-x86-64/lib/SonarServiceWrapper.xml') + filter(ReplaceTokens, tokens: [ + 'sqversion': version + ]) + } + into("${archiveDir}/elasticsearch/") { from file('src/main/assembly/elasticsearch-patch') include 'bin/elasticsearch' diff --git a/sonar-application/src/main/assembly/bin/windows-x86-64/SonarService.bat b/sonar-application/src/main/assembly/bin/windows-x86-64/SonarService.bat new file mode 100644 index 00000000000..1df5e2cf027 --- /dev/null +++ b/sonar-application/src/main/assembly/bin/windows-x86-64/SonarService.bat @@ -0,0 +1,32 @@ +@rem SonarQube +@rem Copyright (C) 2009-2022 SonarSource SA +@rem mailto:info AT sonarsource DOT com +@rem +@rem This program is free software; you can redistribute it and/or +@rem modify it under the terms of the GNU Lesser General Public +@rem License as published by the Free Software Foundation; either +@rem version 3 of the License, or (at your option) any later version. +@rem +@rem This program is distributed in the hope that it will be useful, +@rem but WITHOUT ANY WARRANTY; without even the implied warranty of +@rem MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +@rem Lesser General Public License for more details. +@rem +@rem You should have received a copy of the GNU Lesser General Public License +@rem along with this program; if not, write to the Free Software Foundation, +@rem Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +@echo off +setlocal + +rem DO NOT EDIT THE FOLLOWING SECTIONS + +set REALPATH=%~dp0 +rem check if Java is found +set JAVA_EXE= +call "%REALPATH%lib\find_java.bat" set_java_exe FAIL || goto:eof + +rem call the SonarServiceWrapper.exe passing all the parameters +"%REALPATH%lib\SonarServiceWrapper.exe" %* + +endlocal diff --git a/sonar-application/src/main/assembly/bin/windows-x86-64/SonarService.xml b/sonar-application/src/main/assembly/bin/windows-x86-64/SonarService.xml deleted file mode 100644 index 0aaa9bc0609..00000000000 --- a/sonar-application/src/main/assembly/bin/windows-x86-64/SonarService.xml +++ /dev/null @@ -1,25 +0,0 @@ - - - java - - - - -Xms8m -Xmx32m - -Djava.awt.headless=true - --add-exports=java.base/jdk.internal.ref=ALL-UNNAMED - --add-opens=java.base/java.lang=ALL-UNNAMED - --add-opens=java.base/java.nio=ALL-UNNAMED - --add-opens=java.base/sun.nio.ch=ALL-UNNAMED - --add-opens=java.management/sun.management=ALL-UNNAMED - --add-opens=jdk.management/com.sun.management.internal=ALL-UNNAMED - -cp "..\..\lib\sonar-application-@sqversion@.jar" "org.sonar.application.App" - - - SonarQube - SonarQube - SonarQube - - ../../logs - - - \ No newline at end of file diff --git a/sonar-application/src/main/assembly/bin/windows-x86-64/StartSonar.bat b/sonar-application/src/main/assembly/bin/windows-x86-64/StartSonar.bat index 500b5cbe8f3..e154489c0bc 100755 --- a/sonar-application/src/main/assembly/bin/windows-x86-64/StartSonar.bat +++ b/sonar-application/src/main/assembly/bin/windows-x86-64/StartSonar.bat @@ -19,12 +19,13 @@ @echo off setlocal -rem Path to the Java executable -set JAVA_EXE="java" - - rem DO NOT EDIT THE FOLLOWING SECTIONS + set REALPATH=%~dp0 + +set JAVA_EXE= +call "%REALPATH%lib\find_java.bat" set_java_exe FAIL || goto:eof + call :check_if_sonar_is_running FAIL || goto:eof echo Starting SonarQube... @@ -41,28 +42,28 @@ echo Starting SonarQube... goto:eof :check_if_sonar_is_running -set "SQ_SERVICE=" -for /f %%i in ('%REALPATH%/SonarService.exe status') do set "SQ_SERVICE=%%i" -if [%SQ_SERVICE%]==[Started] ( - echo ERROR: SonarQube is already running as a service. - exit /b 1 -) + set "SQ_SERVICE=" + for /f %%i in ('%REALPATH%SonarService.bat status ^>nul 2^>nul') do set "SQ_SERVICE=%%i" + if [%SQ_SERVICE%]==[Started] ( + echo ERROR: SonarQube is already running as a service. + exit /b 1 + ) -set "SQ_PROCESS=" -where jps >nul 2>nul -if %errorlevel% equ 0 ( - rem give priority to jps command if present - for /f "tokens=1" %%i in ('jps -l ^| findstr "org.sonar.application.App"') do set "SQ_PROCESS=%%i" -) else ( - rem fallback to wmic command - for /f "tokens=2" %%i in ('wmic process where "name='java.exe' and commandline like '%%org.sonar.application.App%%'" get name^, processid 2^>nul ^| findstr "java"') do set "SQ_PROCESS=%%i" -) + set "SQ_PROCESS=" + where jps >nul 2>nul + if %errorlevel% equ 0 ( + rem give priority to jps command if present + for /f "tokens=1" %%i in ('jps -l ^| findstr "org.sonar.application.App"') do set "SQ_PROCESS=%%i" + ) else ( + rem fallback to wmic command + for /f "tokens=2" %%i in ('wmic process where "name='java.exe' and commandline like '%%org.sonar.application.App%%'" get name^, processid 2^>nul ^| findstr "java"') do set "SQ_PROCESS=%%i" + ) -if not [%SQ_PROCESS%]==[] ( - echo ERROR: Another instance of the SonarQube application is already running with PID %SQ_PROCESS% - exit /b 1 -) -goto:eof + if not [%SQ_PROCESS%]==[] ( + echo ERROR: Another instance of the SonarQube application is already running with PID %SQ_PROCESS% + exit /b 1 + ) + goto:eof endlocal diff --git a/sonar-application/src/main/assembly/bin/windows-x86-64/SonarService.exe b/sonar-application/src/main/assembly/bin/windows-x86-64/lib/SonarServiceWrapper.exe similarity index 100% rename from sonar-application/src/main/assembly/bin/windows-x86-64/SonarService.exe rename to sonar-application/src/main/assembly/bin/windows-x86-64/lib/SonarServiceWrapper.exe diff --git a/sonar-application/src/main/assembly/bin/windows-x86-64/lib/SonarServiceWrapper.xml b/sonar-application/src/main/assembly/bin/windows-x86-64/lib/SonarServiceWrapper.xml new file mode 100644 index 00000000000..ec836f3c874 --- /dev/null +++ b/sonar-application/src/main/assembly/bin/windows-x86-64/lib/SonarServiceWrapper.xml @@ -0,0 +1,45 @@ + + + + + %JAVA_EXE% + + + + -Xms8m -Xmx32m + -Djava.awt.headless=true + --add-exports=java.base/jdk.internal.ref=ALL-UNNAMED + --add-opens=java.base/java.lang=ALL-UNNAMED + --add-opens=java.base/java.nio=ALL-UNNAMED + --add-opens=java.base/sun.nio.ch=ALL-UNNAMED + --add-opens=java.management/sun.management=ALL-UNNAMED + --add-opens=jdk.management/com.sun.management.internal=ALL-UNNAMED + -cp "..\..\..\lib\sonar-application-@sqversion@.jar" "org.sonar.application.App" + + + SonarQube + SonarQube + SonarQube + + ../../../logs + + + \ No newline at end of file diff --git a/sonar-application/src/main/assembly/bin/windows-x86-64/lib/find_java.bat b/sonar-application/src/main/assembly/bin/windows-x86-64/lib/find_java.bat new file mode 100644 index 00000000000..12ac5fcb7c6 --- /dev/null +++ b/sonar-application/src/main/assembly/bin/windows-x86-64/lib/find_java.bat @@ -0,0 +1,32 @@ +@echo off +call:%~1 +goto exit + +rem Sets the JAVA_EXE var to be used by the calling script. +rem By default, the java.exe in the PATH is selected. This can be overwritten by the environmental variable SONAR_JAVA_PATH. +rem Returns an error code if Java executable is not found in the PATH and the environmental variable SONAR_JAVA_PATH is not properly set. +:set_java_exe + rem use java.exe from PATH, by default + where "java.exe" >nul 2>nul + if %errorlevel% equ 0 ( + set JAVA_EXE="java.exe" + ) + + rem if the environmental variable SONAR_JAVA_PATH is set, override the default java.exe + if not "%SONAR_JAVA_PATH%"=="" ( + if exist "%SONAR_JAVA_PATH%" ( + set JAVA_EXE="%SONAR_JAVA_PATH%" + ) else ( + echo ERROR: "%SONAR_JAVA_PATH%" not found. Please make sure that the environmental variable SONAR_JAVA_PATH points to the Java executable. + exit /b 1 + ) + ) + + if [%JAVA_EXE%]==[] ( + echo ERROR: java.exe not found. Please make sure that the environmental variable SONAR_JAVA_PATH points to the Java executable. + exit /b 1 + ) + goto:eof + +:exit +exit /b \ No newline at end of file -- 2.39.5