How to use 'tools' Directive in Jenkins pipeline to build multiple JDK versions in same stage

47 views Asked by At

I receive a file like the following:

"BUILD_DEPENDECIES": [
    {
      "DIR": "dep-a",
      "WARFILE": "",
      "JDK": "17"
    },
    {
      "DIR": "dep-b",
      "WARFILE": "",
      "JDK": "8"
    },
    {
      "DIR": "dep-c",
      "WARFILE": "",
      "JDK": "8"
    },
    {
      "DIR": "dep-d",
      "WARFILE": "",
      "JDK": "17"
    }
]

In my Jenkinsfile, I am attempting something similar to the following approach:

pipeline {
    agent any
    
    stages {
        stage ('Maven Build') {
            tools { jdk "jdk8"
                    jdk "jdk17" }
            
            steps {
                echo "## BUILD STAGE ##"
                
                script {

                    def mvnHome = tool "Maven"
                     
                    def buildWithJDK = { project ->
                        def jdkVersion = project.JDK ?: "jdk8"

                        if (jdkVersion == "jdk8") {
                            tool "jdk8"
                        } else if (jdkVersion == "jdk17") {
                            tool "jdk17"
                        }

                        dataApp.build(mvnHome, WORKSPACE, project.DIR)
                    }
                    configFile.BUILD_DEPENDECIES.each { project ->
                        buildWithJDK(project)
                    }
                }     
            }
        }
    }
}

I have read several articles here suggesting correct solutions, but all in different stages. Moreover, I am not sure if two versions of JDK can be declared using 'tools'.

0

There are 0 answers