edbras
edbras

Reputation: 4404

How to install java 13 on Azure-pipeline Ubuntu agent and use it during Maven build?

I am trying to build a Java Maven application and run some tests against a Postgres db.

However, the java app uses JDK 13 and the Azure hosted agent ubuntu doesn't have this default installed. As such, I use the script task to install it, then use it during the Maven build.

However, I tried several config's and in all Maven keeps complaining that it can not found any JDK 13 installed.

The last config I tried is listed below, in which I install it through a script and then use the JavaToolInstaller task to make it available (ensuring the Java_home is set and the java can be found in the path. I then get the error

##[error]Java 13 is not preinstalled on this agent

I also tried it without the JavaToolInstaller task and then export JAVA_HOME and modify the PATH in the script, but then Maven complaints it can not find JDK 13...

Please some help how to use JDK 13 on an ubuntu agent during the maven build?

Azure pipeline snippet:

    variables:
    MAVEN_CACHE_FOLDER: $(Pipeline.Workspace)/.m2/repository
    MAVEN_OPTS: "-Dmaven.repo.local=$(MAVEN_CACHE_FOLDER)"
    JAVA_HOME : "/usr/lib/jvm/openjdk-13-jdk"
    PATH: $(JAVA_HOME)/bin:$(PATH)
    service_name: backend
    mygetUsername: myUserName
    mygetPassword: myPassword

resources:
    containers:
        - container: postgres
          image: postgres:11.6-alpine
          ports:
              - 5432:5432
          env:
              POSTGRES_DB: default
              POSTGRES_USER: default
              POSTGRES_PASSWORD: default
              POSTGRES_HOST_AUTH_METHOD: trust


stages:
    - stage: create_artifact
      displayName: Create artifact
      jobs:
          - job: build
            displayName: Build, test and publish artifact
            services:
                postgres: postgres
            steps:
                - script: |
                    sudo apt-get install openjdk-13-jdk  
                  displayName: Installing JDK 13

                - task: JavaToolInstaller@0
                  displayName: Using JDK 13
                  inputs:
                    versionSpec: "13"
                    jdkArchitectureOption: x64
                    jdkSourceOption: "PreInstalled"

                - task: Cache@2
                  displayName: Cache Maven local repo
                  inputs:
                      key: 'maven | "$(Agent.OS)" | backend/pom.xml'
                      restoreKeys: |
                          maven | "$(Agent.OS)"
                          maven
                      path: $(MAVEN_CACHE_FOLDER)
                - task: Maven@3
                  name: maven_package
                  displayName: Maven package
                  inputs:
                      goals: "package"
                      mavenPomFile: "backend/pom.xml"
                      options: '--settings backend/.mvn/settings.xml -DmygetUsername=$(mygetUsername) -DmygetPassword=$(mygetPassword)'
                      mavenOptions: "-Xmx3072m $(MAVEN_OPTS)"
                      javaHomeOption: "JDKVersion"
                      jdkVersionOption: "1.13"
                      mavenAuthenticateFeed: true

Upvotes: 2

Views: 3884

Answers (2)

Harshita Singh
Harshita Singh

Reputation: 4870

"PreInstalled" feature allows you to use Java versions that are pre-installed on the Microsoft-hosted agent. You can find available pre-installed versions of Java in Software section:

enter image description here

I think your script does not work on hosted machine somehow. And, the JDK version you specify is not present on your host machine. I would suggest you to set your Host machine as per the above table (as per pre-defined JDK installed).

Consequently, the other two options can be leveraged:

Here's an example of getting the archive file from a local directory on Linux. The file should be an archive (.zip, .gz) of the JAVA_HOME directory so that it includes the bin, lib, include, jre, etc. directories.

 - task: JavaToolInstaller@0
    inputs:
      versionSpec: "11"
      jdkArchitectureOption: x64
      jdkSourceOption: LocalDirectory
      jdkFile: "/builds/openjdk-11.0.2_linux-x64_bin.tar.gz"
      jdkDestinationDirectory: "/builds/binaries/externals"
      cleanDestinationDirectory: true

Here's an example of downloading the archive file from Azure Storage. The file should be an archive (.zip, .gz) of the JAVA_HOME directory so that it includes the bin, lib, include, jre, etc. directories.

- task: JavaToolInstaller@0
  inputs:
    versionSpec: '6'
    jdkArchitectureOption: 'x64'
    jdkSourceOption: AzureStorage
    azureResourceManagerEndpoint: myARMServiceConnection
    azureStorageAccountName: myAzureStorageAccountName
    azureContainerName: myAzureStorageContainerName
    azureCommonVirtualFile: 'jdk1.6.0_45.zip'
    jdkDestinationDirectory: '$(agent.toolsDirectory)/jdk6'
    cleanDestinationDirectory: false

Upvotes: 2

edbras
edbras

Reputation: 4404

The problem was that I was pointing to the wrong installed directory. The above location of JAVA_HOME I found on internet, after ouputing the tree content of the /usrl/lib/jvm I changed it and added this to the variable section and maven found the jdk and uses it 😊

JAVA_HOME_13_X64 : "/usr/lib/jvm/java-13-openjdk-amd64"

Upvotes: 0

Related Questions