I have a new project. Should I place apply plugin: 'idea' in build.gradle and run $ gradle idea? Or should I import the gradle project directly into IntelliJ IDEA 14.1? Which one will allow me to add dependencies to build.gradle and have IDEA automatically download & know about them?
With Intellij 14 you can just open the the build.gradle file using Intellij's File --> Open. this will import the gradle project, including all dependencies.
After you change something in the build.gradle file, you can click on "refresh all gradle projects" at the top of the gradle tool window.
You may also mark "use auto-import" under the Build Tools/Gradle tab in Settings. This will resolve all changes made to the gradle project automatically every time you refresh your project.
The idea plugin is the old method of importing a gradle project into Intellij.
With the newer versions of Intellij, it has become redundant.
From my experience using the idea plugin does not always work correctly in IntelliJ and actually IntelliJ documentation guidelines are to simply import build.gradle file.
Also, Peter Niederwieser who is a Principal Software Engineer at Gradleware answered a similar question ~2 years ago mentioning the following:
If you use Gradle's idea task to generate project files, this is normal, as there is no way to tell IDEA what the class path of the build script itself is. If you instead use IDEA's Gradle integration ("Import from Gradle model"), this problem doesn't exist.
Bottom line, your safer way to go would be importing gradle project directly from IntelliJ.
You can create a new Gradle Project in IntelliJ, and it will handle all of the dependencies and integrate well with Gradle. You can see here for more info and specifics.
Related
I have a very basic java project.
In Eclipse there is an option to convert a project to a maven project all you have to do is right click on the java project and click "Convert to Maven Project". So basically it creates a pom.xml file for you.
Does IntelliJ have a similar command to convert to Gradle? Searched around but it did not seem like it does.
The simple way to migrate from Maven to Gradle via Intellij IDEA is:
Install Gradle Build Tool from https://gradle.org/
Add Path to System Enviroments (like in Gradle instructions)
Open your Maven project in Intellij IDEA and then open "Terminal" tab.
Write gradle init. Wait until the building process ends and then save & close your project.
Reopen your project and click Auto-import, and wait while Gradle is running. Approximate time - 5 mins.
Close your project (you may want to commit first)
Select "New Project"
Select Gradle (and any other frameworks you need)
Enter the directory where the Idea project to be converted is and
click "Finish"
Idea should handle the rest, you may need to move your files into main/java (etc)
I don't think there's a simple way to do this in place.
Convert a regular project into a Gradle project
Open your project in IntelliJ IDEA.
Create a build.gradle file. Add the information you need.
example:
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
}
As soon as you create a build.gradle file, IntelliJ IDEA recognizes the Gradle build script and displays a notification suggesting to load the project as Gradle. After you load the project, IntelliJ IDEA enables the Gradle tool window.
From: https://www.jetbrains.com/help/idea/gradle.html#convert_project_to_gradle
Currently, my built structure for a plugin in is a bit messy: I'm using the normal IDEA project file to build the plugin locally. When I push it to the repo and travis-ci is building it, it uses the maven pom.xml because for travis to work, it always has to download the complete IDEA sources.
Although this works, this has several drawbacks:
I need to keep two built mechanisms up to date. This is
When a new IDEA version is out (every few weeks), I need to change the SDK in maven and in my IDEA settings
When I add a new library, change resources, etc. I need to do this for two the two settings as well
I ran into problems when I kept the IDEA Maven plugin turned on because it saw the pom.xml and interfered with my local built. Turning it off means, I cannot download libraries with Maven which has the feature of tracking dependencies.
I saw that Gradle has an 'idea' plugin and after googling, I got the impression that Gradle is the preferred choice these days. I have seen Best way to add Gradle support to IntelliJ IDEA and I'm sure I can use the answers there to turn my pom.xml into a valid build.gradle.
However, maybe someone else has already done this or can provide a better approach. What I'm looking for is a unified way to build my plugin locally and on Travis-CI.
Some Details
For compiling an IDEA plugin, you need its SDK which you can access through an installation of IDEA or a download of the complete package. Locally, I'm using my installation for the SDK. With Travis, my maven built has the rule to download the tar.gz and extract it.
It turns out that in particular for building an IntelliJ plugin, Gradle seems to have many advantages. This is mainly due to the great IntelliJ plugin for Gradle which makes compiling plugins so much easier. With Gradle, I could turn my >220 lines of Maven build into a few lines of easily readable Gradle code. The main advantages are that
It takes care of downloading and using the correct IDEA SDK while you only have to specify the IDEA version.
It can publish your plugin to your Jetbrains repository and make it instantly available to all users
It fixes items in your plugin.xml, e.g. you can use one central version number in gradle.build and it will keep plugin.xml up-to-date or it can include change-notes
It seamlessly integrates with Travis-CI
How to use Gradle with an existing IDEA plugin
Do it manually. It's much easier.
Create an empty build.gradle file
Look at an example and read through the README (there are many build.gradle of projects at the end) to see what each intellij property does.
Adapt it to your plugin by
Setting the intellij.version you want to build against
Setting your intellij.pluginName
Define where your sources and resources are
Define your plugin version
Define a Gradle wrapper that enables people (and Travis) to build your plugin without having Gradle
Create the gradle wrapper scripts with gradle wrapper
Test and fix your build process locally with ./gradlew assemble
If everything works well, you can push build.gradle, gradlew, gradlew.bat and the gradle-folder to your repo.
Building with Travis-CI
For Travis you want to use the gradlew script for building. To do so, you need to make it executable in the travis run. An example can be found here.
I have a set of 15 or so interdependent Gradle Java modules/projects that build correctly with Gradle. However, I run into issues importing them into Eclipse.
If I import them via the Gradle Eclipse plug-in (import as Gradle project) and select "Build Model" in the root folder, it imports the projects, but doesn't treat them as Java projects. Specifically, syntax errors are not recognized.
If I import each project individually, they are recognized as Java projects, but they are not linked properly and Eclipse shows build errors.
How do I properly import/link Gradle projects into Eclipse?
Check if your project has eclipse specific files like .project, .classpath etc. These are hidden files so you need to run command $ls -la to confirm that.
If those files are not there then you need to convert your project into a valid eclipse project. Gradle provides plugin to do that.
Add eclipse plugin in gradle build file (i.e. build.gradle).
apply plugin: 'eclipse'
Run $gradle eclipse
I have provided these details on my blog, here.
Unfortunately is not easy to answer that, it depends on the details of your project. I found that for complex projects it often needs specific tweaks in the build script.
I am going to assume your projects have never been used in Eclipse. These are some tips that may work for such cases (or they may not, and will need more specific tweaks).
1) make sure to apply the 'eclipse' plugin to all your projects if they are plain Java projects, or the 'eclipse-wtp' plugin if they 'war' style web projects.
2) import your projects with 'dependency management' disabled (this is one of the options in the import wizard.
If this doesn't work, I will need more details about your projects and/or exactly what is going wrong with the import. It sounds like maybe your projects are not getting a proper java nature, or the source folders are not correctly configured. The contents of the .classpath and .project files for the 'badly configured' projects would be helpful.
Grade build, even from inside IntelliJ IDEA does not put the dependencies into the "External Libraries" folder, so these classes don't show up as suggestions in the editor and when I manually add them as an import there is a compile error.
How can I get IntelliJ to automatically incorporate the dependencies in my build.gradle file, for instance:
compile 'com.google.code.gson:gson:1.7.2
After adding dependencies open "Gradle" ('View'->Tool Windows->Gradle) tab and hit "refresh"
example of adding (compile 'io.reactivex:rxjava:1.1.0'):
If Idea still can not resolve dependency, hence it is possibly the dependency is not in mavenCentral() repository and you need add repository where this dependency located into repositories{}
You either need to import the project as a Gradle project from within Idea. When you add a dependency you need to open the Gradle window and perform a refresh.
Alternatively generate the project files from gradle with this:
build.gradle:
apply plugin: 'idea'
And then run:
$ gradle idea
If you modify the dependencies you will need to rerun the above again.
When importing an existing Gradle project (one with a build.gradle) into IntelliJ IDEA, when presented with the following screen, select Import from external model -> Gradle.
Optionally, select Auto Import on the next screen to automatically import new dependencies.
For those who are getting the "Unable to resolve dependencies" error:
Toggle "Offline Mode" off
('View'->Tool Windows->Gradle)
Andrey's above post is still valid for the latest version of Intellij as of 3rd Quarter of 2017. So use it. 'Cause, build project, and external command line gradle build, does NOT add it to the external dependencies in Intellij...crazy as that sounds it is true. Only difference now is that the UI looks different to the above, but still the same icon for updating is used. I am only putting an answer here, cause I cannot paste a snapshot of the new UI...I dont want any up votes per se. Andrey still gave the correct answer above:
Tried everything in this thread and nothing worked for me in IntelliJ 2020.2. This answer did the trick, but I had to set the correct path to the JDK and choose it in Gradle settings after that (as showed in figures bellow):
Setting the correct path for the Java SDK (under File->Project Structure):
In Gradle Window, click in "Gradle Settings..."
Select the correct SDK from (1) here:
After that, the option "Reload All Gradle Projects" downloaded all dependencies as expected.
Cheers.
I had this exact same error and nothing else worked. Finally, I did the following:
Close IntelliJ IDEA.
Delete .idea directory from my project folder.
Reopen IntelliJ and import the project again (as Gradle).
After the above, any new gradle dependency I added to build.gradle started appearing in External Dependencies section when I clicked the gradle refresh button.
In my case,I was getting error while refreshing gradle ('View'->Tool Windows->Gradle) tab and hit "refresh" and getting this error no such property gradleversion for class jetgradleplugin.
Had to install latest intellij compatible with gradle 5+
in my case, just re-import the jar file into correct directory .
File->project Structure->[Project setting -> Modules]
I have a toy Java project set up with Gradle in IntelliJ IDEA 13.1.2. It compiles and runs fine, but the IDE highlights 'google' in
import com.google.common.base.Strings;
as red and warns "Cannot resolve symbol 'google'". Any idea how to fix it?
I have tried 1) deleting .idea/ and re-creating the project in IntelliJ IDEA, and 2) re-importing project from the manually created Gradle configuration file build.gradle, but to no avail.
I think user Sap is correct, at least in my case. You should not have to manually add the dependency.
Did you change the dependencies in the gradle file without syncing intellij? Try this button:
For more information, see:
https://www.jetbrains.com/idea/help/synchronizing-changes-in-gradle-project-and-intellij-idea-project.html
Check this.
You can simply open Gradle tool window at [ View ] - [ Tool Windows ] - [ Gradle ].
In the window, you can refresh by clicking refresh button.
All dependencies manually added directly into build.gradle file will be resolved.
One of the solutions that worked for me after trying everything listed on the internet to solve this issue was to install the lombok plugin.
Got to File --> Settings --> Plugins and look for Lombok.
Make sure the "Enable annotation processing" is ticked
My Gradle project is using Intellij 2019.2.3, and File->Invalid Cache/Restart... doesn't work, but View->Tool Windows->Gradle->sync(the circular icon) works instantly.
In my case, I have to do the following:
close project
close idea
remove .idea project directory
remove (idea.system.path) directory
start idea
You can find your idea.system.path here: https://intellij-support.jetbrains.com/hc/en-us/articles/206544519-Directories-used-by-the-IDE-to-store-settings-caches-plugins-and-logs
What helped me was checking "dependencies" in "project settings/modules" section. Apparently, Idea did not pick up them correctly.
Steps which worked for me:
delete all modules from "project settings/modules"
refresh the project from Gradle plugin - that triggers generation of modules
This trick helped me to get modules with correct dependencies generated.
Probably it happened because initially, I imported the project as non-Gradle one.
It turns out that the depended packages need to be separately specified in IntelliJ IDEA in addition to build.gradle and explicitly added as a dependency. Namely,
download the com.google.guava package following the instruction in documentation -- I picked com.google.guava:guava-base:r03 to download
follow the automatically-prompted window to add it as a project library to the Java project.
Specify that the Java module depends on the com.google.guava package in the project settings. See the documentation instruction "Configuring Module Dependencies and Libraries".
In my case (Apache Beam sources) a ./gradlew clean was needed.
In My Case, I've Updated the Gradle version(module: project) from 3.2.2 to 3.5.2, and also there was a problem with the NDK file location it was on the wrong path, I've just switched it to the default NDK path, then invalidate and restart the project.
I was having the wrong import.Check if you have right import. In case you have imported using:
import org.junit.Test
and you have org.junit.jupiter.api.Test in class path, try importing with :
import org.junit.jupiter.api.Test;