Reputation: 41450
I am trying to run Maven using the Maven wrapper rather than the Maven task. However, it's failing because it is using an older version of Java. The JavaInstaller task seems to require a remote source for the JDK, I would rather avoid doing that and use the one that works with Maven task, but I can't find it documented anywhere.
Upvotes: 45
Views: 60751
Reputation: 109
Possible Solutions:
In building Pipeline :
Install as machine environment:
(1) Example with java archive from repository:
# Acquire a specific version of Java from a user-supplied Azure blob or the tool cache and sets JAVA_HOME.
# Does not extract if already extracted at destination directory
- task: JavaToolInstaller@0
enabled: true
inputs:
versionSpec: '11' # string. Required. JDK version. Default: 8.
jdkArchitectureOption: 'x64' # 'x64' | 'x86'. Required. JDK architecture.
jdkSourceOption: 'LocalDirectory' # 'AzureStorage' | 'LocalDirectory' | 'PreInstalled'. Required. JDK source.
jdkFile: '$(Build.SourcesDirectory)\Tools\Java\jdk-11\jdk-11.zip' # string. Required when jdkSourceOption == LocalDirectory. JDK file.
jdkDestinationDirectory: '$(Agent.ToolsDirectory)\Java\jdk-11' # string. Required when jdkSourceOption != PreInstalled. Destination directory.
cleanDestinationDirectory: false # boolean. Optional. Use when jdkSourceOption != PreInstalled. Clean destination directory. Default: true.
createExtractDirectory: false # boolean. Optional. Use when jdkSourceOption != PreInstalled. Create directory for extracting. Default: true.
(2) Example with java tool already extracted from repository:
# Java tool environment installer
- task: PowerShell@2
displayName: 'Install Java tool environment'
enabled: true
inputs:
targetType: 'inline'
script: |
$javahome = "$(Build.SourcesDirectory)\Tools\Java\jdk-11"
Write-Host "Setting JAVA_HOME to '$javahome'."
Write-Host "##vso[task.setvariable variable=JAVA_HOME]$javahome"
Write-Host "Adding JAVA_HOME to PATH:"
Write-Host "##vso[task.prependpath]$javahome\bin"
$env:Path -replace ';', "`n"
(3) For example on a windows machine
Upvotes: 0
Reputation: 2177
Just FYI, this is what I used for including java 17 in my azure pipeline script: - task: JavaToolInstaller@0 inputs: versionSpec: '17' jdkArchitectureOption: 'x64' jdkSourceOption: 'PreInstalled' displayName: 'Installing Java 17'
Upvotes: 0
Reputation: 8090
Just like already mentioned by Martin Kreidenweis, JavaToolInstaller can be used.
When this is used however on self-hosted agent, Java needs to be installed on the agent(s) and the required environment variable needs to be set to point to the installation directory.
JavaToolInstaller uses an environment variable derived from its configuration. Convention:
JAVA_HOME_${versionSpec}_${jdkArchitectureOption}
The environment variable can we set in the agent's home directory in the .env
file like this:
JAVA_HOME_17_x64=/usr/lib/jvm/temurin-17-jdk-amd64
After editing .env
, the agent needs to be restarted to make the environment variable available for the pipeline. This can be done via (agent home):
./svc.sh stop
./svc.sh start
See Azure documentation.
After that a step can be added like:
- task: JavaToolInstaller@0
inputs:
versionSpec: '17'
jdkArchitectureOption: 'x64'
jdkSourceOption: 'PreInstalled'
Upvotes: 7
Reputation: 855
You can now also use the JavaToolInstaller
task to activate one of the pre-installed Java versions, e.g.
- task: JavaToolInstaller@0
inputs:
versionSpec: '11'
jdkArchitectureOption: 'x64'
jdkSourceOption: 'PreInstalled'
See documentation at: https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/tool/java-tool-installer?view=azure-devops
It will also set JAVA_HOME
and prepend the PATH
, see source:
https://github.com/microsoft/azure-pipelines-tasks/blob/46cca412451ac4418d6332114fca8ef8c3095de1/Tasks/JavaToolInstallerV0/javatoolinstaller.ts#L80
Upvotes: 76
Reputation: 1107
The Java version to be used can be set via env
field of the task for Linux or macOS:
- script: |
java -version
env:
JAVA_HOME: $(JAVA_HOME_8_X64)
PATH: $(JAVA_HOME_8_X64)/bin:$(PATH)
and for Windows, change the colon in PATH to semicolon:
- script: |
java -version
env:
JAVA_HOME: $(JAVA_HOME_8_X64)
PATH: $(JAVA_HOME_8_X64)/bin;$(PATH)
Alternatives of Java version include:
JAVA_HOME_7_X64
vs2017-win2016
, windows-2019
macos-10.14
, macos-10.15
ubuntu-16.04
, ubuntu-18.04
JAVA_HOME_8_X64
vs2017-win2016
, windows-2019
macos-10.14
, macos-10.15
ubuntu-16.04
, ubuntu-18.04
, ubuntu-20.04
JAVA_HOME_11_X64
vs2017-win2016
, windows-2019
macos-10.14
, macos-10.15
ubuntu-16.04
, ubuntu-18.04
, ubuntu-20.04
JAVA_HOME_12_X64
macos-10.14
, macos-10.15
ubuntu-16.04
, ubuntu-18.04
JAVA_HOME_13_X64
vs2017-win2016
, windows-2019
macos-10.14
, macos-10.15
JAVA_HOME_14_X64
macos-10.14
, macos-10.15
Upvotes: 24
Reputation: 41450
Add the following script
before you run Maven for Unix based agents
- script: |
echo "##vso[task.setvariable variable=JAVA_HOME]$(JAVA_HOME_11_X64)"
echo "##vso[task.setvariable variable=PATH]$(JAVA_HOME_11_X64)/bin:$(PATH)"
displayName: "Set java version"
For Windows based agents
- script: |
echo "##vso[task.setvariable variable=JAVA_HOME]$(JAVA_HOME_11_X64)"
echo "##vso[task.setvariable variable=PATH]$(JAVA_HOME_11_X64)\bin;$(PATH)"
displayName: "Set java version"
This part of the pipeline code shows how the JAVA_HOME value is selected: https://github.com/microsoft/azure-pipelines-tasks/blob/master/Tasks/Common/java-common/java-common.ts
Upvotes: 16