I have a pipeline for .net core project and i am trying to use SonarCloud for code analysis. My pipeline below is being run by a hosted agent. It complains about Java_Home. How can i get around this?
error
ERROR: JAVA_HOME not found in your environment, and no Java executable present in the PATH. Please set the JAVA_HOME variable in your environment to match the location of your Java installation, or add "java.exe" to the PATH
yaml
pool:
name: company-pool
variables:
BuildConfiguration: 'Release'
trigger:
branches:
include:
- main
steps:
- task: UseDotNet@2
displayName: 'Use .NET 6.x'
inputs:
version: 6.x
includePreviewVersions: true
- task: SonarCloudPrepare@1
displayName: 'Prepare analysis on SonarCloud'
inputs:
SonarCloud: SonarCloud
organization: myorg
projectKey: 'mysummary'
projectName: 'myproject'
- task: DotNetCoreCLI@2
displayName: 'dotnet restore'
inputs:
command: restore
projects: |
*src/Comapny.Web/Comapny.Web.csproj
- task: DotNetCoreCLI@2
displayName: 'Build projects'
inputs:
projects: |
*src/Comapny.Web/Comapny.Web.csproj
arguments: '--configuration $(BuildConfiguration)'
- task: SonarCloudAnalyze@1
displayName: 'Run Code Analysis'
inputs:
jdkversion: 'JAVA_HOME'
- task: SonarCloudPublish@1
displayName: 'Publish Quality Gate Result'
Based on the error message, the cause of the issue is that you haven't configured the JAVA environment in your Agent machine.
To solve this issue, you can JavaToolInstaller@0 to configure the Java Environment. But since you don’t have the Preinstalled java version on your local machine, you need to add steps to download it.
Here is an example:
Linux Machine:
Windows Machine:
Then the JAVA_HOME will be set on the self-hosted agent.