简体   繁体   中英

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM