简体   繁体   中英

Reuse maven step in Jenkins Job DSL

I am using the Job DSL to define a job that requires multiple maven steps to be run. This is an example:

def mavenInst = 'maven-3x'
job('test') {
  steps{           
    maven {
      mavenInstallation(mavenInst)       
      goals('fuu')
    }
    maven {
      mavenInstallation(mavenInst)       
      goals('bar')
    }
    // more steps of the same form.
    maven {
      mavenInstallation(mavenInst)       
      goals('fuu bar')
    }
  }
}

So, much of code is repeated quite often.

Is it possible to extract the respective parts of the job description and call them from within the Job DSL? I imagine something like this:

def mavenInst = 'maven-3x'
job('test') {
  steps{                  
    myCustomStep('fuu')
    myCustomStep('bar')
    // more steps of the same form.
    myCustomStep('fuu bar')
  }
}

This would result in significantly less code and would be easier to change in the future.

I have read that the steps need some sort of context but I can not figure out how to do it. I tried to extract the block into a configure closure as this:

def fuubar = { it -> 
  mavenInstallation(mavenInst)
  goals('fuu bar')
}

But when I call the element with configure fuubar , nothing is shown in the resulting job configure.xml.

Any help would be appreciated.

I would think you can do this

job('test') {
    steps {
        maven {
            mavenInstallation('maven-3x')     
            goals('fuu')
            goals('bar')
            goals('fuu bar')
        }
    }
}

based on the excellent help

and also in the dsl playground

which gives you this

<!-- 1. test -->
<project>
    <actions></actions>
    <description></description>
    <keepDependencies>false</keepDependencies>
    <properties></properties>
    <scm class='hudson.scm.NullSCM'></scm>
    <canRoam>true</canRoam>
    <disabled>false</disabled>
    <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
    <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
    <triggers class='vector'></triggers>
    <concurrentBuild>false</concurrentBuild>
    <builders>
        <hudson.tasks.Maven>
            <targets>fuu bar fuu bar</targets>
            <mavenName>maven-3x</mavenName>
            <jvmOptions></jvmOptions>
            <usePrivateRepository>false</usePrivateRepository>
        </hudson.tasks.Maven>
    </builders>
    <publishers></publishers>
    <buildWrappers></buildWrappers>
</project>

EDIT

Rereading your question it seems this is an example.

The above could also be written

def goal_names = ['fuu', 'bar', 'fuu bar']

job('test') {
        steps {
            maven {
              mavenInstallation('maven-3x')     
              goal_names.each { goal ->
                goals(goal)
              }
            }
        }
    }

EDIT 2 to use separate steps

def goal_names = ['fuu', 'bar', 'fuu bar']
job('test') {
  steps {
    goal_names.each { goal ->
      maven {
        mavenInstallation('maven-3x')     
        goals(goal)
      }
    }
  }
}

XML

<!-- 1. test -->
<project>
    <actions></actions>
    <description></description>
    <keepDependencies>false</keepDependencies>
    <properties></properties>
    <scm class='hudson.scm.NullSCM'></scm>
    <canRoam>true</canRoam>
    <disabled>false</disabled>
    <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
    <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
    <triggers class='vector'></triggers>
    <concurrentBuild>false</concurrentBuild>
    <builders>
        <hudson.tasks.Maven>
            <targets>fuu</targets>
            <mavenName>maven-3x</mavenName>
            <jvmOptions></jvmOptions>
            <usePrivateRepository>false</usePrivateRepository>
        </hudson.tasks.Maven>
        <hudson.tasks.Maven>
            <targets>bar</targets>
            <mavenName>maven-3x</mavenName>
            <jvmOptions></jvmOptions>
            <usePrivateRepository>false</usePrivateRepository>
        </hudson.tasks.Maven>
        <hudson.tasks.Maven>
            <targets>fuu bar</targets>
            <mavenName>maven-3x</mavenName>
            <jvmOptions></jvmOptions>
            <usePrivateRepository>false</usePrivateRepository>
        </hudson.tasks.Maven>
    </builders>
    <publishers></publishers>
    <buildWrappers></buildWrappers>
</project>

I managed to tackle that since I got similar issue and I wanted to have reusable methods with customization. I am not sure whether it is possible to encapsulate multiples steps in a method but you can encapsulate closure like this:

Closure runInLatestInstallation(String moduleName, String mavenGoals) {
    return {
        rootPOM("${moduleName}/pom.xml")
        goals(mavenGoals)
        mavenInstallation('Latest')
    }
}

and you can call it like this:

maven runInLatestInstallation(moduleName, 'versions:update-parent')

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