Child pages
  • Add a Maven Installation, Tool Installation, Modify System Config
Skip to end of metadata
Go to start of metadata

Jenkins system configuration is not available via REST endpoints, this is a snippet to modify it:

a=Jenkins.instance.getExtensionList(hudson.tasks.Maven.DescriptorImpl.class)[0];
b=(a.installations as List);
b.add(new hudson.tasks.Maven.MavenInstallation("MAVEN3", "/home/apache-maven-3", []));
a.installations=b
a.save()
  • No labels

2 Comments

  1. ... and for those that what an auto installer...

    import jenkins.model.*
    import hudson.tasks.Maven.MavenInstallation
    import hudson.tasks.Maven.MavenInstaller
    import hudson.tools.InstallSourceProperty
    def extensions = Jenkins.instance.getExtensionList(hudson.tasks.Maven.DescriptorImpl.class)[0]
    def installations = (extensions.installations as List)
    def mavenAutoInstaller = new MavenInstaller('default')
    def installProperty = new InstallSourceProperty([mavenAutoInstaller])
    def autoInstallation = new MavenInstallation('default', null, [installProperty])
    installations.add(autoInstallation)
    extensions.installations = installations
    extensions.save()

    1. Here's a slightly more complete example, which is suitable for use as an init.d script. It only adds an installation if one is not already found, and allows you to specify the version to be installed.

      // 
      // Configure Tool - Maven
      // 
      // Installs a Maven installation named "maven-3" if one is not already found.
      // References:
      // * https://github.com/batmat/jez/blob/master/jenkins-master/init_scripts/add_maven_auto_installer.groovy
      // * https://wiki.jenkins-ci.org/display/JENKINS/Add+a+Maven+Installation%2C+Tool+Installation%2C+Modify+System+Config
      // * https://github.com/glenjamin/jenkins-groovy-examples/blob/master/README.md
      // These are the basic imports that Jenkin's interactive script console 
      // automatically includes.
      import jenkins.*;
      import jenkins.model.*;
      import hudson.*;
      import hudson.model.*;
      mavenName = "maven-3"
      mavenVersion = "3.3.9"
      println("Checking Maven installations...")
      // Grab the Maven "task" (which is the plugin handle).
      mavenPlugin = Jenkins.instance.getExtensionList(hudson.tasks.Maven.DescriptorImpl.class)[0]
      // Check for a matching installation.
      maven3Install = mavenPlugin.installations.find

      Unknown macro: { install -> install.name.equals(mavenName) }

      // If no match was found, add an installation.
      if(maven3Install == null) { println("No Maven install found. Adding...")
           newMavenInstall = new hudson.tasks.Maven.MavenInstallation('maven-3', null,  [new hudson.tools.InstallSourceProperty([new hudson.tasks.Maven.MavenInstaller(mavenVersion)])] ) mavenPlugin.installations += newMavenInstall mavenPlugin.save() println("Maven install added.")
      } else { println("Maven install found. Done.")
      }

      //
      // Configure Tool - Maven
      //
      // Installs a Maven installation named "maven-3" if one is not already found.
      // References:
      // * https://github.com/batmat/jez/blob/master/jenkins-master/init_scripts/add_maven_auto_installer.groovy
      // * https://wiki.jenkins-ci.org/display/JENKINS/Add+a+Maven+Installation%2C+Tool+Installation%2C+Modify+System+Config
      // * https://github.com/glenjamin/jenkins-groovy-examples/blob/master/README.md
      
      
      // These are the basic imports that Jenkin's interactive script console
      // automatically includes.
      import jenkins.*;
      import jenkins.model.*;
      import hudson.*;
      import hudson.model.*;
      
      mavenName = "maven-3"
      mavenVersion = "3.3.9"
      println("Checking Maven installations...")
      
      // Grab the Maven "task" (which is the plugin handle).
      mavenPlugin = Jenkins.instance.getExtensionList(hudson.tasks.Maven.DescriptorImpl.class)[0]
      
      // Check for a matching installation.
      maven3Install = mavenPlugin.installations.find {
      	install -> install.name.equals(mavenName)
      }
      
      // If no match was found, add an installation.
      if(maven3Install == null) {
      	println("No Maven install found. Adding...")
      
      	newMavenInstall = new hudson.tasks.Maven.MavenInstallation('maven-3', null,
      		[new hudson.tools.InstallSourceProperty([new hudson.tasks.Maven.MavenInstaller(mavenVersion)])]
      	)
      
      	mavenPlugin.installations += newMavenInstall
      	mavenPlugin.save()
      
      	println("Maven install added.")
      } else {
      	println("Maven install found. Done.")
      }