Archimedes Trajano
Archimedes Trajano

Reputation: 41450

How to you use a specific version of Java in Azure Devops Agent without downloading?

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

Answers (6)

bomtom
bomtom

Reputation: 109

Possible Solutions:

In building Pipeline :

  • (1) Azure install task that needs an java archive
  • (2) Use already extracted Java and set java_home and path in pipeline

Install as machine environment:

  • (3) provide a java copy and set system env path and java_home

(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

  • copy the jdk to C:\Java\Jdk11
  • set system environment variable JAVA_HOME = C:\Java\Jdk11
  • and extend environment varible PATH by C:\Java\Jdk11\bin (PATH=%JAVA_HOME%;%PATH%)
  • restart azure agent

Upvotes: 0

Stephen
Stephen

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

R. Oosterholt
R. Oosterholt

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

Martin Kreidenweis
Martin Kreidenweis

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

Xiangkun
Xiangkun

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
    • Available on Windows: vs2017-win2016, windows-2019
    • Avaiable on macOS: macos-10.14, macos-10.15
    • Available on Linux: ubuntu-16.04, ubuntu-18.04
  • JAVA_HOME_8_X64
    • Available on Windows: vs2017-win2016, windows-2019
    • Available on macOS: macos-10.14, macos-10.15
    • Available on Linux: ubuntu-16.04, ubuntu-18.04, ubuntu-20.04
  • JAVA_HOME_11_X64
    • Available on Windows: vs2017-win2016, windows-2019
    • Available on macOS: macos-10.14, macos-10.15
    • Available on Linux: ubuntu-16.04, ubuntu-18.04, ubuntu-20.04
  • JAVA_HOME_12_X64
    • Available on macOS: macos-10.14, macos-10.15
    • Available on Linux: ubuntu-16.04, ubuntu-18.04
  • JAVA_HOME_13_X64
    • Available on Windows: vs2017-win2016, windows-2019
    • Available on macOS: macos-10.14, macos-10.15
  • JAVA_HOME_14_X64
    • Available on macOS: macos-10.14, macos-10.15

Upvotes: 24

Archimedes Trajano
Archimedes Trajano

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

Related Questions