Recently we are having an issue with the Sonarscanner in our .net project, the error indicated that we needed to upgrade the java from 11 to 17 which we did and set the envionment variables, but still it seems like it is picking up the older version not sure why. we are using Azure build pipelines and the sonarscanner is configured to run as part of build process. any suggestion why the sonarscanner not picking up the java 17 version or if i am missing sth here?
– yaml file config that kick start the build and sonar process as below,
- job: xxxxxprocessorservicexxxx
pool:
name: Azure Pipelines
demands:
- Project -equals UTC
vmImage: 'ubuntu-latest'
dependsOn: checkservicechanges
condition: or(eq(variables['Build.Reason'], 'Schedule'), eq(variables['CreateAllServices'], 'true'), eq(dependencies.checkservicechanges.outputs['Myvariable.astridprocessorservice'], 'true'))
steps:
- task: JavaToolInstaller@0.189.0
inputs:
versionSpec: '17'
jdkArchitectureOption: 'x64'
jdkSourceOption: 'PreInstalled'
env:
JAVA_HOME: "$(JAVA_HOME_17_X64)"
PATH: "$(JAVA_HOME_17_X64)/bin:$(PATH)"
- task: Bash@3
inputs:
targetType: 'inline'
script: |
cd /etc/bkupexec
./agent.be &
- task: Bash@3
inputs:
targetType: 'inline'
script: |
java -version
whereis java
- template: xxxxprocessorservicexxxx/xxxxservicexxx.yaml
– The below is some relavent portion of the xxxxservicexxx.yaml template file
steps:
- bash: |
set -xe # Fail the command if it exits with a non-zero value.
java -version
echo "$PATH"
sudo dotnet tool install --global dotnet-sonarscanner --version 6.0.0
echo "2\$JAVA_HOME\:$JAVA_HOME"
sudo dotnet restore ${{ parameters.buildProjectFile }}
sudo dotnet tool list --global
if [ $(build.Reason) = 'PullRequest' ];
then
sudo /root/.dotnet/tools/dotnet-sonarscanner begin /o:"abcsoftware" \
/k:${{ parameters.projectKey }} \
/n:${{ parameters.projectName }} \
/d:sonar.java.source=17 \
/d:sonar.login=$SONAR_LOGIN_TOKEN \
/d:sonar.host.url="https://sonarcloud.io" \
/d:sonar.exclusions="**/Migrations/**" /d:sonar.verbose="true" \
/d:sonar.pullrequest.provider=vsts \
/d:sonar.pullrequest.key=$PR_KEY \
/d:sonar.pullrequest.branch=${PR_BRANCH:11} \
/d:sonar.pullrequest.base=${PR_BASE:11} \
/d:sonar.cs.opencover.reportsPaths="**/*.opencover.xml" \
/d:sonar.coverage.exclusions="**/obj/,**/bin/"
elif [ $(Build.SourceBranchName) = 'master' ]
then
sudo /root/.dotnet/tools/dotnet-sonarscanner begin /o:"abcsoftware" \
/k:${{ parameters.projectKey }} \
/n:${{ parameters.projectName }} \
/d:sonar.java.source=17 \
/d:sonar.login=$SONAR_LOGIN_TOKEN \
/d:sonar.host.url="https://sonarcloud.io" \
/d:sonar.exclusions="**/Migrations/**" /d:sonar.verbose="true" \
/d:sonar.cs.opencover.reportsPaths="**/*.opencover.xml" \
/d:sonar.coverage.exclusions="**/obj/,**/bin/"
else
sudo /root/.dotnet/tools/dotnet-sonarscanner begin /o:"abcsoftware" \
/k:${{ parameters.projectKey }} \
/n:${{ parameters.projectName }} \
/d:sonar.java.source=17 \
/d:sonar.login=$SONAR_LOGIN_TOKEN \
/d:sonar.host.url="https://sonarcloud.io" \
/d:sonar.exclusions="**/Migrations/**" /d:sonar.verbose="true" \
/d:sonar.branch.name=$CI_BRANCH \
/d:sonar.branch.target=$CI_BRANCH \
/d:sonar.cs.opencover.reportsPaths="**/*.opencover.xml" \
/d:sonar.coverage.exclusions="**/obj/,**/bin/"
fi
sudo dotnet build ${{ parameters.buildProjectFile }} -c Release -o /app/build
sudo dotnet test --collect:"XPlat Code Coverage" ${{ parameters.testProjectFile }} \
-- DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=opencover
sudo /root/.dotnet/tools/dotnet-sonarscanner end /d:sonar.login=$SONAR_LOGIN_TOKEN
env:
PR_BRANCH: $(System.PullRequest.SourceBranch)
PR_BASE: $(System.PullRequest.TargetBranch)
PR_KEY: $(System.PullRequest.PullRequestId)
CI_BRANCH: $(Build.SourceBranchName)
SONAR_LOGIN_TOKEN: $(SonarLoginToken)
# JAVA_HOME: "/usr/lib/jvm/java-17-openjdk-amd64"
# PATH: "$JAVA_HOME_17_X64/bin:$PATH"
displayName: 'SonarCloud analysis and upload'
failOnStderr: true
............................
– The below error in the log indicate that it is stil using java 11, as its still complaining that it recognizes class file versions up to 55.0
13:50:15.347 Writing processing summary to /home/vsts/work/1/s/.sonarqube/out/ProjectInfo.log
Calling the SonarScanner CLI...
Executing file /root/.dotnet/tools/.store/dotnet-sonarscanner/6.0.0/dotnet-sonarscanner/6.0.0/tools/netcoreapp3.1/any/sonar-scanner-5.0.1.3006/bin/sonar-scanner
Args: -Dsonar.scanAllFiles=true -Dproject.settings=/home/vsts/work/1/s/.sonarqube/out/sonar-project.properties --from=ScannerMSBuild/6.0 --debug <sensitive data removed>
Working directory: /home/vsts/work/1/s
Timeout (ms):-1
Process id: 2208
Error: LinkageError occurred while loading main class org.sonarsource.scanner.cli.Main
java.lang.UnsupportedClassVersionError: org/sonarsource/scanner/cli/Main has been compiled by a more recent version of the Java Runtime (class file version 61.0), this version of the Java Runtime only recognizes class file versions up to 55.0
Process returned exit code 1
The SonarScanner did not complete successfully
13:50:16.63 Post-processing failed. Exit code: 1
##[debug]Exit code 1 received from tool '/usr/bin/bash'
##[debug]STDIO streams have closed for tool '/usr/bin/bash'
##[error]Bash exited with code '1'.
##[debug]Processed: ##vso[task.issue type=error;]Bash exited with code '1'.
##[error]Bash wrote one or more lines to the standard error stream.
##[debug]Processed: ##vso[task.issue type=error;]Bash wrote one or more lines to the standard error stream.
##[error]+ java -version