Jenkins : Update maven jobs to use the post build task to deploy artifacts

This script updates all maven jobs having a deploy goal by install and activate the post build step to deploy artifacts at the end of the build
The script also overrides the parameter of the job to ensure you are archiving the artifacts (this is mandatory by the post build task).

import java.util.*
import hudson.model.*
import hudson.maven.*
import hudson.maven.reporters.*
import hudson.matrix.*
import hudson.tasks.*

hudson.model.Hudson.instance.items.findAll{job -> job.isBuildable() && job instanceof MavenModuleSet}.each {
job -> 
  println(job.name)
  if (job.goals.indexOf("deploy")>0) {
    println("* Must update goals (deploy -> install) : " + job.goals)
    def newGoals = job.goals.replaceAll("deploy", "install");
    println("* New goals : " + newGoals)
    // Comment the 3 following lines for a first run to check goals modifications without modifying anything
    job.goals = newGoals
    job.archivingDisabled = false // It is necessary to archive artifacts to deploy them after the build
    job.publishers.replace(new hudson.maven.RedeployPublisher(null, null, true, false));
    println("* Redeploy publisher added");
  } else {
    println("* This maven job doesn't deploy its artifacts");
  }
}