使用 Intellij Idea 在 Java 项目中突出显示 Jenkinsfile 语法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47796757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 02:40:56  来源:igfitidea点击:

Jenkinsfile syntax highlighting in Java project using Intellij Idea

javajenkinsintellij-ideaautocompletesyntax-highlighting

提问by rexford

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?

我们正在 IntelliJ 理念中开发我们的 Java 项目,并希望将我们的构建与 Jenkins 集成。当我们在 Idea 中创建 Jenkinsfile 时,我们不会获得语法高亮或自动完成。由于我们是 Jenkins 的新手,这些功能对我们来说非常有用。我们如何让 Idea 更支持 Jenkinsfiles?

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

如果没有办法让 Jenkinsfile 的语法高亮显示和自动完成,还有哪些其他编辑器会有所帮助?

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'

  • 我们正在使用 java 项目,而不是 groovy 项目。
  • 我们已经尝试过插件https://github.com/oliverlockwood/jenkinsfile-idea-plugin。当插件被激活时,Jenkinsfile 被识别为这样,但是我们收到一条错误消息而不是语法突出显示,请参见下文。

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

    Idea 将“管道”的“p”突出显示为错误。错误信息如下:

    JenkinsTokenType.COMMENT、JenkinsTokenType.CRLF 或 JenkinsTokenType.STEP_KEY 预期,得到 'p'

Thanks for any help!

谢谢你的帮助!

采纳答案by Tom

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).

如果您希望 IDEA 将 Jenkinsfile 识别为 Groovy 文件,那么您可以添加字符串“Jenkinsfile”作为 Groovy 文件的有效文件名模式(通常包含文件结尾)。这是“开箱即用”的支持,不需要任何额外的插件(“Groovy”插件除外,但它已经是 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

为此,请转到设置菜单,打开“编辑器”项目,然后打开“文件类型”。现在选择上方列表中的“Groovy”并添加“Jenkinsfile”。如果您想更灵活地处理 Jenkinsfile 的可选文件结尾,您也可以使用像“Jenkinsfile*”这样的正则表达式。
设置现在应如下所示: IDEA 文件类型设置

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

您的示例现在在 IDEA 中看起来像这样(使用 Dracula 主题): Jenkinsfile 语法高亮

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.

因此,据我所知,IDEA 现在提供了语法高亮和自动完成功能。它在编写时建议现有的函数/方法名称,但我不是 Groovy 开发人员,因此我无法判断是否缺少某些建议。

回答by rexford

Below is the feedback we've got from IntelliJ. We will follow up on this if we are able to provide a working solution.

以下是我们从IntelliJ得到的反馈。如果我们能够提供可行的解决方案,我们将对此进行跟进。

Andrey Dernov, IntelliJ, Dec 14, 07:50 CET

Andrey Dernov,IntelliJ,欧洲中部时间 12 月 14 日 07:50

Hello Georg, there is no special Editor intellisense support for editing Jenkinsfiles in IDE. You will need to write a custom language plugin for this.

你好 Georg,在 IDE 中编辑 Jenkinsfiles 没有特殊的编辑器智能支持。您需要为此编写自定义语言插件。

Or you can consider implementing such support via writing Kotlin DSL. Some examples to start with.

或者您可以考虑通过编写 Kotlin DSL 来实现此类支持。一些例子开始。

回答by Neil Millard

Looking at the source code, it looks like COMMENTS are not defined (they are commented out in the code)

看源码,好像没有定义COMMENTS(在代码中注释掉了)

The STEP_KEY is defined as: STEP_NAME="sh" | "parallel"

STEP_KEY 定义为: STEP_NAME="sh" | “平行线”

I'm not sure the plugin does much more and it hasn't been updated in 2 years.

我不确定该插件能做的更多,而且它已经 2 年没有更新了。

回答by futchas

Another option is to use a shabang on top of the Jenkinsfile like this #!/usr/bin/env groovy. Also you can try out gdsl: https://st-g.de/2016/08/jenkins-pipeline-autocompletion-in-intellijbut so far it doesn't support declarative pipelines: https://issues.jenkins-ci.org/browse/JENKINS-40127

另一种选择是像这样在 Jenkinsfile 之上使用 shabang #!/usr/bin/env groovy。你也可以尝试GDSL:https://st-g.de/2016/08/jenkins-pipeline-autocompletion-in-intellij但到目前为止,它不支持声明管道:HTTPS://issues.jenkins-ci .org/browse/JENKINS-40127

回答by Josh M.

Use shlike this and the error should go away (worked for me)...

sh像这样使用,错误应该消失(对我有用)......

steps {
    sh """
        echo 'Hello, World!'
    """
}

回答by Kamil Wróbel

If you add

如果添加

#!groovy?

header to your jenkinsfile then you should get groovy syntax highlighting in IDE.

标题到您的 jenkinsfile 那么您应该在 IDE 中获得 groovy 语法突出显示。