6

One step in my Azure DevOps pipeline requires Java to be installed on the agent.

I found the "Java Tool Installer" task here:

https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/tool/java-tool-installer?view=azure-devops

This looks, however, more like a SDK installer. I only need a Java runtime environment. I am looking for something like the Python installer task:

steps:
- task: UsePythonVersion@0
  inputs:
    versionSpec: '3.6'

Is there anything for Java getting close to this?

hey
  • 2,643
  • 7
  • 29
  • 50
  • For others (like me) who were looking to install JDK8 on self-hosted agent: https://stackoverflow.com/a/71113705/894565 – Jatin Feb 14 '22 at 14:46

2 Answers2

4

Is there anything for Java getting close to this?

Test with the Python installer task, this task is used to specify a specific python version via setting the environment.

To achieve a similar purpose with Java, you could set the Java_Home and Path variable during the runtime.

You could add a powershell task at the first step.

Here is an example:

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      echo "##vso[task.setvariable variable=JAVA_HOME]$(JAVA_HOME_11_X64)"
      echo "##vso[task.setvariable variable=PATH]$(JAVA_HOME_11_X64)\bin;$(PATH)"

The $(JAVA_HOME_11_X64) variable is an environment variable.

You could check this variable with the script env | sort. Then the supported value will list in the output.

For example:

Java version

In this case, the JAVA_HOME variable will be set as the expected value.

enter image description here

Hope this helps.

Kevin Lu-MSFT
  • 20,786
  • 3
  • 19
  • 28
  • Ah, thanks! Great! I also did not get that several Java versions are preinstalled on the agents, and listed in https://learn.microsoft.com/en-us/azure/devops/pipelines/agents/hosted?view=azure-devops&tabs=yaml – hey Jun 25 '20 at 19:27
2

Since Azure supports Docker, I would simply go for docker:

trigger:
- main

pr:
- main
- releases/*

pool:
  vmImage: 'ubuntu-20.04'

container: adoptopenjdk:latest

steps:
- script: ./gradlew check
koppor
  • 19,079
  • 15
  • 119
  • 161