It is best to start anew:
- Select File | New Project
- Select Gradle
- Select Java AND Groovy
- Choose GroupId and ArtifactId
- Enter path to Gradle. For Gradle on Mac installed via Homebrew, the Gradle home is like this:
- Choose Project name and Project Location
- Finish
Set up for Jenkins Plugins files which are of types .hpi or .jpi.
- Select IntelliJ IDEA | Preferences | Editor | File Types
- Select Archive
- Select + at the bottom left corner
- Add both .hpi and .jpi
- Select OK
Modify build.gradle to add the following lines.
compile 'org.jenkins-ci.main:jenkins-core:2.23'
// Jenkins plugins
compile group: 'org.jenkins-ci.plugins', name: 'credentials', version: '2.1.13', ext: 'jar'
compile group: 'org.jenkins-ci.plugins', name: 'matrix-auth', version: '1.6', ext: 'jar'
compile group: 'org.jenkins-ci.plugins.workflow', name: 'workflow-cps', version: '2.39', ext: 'jar'
// TRICKY: The lib folder contains all other plugins *JAR* files
// if not found in Maven
compile fileTree(dir: 'lib', include: ['*.jar'])
The above example will grab Jenkins core libraries, Matrix Authorization Plugin hpi, other plugin dependencies and javadocs for all imported libraries. Having these libraries imported will enable code auto-completion, syntax checks, easy refactoring when working with Groovy scripts for Jenkins. It will be a great productivity boost.
NOTE 1: The last line compile fileTree
is the last resort for any Jenkins plugins that you cannot find the right group ID and artifact ID.
It is rare these days but such cases cannot be completely ruled out.
NOTE 2: The ext: 'jar'
is VERY important to ensure that jar
files, instead of hpi
/jpi
files, are being downloaded and understood by IntellJ.
Without that ext
option specified, IntellJ won't find JAR files nested in hpi
/jpi
files which is the default binaries for Jenkins plugins.
The final build.gradle will look like this. All of the above setup should suffice for working with Groovy Init Scripts. For working with Jenkins Shared Pipeline Libraries, we should take one extra step shown as follows.
All Groovy files in Jenkins shared library for Pipelines have to follow this directory structure:
(root)
+- src # Groovy source files
| +- org
| +- foo
| +- Bar.groovy # for org.foo.Bar class
+- vars
| +- foo.groovy # for global 'foo' variable
| +- foo.txt # help for 'foo' variable
+- resources # resource files (external libraries only)
| +- org
| +- foo
| +- bar.json # static helper data for org.foo.Bar
Note that the Groovy code can be in both src
and vars
folders.
Therefore, you need to add the following lines in build.gradle
to inform Gradle locations of Groovy source codes:
sourceSets {
main {
groovy {
srcDirs = ['vars', 'src']
}
}
test {
groovy {
srcDirs = ['test/groovy']
}
}
}