Maddy
Maddy

Reputation: 705

Getting error on java version while running maven job from Azure Pipeline

I am running my job on a specific agent and using below commands

- script: |
    java -version
  env:
    JAVA_HOME: $(JAVA_HOME_11_X64)
    PATH: $(JAVA_HOME_11_X64)/bin:$(PATH)

Output

openjdk version "11.0.11" 2021-04-20
OpenJDK Runtime Environment (build 11.0.11+9-Ubuntu-0ubuntu2.18.04)
OpenJDK 64-Bit Server VM (build 11.0.11+9-Ubuntu-0ubuntu2.18.04, mixed mode, sharing)

But when the maven command runs it doesn't find java so what exactly should I pass here. openjdk 11.0.11 is present on agent and how to pass this in maven? I tried passing 1.8 and 11.0.11 but it given error for that too. How to solve this and what exactly should I pass here in maven task?

- task: Maven@3
  inputs:
    mavenPomFile: 'pom.xml'
    mavenOptions: '-Xmx3072m'
    javaHomeOption: 'JDKVersion'
    jdkVersionOption: '11'
    jdkArchitectureOption: 'x64'

Output

##[error]Unhandled: Failed to find the specified JDK version. Please ensure the specified JDK version is installed on the agent and the environment variable 'JAVA_HOME_11_X64' exists and is set to the location of a corresponding JDK or use the [Java Tool Installer](https://go.microsoft.com/fwlink/?linkid=875287) task to install the desired JDK.
Finishing: Maven



Upvotes: 1

Views: 3874

Answers (1)

Kevin Lu-MSFT
Kevin Lu-MSFT

Reputation: 35194

In Maven task, if you need to use JAVA 11 , you can try to set the jdkVersionOption to 1.11.

For example:

- task: Maven@3
  inputs:
    mavenPomFile: 'pom.xml'
    publishJUnitResults: true
    testResultsFiles: '**/surefire-reports/TEST-*.xml'
    javaHomeOption: 'JDKVersion'
    jdkVersionOption: '1.11'
    mavenVersionOption: 'Default'

From your description, JAVA 11 has been installed on your agent.

If this issue still exists, you can configure the Pipeline variable for Java 11.

You can add a Command Line task/ PowerShell task/ Bash task to run the following command.

- 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)"

In this case, the java parameter will be set to the Pipeline global variable

Upvotes: 1

Related Questions