DEV Community

Jean-François Lamy
Jean-François Lamy

Posted on • Edited on

4 1

Compiling Java 17 using Maven in an Azure DevOps Pipeline

Using ubuntu-latest machines in an Azure DevOps pipeline, there is currently no support for versions beyond 11. So using a Maven@3 task to compile using a newer version fails. The workaround is to use a container version of Maven.

jobs:
  # build uberjar
  - job: ${{ parameters.jobName }}
    pool:
      vmImage: ubuntu-latest
    container: maven:3.8.1-openjdk-17-slim
    variables:
      - name: JAVA_HOME_11_X64
        value: /usr/local/openjdk-17
    steps:
      - template: steps-prepare-maven.yml
      - task: Maven@3
        displayName: build ${{ parameters.moduleName }} uberJar 
        inputs:
          mavenPomFile: pom.xml
          mavenOptions: -Xmx3072m $(MavenOpts)
          javaHomeSelection: 'path'
          jdkDirectory: '/usr/local/openjdk-17'
          publishJUnitResults: true
          testResultsFiles: "**/surefire-reports/TEST-*.xml"
          effectivePomSkip: true
          goals: -P production -pl ${{ parameters.moduleName }} -am $(MavenOpts) -Dmaven.test.skip=${{ parameters.skipTests }} $(BuildGoal)
Enter fullscreen mode Exit fullscreen mode

We are using openjdk-17 and explicitly using javaHomeSelection and jdkDirectory to set our JAVA_HOME to match where the container puts the JDK.

So why are we also defining the environment variable JAVA_HOME_11_X64, you ask? Well, I have reusable templates that are used outside the container setting. For example, the steps-prepare-maven.yml file is a template that creates a settings.xml file using the project secrets and updates the revision based on my build parameters - I also use it on Windows jobs. Such steps use the default version, Java 11. When running inside the container, the JAVA_HOME_11_X64 takes precedence and the Maven steps in the reusable templates work correctly.

This is extracted from a template file, replace the parameters with what you need.

Credit: sfragata's answer to a github issue got me started. Then I stumbled around until I could fix my included templates using the environment variable.

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay