Groovy Project Setting in IDEA IntelliJ - intellij-idea

I'm using IDEA to write groovy scripts for my jenkins job. So I have these imports
import groovy.json.JsonSlurper
import hudson.FilePath
import hudson.model.Node
import hudson.model.Slave
import jenkins.model.Jenkins
Before I added jenkins-core-2.9.jar into the project dependency, those jenkins classes are marked in red. Then I added the jar as a dependency by "File"->"Project Structure"->"Modules"->"Dependencies"
Then those red marks are gone. However, when I tried to run the script, it says Error:(14, 1) Groovyc: unable to resolve class jenkins.model.Jenkins. What else do I need to do?

I had the same problem and it was a permission issue. For me this command fixed it: sudo chmod o=r jenkins-core-2.73.3.jar

As stated in this link,
Note that if you configure a dependency through the Project Structure dialog, the dependency will only appear in the IntelliJ IDEA Project tool window, not in the Gradle tool window.
Adding the dependency like this only adds the dependency in the idea workspace. When you run the task either from the gradle tool window, or from an external command line tool, that dependency no longer exists. You should add it to your gradle dependencies:
dependencies {
compile "org.jenkins-ci.main:jenkins-core:2.9"
}
You can configure a repository to load this dependency from. This repository can be a file path, or a maven repository etc.
See here for more details on gradle dependency management.

Related

Ktor: Can't import dependencies

I have created the sample Ktor project in IntelliJ IDEA.
I added the following dependencies to build.gradle.kts:
implementation("org.ktorm:ktorm-core:3.4.1")
implementation("io.reactivex.rxjava3:rxkotlin:3.0.0")
It looks like the dependencies are found, as the project builds and runs.
I am trying to add imports to the source code:
import org.ktorm.database.*
import io.reactivex.Completable
The IDE doesn't see packages org.ktorm nor io.reactivex.
What can I do to use Ktorm or RxJava in Ktor? It looks like it doesn't see any dependencies I add.
I have figured it out. After adding a dependency select File->Invalidate caches, and then click the button "Invalidate and restart".

How to import a project with modules and java packages in IntelliJ?

We have a java project with several modules. We use git for versioning and want the developers to be able to chose their own IDE. So we don't push .idea and .iml files with git. This means that developers in IntelliJ needs who clones from git needs to set up the project in IntelliJ as well. However we are having some issues with setting it up. In my own project it works fine and looks like:
Proejct Module (Is this a real module?)
⤷ Module A
⤷ src
⤷ main
⤷ java
⤷ Package 1
⤷ Package 2
⤷ test
⤷ Module B
Before we added packages to the project new developers could just pull from git and open Module P in IntelliJ, then go to project structure and import module from Module A and Module B. However if they try the same thing now IntelliJ tries to make the Modules main and test from Module A.
A workaround we temporarily use for this is to open Module A and B directly as separate IntelliJ projects. If we do this IntelliJ does not try to make modules out of main and test. However this is very inconveniant for us.
How can we fix this?
It's a good approach to make your source control system "agnostic" about the IDE and not check in .idea, *.iml and equivalent things from other IDE's.
However you really need to use a build control system like maven or gradle.
In my workplace we use maven for all our java projects. It's very simple to define a pom.xml at the root level which defines your modules, and many other things.
When you import a maven project into intellij, all the relevant IntelliJ modules are automatically created for you. In essence, you only need to import the pom.xml and you're good to go.
Otherwise it will be a nightmare to import and create modules every time you check out source from git. Maven isn't perfect but I highly recommend using either maven or gradle.
To define multi-modules from the root pom.xml, make a section like this:
<modules>
<module>module-A</module>
<module>module-B</module>
</modules>
And then the pom.xml in both module A and B should define a section referring to the root pom via the <parent> tag.

IDEA 2016.1, Gradle's processResources expanding not working automatically

I have an IDEA 2016.1 Enterprise and a Gradle 2.12 multi-module project. In one of the modules, in src/main/resources, I have a file which I would like Gradle to 'expand', here is my configuration:
processResources {
filesMatching('my.properties') {
expand(project.properties)
}
}
(I would like to expand just this single file, and just copy the rest.)
It all works fine when built on the command line, but not by default in IDEA - when I clean and build the project, the file lands in build/resources/main but the placeholders are not replaced. I have to manually invoke the Gradle processResources task using the Gradle pane in IDEA and double clicking on the task.
Is this something I should report to Jetbrains (i.e. a bug) or has anybody have it working and I should change something in my configuration?
When you build from command line, you are using gradle. However, when you build the project from intellij, by default intellij doesn't use gradle to build, but use its internal build system which doesn't understand your gradle's processResources.
One way to solve it is to check "Delegate IDE build/run actions to gradle" as shown below:
If you don't want to use gradle build in intellij, there's another workaround - add processResources as a gradle task to run after build in your "Run/Debug Configurations":
Try adding the dependency in your build.gradle file, eg.
assemble.dependsOn processResources
This should work if you have java plugin applied.

