Jenkinsfile syntax highlighting in Java project using Intellij Idea

rexford picture rexford · Dec 13, 2017 · Viewed 53.3k times · Source

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

    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!

Answer

Tom picture Tom · Jan 18, 2018

If you want IDEA to recognize a Jenkinsfile as a Groovy file, then you can add the String "Jenkinsfile" as a valid file name pattern (normally contains file endings) for Groovy files. This is supported "out of the box" without requiring any additional Plugin (except the "Groovy" Plugin, but that is already part of IDEA).

To do that go to the settings menu, open the "Editor" item and then "File Types". Now select "Groovy" in the upper list and add "Jenkinsfile". You can also use a regex like "Jenkinsfile*" if you want to be more flexible regarding an optional file ending for the Jenkinsfile.
The setting should now look like this: IDEA file type settings

Your example now looks like this in IDEA (with the Dracula theme): Jenkinsfile syntax highlight

So IDEA now provides syntax highlighting and auto completion as far as I can tell. It suggests existing function/method names while writing, but I'm not a Groovy developer, thus I can't tell if some suggestions are missing.