Best way to add Gradle support to IntelliJ Project

119,348

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 created build.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.

Share:
119,348

Related videos on Youtube

Gremash
Author by

Gremash

Updated on July 08, 2022

Comments

  • Gremash
    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?

  • Marquis Blount
    Marquis Blount over 8 years
    when attempting this solution did anyone come across the error "Module 'xx' is not backed by gradle? if so how was it resolved
  • Amala
    Amala about 8 years
    Thanks 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
    panza about 8 years
    gradle 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
    Yar almost 8 years
    Alright 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
    barracuda317 almost 7 years
    How can i fix the problem, that the new project has to be in another folder?
  • Alex H.
    Alex H. over 6 years
    Can 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
    xeruf about 6 years
    This 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
    user25 about 6 years
    well 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
    NOTiFY almost 6 years
    Does 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
    Michal Kordas almost 6 years
    @NOTiFY I doubt it does. It's equivalent to checking out project from scratch.
  • NOTiFY
    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
    Guy Coder over 5 years
    Of interest: Creating New Gradle Builds - Notes: gradle init can generate various different types of projects, and even knows how to translate simple pom.xml files to Gradle.
  • Jesse Barnum
    Jesse Barnum over 3 years
    Better 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'.