Search code examples
javaazureazure-devopsazure-devops-self-hosted-agent

Azure DevOps Pipeline for Java Project with self hosted agent


We are having a set of Java Projects which were developed using different JDK versions, different versions of Gradle and Maven were used in projects.

We are supposed to create a Azure DevOps Pipeline using Self Hosted Agent and as of now build agent server was installed with JDK 11.

How to create pipeline to handle such variety of projects? Do we need to install multiple JDK versions in Self Hosted Agent or any other better way?


Solution

  • Yes, you need to install multiple JDK versions if you want to use Self Hosted Agent. The better way is to use Microsoft-hoseted agent because it has some versions of JDK pre-installed. You can refer to the document about Build environment and Build using multiple versions.

    Update:

    Here are my samples of Gradle with self-hosted agent:

    1.Use java tool install task:

    steps:
    - task: JavaToolInstaller@0
      inputs:
        versionSpec: '11'
        jdkArchitectureOption: 'x64'
        jdkSourceOption: 'LocalDirectory'
        jdkFile: 'C:\jdk-11.0.10.zip'
        cleanDestinationDirectory: false
    - task: Gradle@2
      inputs:
        gradleWrapperFile: 'gradlew'
        tasks: 'build'
        publishJUnitResults: false
        javaHomeOption: 'JDKVersion'
        jdkVersionOption: '1.11'
        gradleOptions: '-Xmx3072m'
        sonarQubeRunAnalysis: false
    - task: JavaToolInstaller@0
      inputs:
        versionSpec: '8'
        jdkArchitectureOption: 'x64'
        jdkSourceOption: 'LocalDirectory'
        jdkFile: 'C:\jdk1.8.0_281.zip'
        cleanDestinationDirectory: false
    - task: Gradle@2
      inputs:
        gradleWrapperFile: 'gradlew'
        tasks: 'build'
        publishJUnitResults: false
        javaHomeOption: 'JDKVersion'
        jdkVersionOption: '1.11'
        gradleOptions: '-Xmx3072m'
        sonarQubeRunAnalysis: false
    

    JDK file of java tool install task:

    Applicable when jdkSourceOption == LocalDirectory. Specify the path to the jdk archive file that contains the compressed JDK. The path could be in your source repository or a local path on the agent. The file should be an archive (.zip, .tar.gz, .7z), containing bin folder either on the root level or inside a single directory.

    2.Use gradle task directly:

    steps:
    - task: Gradle@2
      inputs:
        gradleWrapperFile: 'gradlew'
        tasks: 'build'
        publishJUnitResults: false
        javaHomeOption: 'Path'
        jdkDirectory: 'C:\Program Files\Java\jdk-11.0.10'
        gradleOptions: '-Xmx3072m'
        sonarQubeRunAnalysis: false
    
    - task: Gradle@2
      inputs:
        gradleWrapperFile: 'gradlew'
        tasks: 'build'
        publishJUnitResults: false
        javaHomeOption: 'Path'
        jdkDirectory: 'C:\Program Files\Java\jdk1.8.0_281'
        gradleOptions: '-Xmx3072m'
        sonarQubeRunAnalysis: false