Skip to content

Latest commit

 

History

History
98 lines (77 loc) · 3.88 KB

IDE.md

File metadata and controls

98 lines (77 loc) · 3.88 KB

Setup IntelliJ IDEA for Jenkins

Start a new project

It is best to start anew:

  1. Select File | New Project
  2. Select Gradle
  3. Select Java AND Groovy Screeshot
  4. Choose GroupId and ArtifactId Screeshot
  5. Enter path to Gradle. For Gradle on Mac installed via Homebrew, the Gradle home is like this: Screeshot
  6. Choose Project name and Project Location Screeshot
  7. Finish Screeshot

Configure IDEA

Set up for Jenkins Plugins files which are of types .hpi or .jpi.

  1. Select IntelliJ IDEA | Preferences | Editor | File Types
  2. Select Archive
  3. Select + at the bottom left corner
  4. Add both .hpi and .jpi
  5. Select OK

Screeshot

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.

Setup for Jenkins pipeline library

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']
        }
    }
}