简体   繁体   中英

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.

You can now also use the JavaToolInstaller task to activate one of the pre-installed Java versions, eg

- task: JavaToolInstaller@0
  inputs:
    versionSpec: '11'
    jdkArchitectureOption: 'x64'
    jdkSourceOption: 'PreInstalled'

See documentation at: https://docs.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

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

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

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM