System Groovy Script: hudson.*, jenkins.* and org.jenkinsci.* imports are not found in IntelliJ IDEA

341 views
Skip to first unread message

Amedee Van Gasse

unread,
Feb 16, 2021, 7:08:01 AM2/16/21
to Jenkins Users
Following up on my previous question about a System Groovy Script.

I have a .groovy file in a git repo.
I have cloned the git repo to my pc.
I have opened the directory in IntelliJ IDEA.

Now I get a lot of red text. None of the imports are found.
It looks like this:

import hudson.model.*;
import hudson.util.*;
import jenkins.model.*;
import hudson.FilePath.FileCallable;
import hudson.slaves.OfflineCause;
import hudson.node_monitors.*;
import org.jenkinsci.plugins.workflow.job.WorkflowRun
import org.jenkinsci.plugins.workflow.flow.FlowExecution;
import org.jenkinsci.plugins.workflow.graph.FlowGraphWalker;
import org.jenkinsci.plugins.workflow.graph.FlowNode;
import org.jenkinsci.plugins.workflow.graph.StepStartNode;
import org.jenkinsci.plugins.workflow.cps.nodes.StepStartNode;
import org.jenkinsci.plugins.workflow.actions.WorkspaceAction


How do I configure IntelliJ IDEA so that I no longer have red alerts on the imports?

For the record: I did not write this code. I have been asked to maintain it, and fix a bug.
It's my professional opinion that all code should be under version control, and I don't want to edit the code directly on the Jenkins web pages but in a IDE so that I have syntax checking, autocomplete, and all the other usual conveniences.

You are allowed to assume that there are only 3 files in the repository: a .groovy file, a .gitattributes and a .gitignore. I do know how to write code in the Groovy language, but I don't know how to set up a Groovy project. As an analogy: I know how to fly a plane, but I don't know how to take off or land.

Amedee Van Gasse

unread,
Feb 16, 2021, 7:21:23 AM2/16/21
to Jenkins Users
Answering my own question for at least the hudson.* and jenkins.* imports:

* download jenkins-core for my Jenkins version from https://repo.jenkins-ci.org/ (in my case: version 2.263.3, so I need https://repo.jenkins-ci.org/releases/org/jenkins-ci/main/jenkins-core/2.263.3/jenkins-core-2.263.3.jar)
* save the jar in a directory lib
* add the jar as a library to my project

Now, at least for the hudson and jenkins imports, IntelliJ doesn't complain.
I think I'll have to repeat for the Workflow plugin?

Now, I know that this is the really old fashioned way of doing it, from the days of yore from before dependency management.
I'd prefer not to have the lib folder with jars added to git.

What would be a nice dependency management ish way of doing it?


Amedee Van Gasse

unread,
Feb 16, 2021, 7:47:28 AM2/16/21
to Jenkins Users
I am happy to report that after adding the following jars to my lib folder, I no longer have errors:

jenkins-core-2.263.3.jar          workflow-api-2.41.jar          workflow-cps-2.87.jar          workflow-job-2.40.jar          workflow-step-api-2.23.jar
jenkins-core-2.263.3-javadoc.jar  workflow-api-2.41-javadoc.jar  workflow-cps-2.87-javadoc.jar  workflow-job-2.40-javadoc.jar  workflow-step-api-2.23-javadoc.jar
jenkins-core-2.263.3-sources.jar  workflow-api-2.41-sources.jar  workflow-cps-2.87-sources.jar  workflow-job-2.40-sources.jar  workflow-step-api-2.23-sources.jar

The unfortunate part is that I still have to manually hunt down the correct jars and add them to git, but at least I can finally start with coding!

Yannick Lacaute

unread,
Feb 16, 2021, 8:32:05 AM2/16/21
to jenkins...@googlegroups.com
Hello, maybe you could you use maven to declare the dependencies instead of downloading them manually.
The IDE will download jars and resolve imports automatically, this is what I do when I develop shared library for example.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/215cec8c-1d63-42f3-b33e-29ae19646767n%40googlegroups.com.


--
Yannick

Amedee Van Gasse

unread,
Feb 16, 2021, 10:57:27 AM2/16/21
to Jenkins Users
Oh? I didn't know that Maven also did Groovy.
You learn something new every day, don't ya?

For now I did something with Gradle. Don't know if it actually works because I have never used Gradle before, and I already had the jars in my lib folder. But anyway, as long as it #worksforme, it's good enough.

Thanks!

Reply all
Reply to author
Forward
0 new messages