Best way to add Gradle support to IntelliJ Project
Solution 1
Add:
build.gradle
in your root project folder, and use plugin for example:
apply plugin: 'idea'
//and standard one
apply plugin: 'java'
and with this fire from command line:
gradle cleanIdea
and after that:
gradle idea
After that everything should work
Solution 2
There is no need to remove any .iml
files. Follow this:
- close the project
File
->
Open...
and choose your newly createdbuild.gradle
- IntelliJ will ask you whether you want:
Open Existing Project
Delete Existing Project and Import
- Choose the second option and you are done
Solution 3
Another way, simpler.
Add your
build.gradle
file to the root of your project. Close the project. Manually remove "*.iml" file and/or ".idea" directory. Then choose "Import Project...", navigate to your project directory, select the build.gradle file and click OK.
Solution 4
In IntelliJ 2017.2.4 I just closed the project and reopened it and I got a dialog asking me if I wanted to link with build.gradle which opened up the import dialog for Gradle projects.
No need to delete any files or add the idea plugin to build.gradle.
Solution 5
Just as a future reference, if you already have a Maven
project all you need to do is doing a gradle init
in your project directory which will generates build.gradle
and other dependencies, then do a gradle build
in the same directory.
Related videos on Youtube
Gremash
Updated on July 08, 2022Comments
-
Gremash almost 2 years
I have looked around quite a bit and haven't found the best solution to convert an existing IntelliJ project to Gradle. I work in a team environment and we currently share the .ipr file as we have a few build configurations that we track. We will be getting rid of those in favor of Gradle eventually but I can't screw things up too much until the Gradle conversion is done.
Also, our Java source files are located in the root of the src directory instead of src/main/java as is standard.
Is there a way I can add Gradle to my project that won't make me delete and recreate my IntelliJ project and won't screw everyone else up when they do a Git pull?
-
Trisha about 9 yearsTo answer the question of your source directory - you need to override the default source directory with sourceSets. See how we did this in the MongoDB Java driver: github.com/mongodb/mongo-java-driver/blob/…
-
-
Marquis Blount over 8 yearswhen attempting this solution did anyone come across the error "Module 'xx' is not backed by gradle? if so how was it resolved
-
Amala about 8 yearsThanks this was driving me crazy and I couldn't figure out how to make IntelliJ refresh the projects dependencies after adding it to build.gradle.
-
panza about 8 yearsgradle idea (that depends on ideaProject, ideaModule, ideaWorkspace) did not work for me as ideaModule kept failing. Instead, the task gradle ideaProject worked fine and the project is now Gradle-enabled.
-
Yar almost 8 yearsAlright there is a problem with this approach. You'll get a "dependencies cannot be applied to groovy.lang.closure" error. Check out this solution instead: stackoverflow.com/a/17168175/2517730
-
barracuda317 almost 7 yearsHow can i fix the problem, that the new project has to be in another folder?
-
Alex H. over 6 yearsCan confirm this works nicely. Just run gradle init in the project root, then you'll get the dialog next time you open the project.
-
xeruf about 6 yearsThis is somewhat outdated, since the idea Gradle plugin still resorts to the file-based idea project, while the folder-based project is standard and much more handy
-
user25 about 6 yearswell I don't have global local gradle on my system, usually it downloads specific gradle version for every project separately gradle: command not found...
-
NOTiFY almost 6 yearsDoes this add the JARs from your Project Structure | Libraries as dependencies and the create an EAR (using the plugin) as defined the in Project Structure | Artifacts?
-
Michal Kordas almost 6 years@NOTiFY I doubt it does. It's equivalent to checking out project from scratch.
-
NOTiFY almost 6 years@Michal Kordas I added all the dependencies by hand, didn't take anywhere near the time i expected and now add the WAR, JAR & EAR creation. There's a feature request for this at JetBrains youtrack.jetbrains.com/issue/IDEABKL-6990
-
Guy Coder over 5 yearsOf interest: Creating New Gradle Builds - Notes:
gradle init
can generate various different types of projects, and even knows how to translate simplepom.xml
files to Gradle. -
Jesse Barnum over 3 yearsBetter instructions would be helpful. What does "use plugin for example" mean? Am I supposed to type those commands somewhere? Also, "gradle cleanIdea" just gives me an error: Task 'cleanIdea' not found in root project 'HamsterBall'.