2

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?

Jatin
  • 31,116
  • 15
  • 98
  • 163
VKD
  • 633
  • 2
  • 12
  • 28
  • See this [answer](https://stackoverflow.com/a/72314866/1183010) to see how to be able to use multiple Java versions of your self hosted agents in different pipelines. – R. Oosterholt May 20 '22 at 07:48

2 Answers2

1

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
Walter
  • 2,640
  • 1
  • 5
  • 11
  • How to select the Java Version in case we are using Self Hosted Agent Server? – VKD Feb 10 '21 at 11:33
  • @Kattesang Sorry for the late. Please refer to my update in the answer. – Walter Feb 15 '21 at 08:34
  • How to do the same for gradle? – VKD Feb 15 '21 at 10:42
  • In my Self hosted agent, I have installed JDK 11 using msi. and have JDK 8 as zip file. I have used Java Tool Installer task to set the version as 8. Gradle wrapper properties has a line as "distributionUrl=https\://services.gradle.org/distributions/gradle-3.5-bin.zip". But getting error as "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] task to install the desired JDK." – VKD Feb 15 '21 at 11:35
  • @Kattesang Please see my updates in the answer. I didn't add any distributionUrl in the Gradle wrapper properties file. – Walter Feb 16 '21 at 09:19
  • @Kattesang Not get your response for several days, would you please share your latest progress about this issue? – Walter Feb 22 '21 at 02:10
1

The other answer requires the JDK binary to be present at a location. In its absence:

      - task: BASH@3
        displayName: 'install-java8'
        inputs:
          targetType: 'inline'
          script: |
            rm -rf /opt/jdk
            mkdir /opt/jdk
            wget https://github.com/adoptium/temurin8-binaries/releases/download/jdk8u322-b06/OpenJDK8U-jdk_x64_linux_hotspot_8u322b06.tar.gz
            tar -zxf OpenJDK8U-jdk_x64_linux_hotspot_8u322b06.tar.gz -C /opt/jdk
            export PATH=$PWD/jdk8u322-b06/bin:$PATH
            update-alternatives --install /usr/bin/java java /opt/jdk/jdk8u322-b06/bin/java 100 && \
            update-alternatives --install /usr/bin/javac javac /opt/jdk/jdk8u322-b06/bin/javac 100 && \
            update-alternatives --install /usr/bin/jar jar /opt/jdk/jdk8u322-b06/bin/jar 100 && \
            echo 'export JAVA_HOME=/opt/jdk/jdk8u322-b06/' >> /etc/profile.d/java.sh
            java -version                


      - task: BASH@3
        displayName: 'verify java8 installation'
        inputs:
          targetType: 'inline'
          script: |
            source /etc/profile.d/java.sh 
            java -version
            echo $JAVA_HOME
Jatin
  • 31,116
  • 15
  • 98
  • 163