How to get IntelliJ to associate Gradle sources with build.gradle?

When writing Gradle scripts for my Java project, specifically, when writing build.gradle files, IntelliJ does not recognize the Gradle API.
For instance, Gradle methods calls like apply, dependencies configure appear with a black line under them and it is not possible to navigate to method declarations, there is no auto-completion etc.
I managed to work around this by adding compile gradleApi() to the build's dependencies block. However, I don't want to have this explicit dependency in my code.
I tried editing IntelliJ's project structure and add a dependency on a Gradle library (tried gradle-core and gradle-all) to my modules, but that seems to have no effect.
Is there a way to make IntelliJ associate all build.gradle files with the Gadle sources?
I solved this problem as follows:
As mention in already posted answers, configure gradle
update gradle/wrapper/gradle-wrapper.properties file
change bin to all in distributionUrl i.e.
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-bin.zip
to
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-all.zip
OR
[optional] If you are using old version of gradle wrapper and wanted to upgrade, then execute
./gradlew wrapper --gradle-version 6.8.3 --distribution-type all
Update gradle task (if present in build file)
wrapper {
gradleVersion = '6.8.3'
distributionType = Wrapper.DistributionType.ALL
}
Before importing the project to IntelliJ-Idea IDE, update build.gradle and add java and idea plugin to the plugins list
plugins {
id "java-library"
id "idea"
}
From a terminal, execute ./gradlew clean build idea or simply ./gradlew idea
Import project to IntelliJ idea.
Go to Preferences --> build,Execution,Deployment --> BuildTools --> Gradle
You can see
Restart IntelliJ idea IDE.
So above we have configured both of the options so choose either of them, except the specified location option. That's it.
Before
After
Autocomplete functionality as mentioned in this answer.
I had similar frustrations with Grails 3, which defines and runs a wrapper task when an app is created. Changing to the "all" zip in the wrapper properties file did not work because this kept getting changed back to the "bin" zip.
This was solved when it was understood that the "gradle-wrapper.properties" file simply stores the values from the "wrapper" task, and if this task is run after the properties are changed, they get changed right back.
This is easily fixed by setting some properties on the wrapper task:
wrapper.gradleVersion='3.2.1'
wrapper.distributionType=Wrapper.DistributionType.ALL
Now importing the project into IDEA gives you smart editing of your build.gradle.
when I choose build.gradle in IDEA and open it, IDE prompts
You can configure Gradle wrapper to use distribution with sources. It will provide IDE with Gradle API/DSL documentation.
I choose Ok, apply suggestion!
after project refreshing I am able to use code completion
before you import your project, configure it to use the customizable gradle wrapper as per the instructions here :-
https://docs.gradle.org/current/userguide/gradle_wrapper.html
add a task to your top level project like this:-
task wrapper(type: Wrapper) {
println "Wrapper gradleVersion = '2.12'"
gradleVersion = '2.12'
}
or whatever the latest version is.
make sure you can build the project from the gradle command line before you try importing into intelliJ, using the ./gradlew command, which will download and install a gradle distribution for you the first time you build.
set your java home, intelliJ home and gradle home variables in your machine and in intelliJ (mine look like this, yours may be different depending on your setup and your history of hacking around your machine...:-
(from .bashrc
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home
)
When you do import, choose the customisable gradle wrapper. if all is well, when you open the top level build.gradle for your project, you will be asked to configure sources for the gradle dsl, which will also update your gradle wrapper properties file to this:-
#Thu Mar 31 14:04:00 BST 2016
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.12-all.zip
.. the change being from ... bin.zip to all.zip. and that's it. This had been giving me lots of grief for a long time, but that's the way to do it. (on IntelliJ IDEA 2016.1 CE at least...)
most of this was in
Dimitry's answer too, but I couldn't get it to work using the default wrapper , it had to be the customisable wrapper.

How to define Gradle's home in IDEA?

I am trying to import a Gradle project into IntelliJ, and when I get to the Gradle Home textbox, it is not automatically populated, nor will typing in the path of Gradle Home result in a valid location - I have the GRADLE_USER_HOME environment variable set (to what I think is!) the correct path, and I have been able to successfully import this same project into Eclipse. Any suggestions?
You can write a simple gradle script to print your GRADLE_HOME directory.
task getHomeDir {
doLast {
println gradle.gradleHomeDir
}
}
and name it build.gradle.
Then run it with:
gradle getHomeDir
If you installed with homebrew, use brew info gradle to find the base path (i.e. /usr/local/Cellar/gradle/1.10/), and just append libexec.
The same task in Kotlin in case you use build.gradle.kts:
tasks.register("getHomeDir") {
println("Gradle home dir: ${gradle.gradleHomeDir}")
}
Installed on a Mac via Homebrew, the path
/usr/local/opt/gradle/libexec
is preferable to
/usr/local/Cellar/gradle/X.X/libexec
since the former will survive version upgrades.
If you installed gradle with homebrew, then the path is:
/usr/local/Cellar/gradle/X.X/libexec
Where X.X is the version of gradle (currently 2.1)
If you are using IntelliJ, just do the following.
Close the project
(re)Open the project
you will see "Import gradle project" message on the right bottom. click.
select "Use default gradle wrapper". not "Use local gradle distribution"
That's all.
This is what helped me solve the problem of not having Gradle home set for the IDEA when importing a Gradle project.
THREE OPTIONS -- (A) Default Wrapper (B) "gradle 'wrapper' task configuration" OR (C) "local gradle distribution" defined by jetbrains: https://www.jetbrains.com/help/idea/gradle-settings.html
A. Default Wrapper (recommended)
If you are able, select this recommended option. If it is grayed out, see option C, which should then set your default for all subsequent projects.
B. Gradle 'Wrapper' Task Configuration
If you want IDEA to define your gradle version for you from your build script
Set this option if you define your gradle build versions as a task within your actual gradle build.
Example below from jetbrains: https://www.jetbrains.com/help/idea/gradle-settings.html
(useful if you do not want to share gradle builds between projects)
C. Local Gradle Distribution
1. Run the following command to get gradle location:
brew info gradle (if gradle was installed with homebrew)
2. You are looking for something like this:
/usr/local/Cellar/gradle/4.8.1
3. Next, append 'libexec' to the gradle location you just found:
/usr/local/Cellar/gradle/4.8.1/libexec
This is because "libexec is to be used by other daemons and system utilities executed by other programs" (i.e. IDEA). Please see https://unix.stackexchange.com/questions/312146/what-is-the-purpose-of-usr-libexec
4. Finally, put that new path in the Gradle home input box if IDEA prompts you.
IDEA should now have allowed you to hit OK
C:\Users\<_username>\.gradle\wrapper\dists\gradle-<_version>-all\<_number_random_maybe>\gradle-<_version>
\Android studio\gradle didn't worked for me.
And "Default gradle wrapper" wasn't configured while importing (cloning) the project from bitbucket
If it causes problem to figure out the path, here is my path :
C:\Users\prabs\.gradle\wrapper\dists\gradle-5.4.1-all\3221gyojl5jsh0helicew7rwx\gradle-5.4.1
This is instruction for MAC only.
I had the same problem. I solved it by configuring $GRADLE_HOME in .bash_profile. Here's how you do it:
Open .bash_profile (usually it's located in the user’s home directory).
Add the following lines to update $PATH variable:
export GRADLE_HOME=/usr/local/opt/gradle/libexec
export PATH=$GRADLE_HOME/bin:$PATH
Save it.
Apply your changes by running
source .bash_profile
I wrote my own article with instruction in a case if somebody will encounter the same problem.
On a mac it should ideally be at : /Applications/Android Studio.app/Contents/gradle/gradle-2.14.1
(Replace the version string with the latest)
AFAIK it is GRADLE_HOME not GRADLE_USER_HOME (see gradle installation http://www.gradle.org/installation).
On the other hand I played a bit with Gradle support in Idea 13 Cardea and I think the gradle home is not automatically discover by Idea. If so you can file a issue in youtrack.
Also, if you use gradle 1.6+ you can use the Graldle support for setting the build and wrapper. I think idea automatically discover the wrapper based gradle project.
$ gradle setupBuild --type java-library
$ gradle wrapper
Note: Supported library types: basic, maven, java
Regards
If you're using MacPorts, the path is
/opt/local/share/java/gradle
I had to setup the Project SDK before selecting gradle path. Once that was set correctly, I had to choose "Use default gradle wrapper (recommended) in "Import Project from Gradle" dialog.
Still works if I remove gradle using brew:
$ brew remove gradle
This is where my gradle home is (Arch Linux):
/usr/share/java/gradle/
I had some weird errors where it could not find my class, I had to right click on my src folder (was red) to "Make Directory as" -> Source Folder Root
Click New -> Project from existing sources -> Import gradle project...
Then Idea recognized gradle automatically.
I couldn't get it to accept my Gradle JVM selection until I deleted a broken JDK
Th window below is from File -> Other Settings -> Structure For New Projects...
I had a red 1.8 JDK SDK entry here, once I deleted that Gradle JVM error below disappeared and I could move on to the next step
In case you are using Mac, most probably your gradle home should be /usr/local/gradle-2.0 for example.
In preference of IDEA search for gradle and set gradle home as given above.
It should work