Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 565
Archimedes Trajano Avatar asked Apr 09 '20 15:04

Archimedes Trajano


People also ask

Does Azure DevOps use Java?

Azure DevOps Services Each commit can automatically build at GitHub and deploy to an Azure App Service. You can use whatever runtime you prefer, Tomcat, or Java SE. For more information, see Java for Azure App Service.


2 Answers

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://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

like image 94
Martin Kreidenweis Avatar answered Sep 20 '22 11:09

Martin Kreidenweis


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

like image 41
Archimedes Trajano Avatar answered Sep 18 '22 11:09

Archimedes Trajano