Search code examples
javajenkinsintellij-ideaautocompletesyntax-highlighting

Jenkinsfile syntax highlighting in Java project using IntelliJ IDEA


We already tried the approaches as listed below:

After having searched the web for many hours on multiple days, we still haven't found a helpful resource on this matter. Thus, it appears to make sense to ask a new question here.

We are developing our Java projects in IntelliJ idea and want to integrate our builds with Jenkins. When we create a Jenkinsfile in Idea, we do not get syntax highlighting or auto completion. Since we are new to Jenkins, those features would be really useful to us. How can we make Idea be more supportive with Jenkinsfiles?

If there is no way to get syntax highlighting and auto completion for a Jenkinsfile in IntelliJ IDEA, what other editors would be helpful?


Please note:

  • we are working with Java projects, not Groovy projects.

  • We've already tried the plugin https://github.com/oliverlockwood/jenkinsfile-idea-plugin. When the plugin is activated, the Jenkinsfile is recognized as such, but instead of syntax highlighting we get an error message, please see below.

     pipeline {
     agent { docker 'maven:3.3.3' }
     stages {
         stage('build') {
             steps {
                 sh 'echo Hello, World!'
             }
         }
       }
     }
    

IntelliJ IDEA highlights the p of pipeline as error. The error message reads:

JenkinsTokenType.COMMENT, JenkinsTokenType.CRLF or
JenkinsTokenType.STEP_KEY expected, got 'p'

Thanks for any help!


Solution

  • At long last we found a solution that works for us and provides syntax highlighting and code completion for the Jenkinsfile present in an otherwise normal Java project in Idea. The solution is taken from here, here (and from additional personal experiments / research)

    1. Download the Groovy SDK (if you did not do so already) from the Groovy Page and configure it on your Java project. For help on this see here
    2. Download the pipeline GDSL file from your Jenkins instance which should be available under a link like https://yourJenkinsInstance.tld/pipeline-syntax/gdsl, and add it to the classpath of your Java project. E.g. by creating a new folder src/main/jenkins, putting the pipeline GDSL file there and marking the folder as source root in IntelliJ IDEA. Make sure that the file extension is .gdsl, not .groovy. Otherwise, code completion will not work.
    3. Add "Jenkinsfile" as a valid file name pattern for groovy files as described here
    4. To avoid the error message 'node' cannot be applied to '(groovy.lang.Closure<java.lang.Object>), you can add this line at the top of your Jenkinsfile:
      // noinspection GroovyAssignabilityCheck