Building a Jenkins shared library becomes so much easier if you work on it in an IDE. Especially when writing Groovy classes, you will want features like auto-completion, easy navigation between classes and compilation support. IntelliJ does a great job of deriving the project setup from a build definition.
Listing 1 shows a sample Maven build script. Pointing IntelliJ to the build script when opening the project will automatically derive the source directories, set up the proper JDK version and configure the Groovy compiler. Please note that the source directory conventions of a shared library does not follow the standard Maven conventions and therefore has to be reconfigured.
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.bmuschko.jenkins</groupId>
<artifactId>jenkins-shared-lib</artifactId>
<name>jenkins-shared-lib</name>
<version>1.0.0</version>
<properties>
<jdk.version>8</jdk.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<groovy.cps.version>1.30</groovy.cps.version>
<groovy.version>2.4.12</groovy.version>
</properties>
<dependencies>
<dependency>
<groupId>com.cloudbees</groupId>
<artifactId>groovy-cps</artifactId>
<version>${groovy.cps.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy</artifactId>
<version>${groovy.version}</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<compilerId>groovy-eclipse-compiler</compilerId>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-compiler</artifactId>
<version>3.5.0-01</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-batch</artifactId>
<version>2.5.8-02</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
Listing 1. Building a shared library with Maven
I tried to locate the compatible Jenkins versions used to compile and parse a Jenkins pipeline. The only hint I could find was under Manage Jenkins > About Jenkins. For my version of Jenkins, the Maven GAV is org.codehaus.groovy:groovy-all:2.4.12
. In the build script for your shared library, you should rely on that exact version to ensure optimal version compatibility. You will also get a hint about the compatible Groovy version by looking at the parent POM of the dependency com.cloudbees:groovy-cps
.
There’s nothing speaking against setting up a similar build with Gradle. While the syntax is completely different, the essence of the configuration would be the same. In a nutshell, apply the Groovy plugin, declare the relevant dependencies and reconfigure the source directory. There’s no need to configure the Groovy compiler explicitly. The Groovy plugin already takes care of it.