176

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?

1

9 Answers 9

223

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

7
  • 7
    when attempting this solution did anyone come across the error "Module 'xx' is not backed by gradle? if so how was it resolved Aug 11, 2015 at 19:51
  • 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.
    – Amala
    Feb 18, 2016 at 18:31
  • 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.
    – panza
    Mar 2, 2016 at 15:58
  • 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
    – Yar
    Jun 17, 2016 at 15:03
  • 1
    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
    – xeruf
    Feb 17, 2018 at 14:06
76

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
4
  • How can i fix the problem, that the new project has to be in another folder? Jun 23, 2017 at 15:31
  • 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?
    – NOTiFY
    Jul 10, 2018 at 14:02
  • @NOTiFY I doubt it does. It's equivalent to checking out project from scratch. Jul 11, 2018 at 6:37
  • @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
    – NOTiFY
    Jul 11, 2018 at 9:25
50

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.

0
22

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.

1
  • 2
    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.
    – Alex H.
    Nov 27, 2017 at 20:23
17

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.

1
  • 2
    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.
    – Guy Coder
    Oct 24, 2018 at 18:33
7

I'm using Version 12 of IntelliJ.

I solved a similar problem by creating an entirely new project and "Checking out from Version Control" Merging the two projects later was fairly easy.

0
4
  1. Add build.gradle in your project's root directory.

  2. Then just File -> Invalidate Caches / Restart

Here is a basic build.gradle for Java projects:

plugins {
    id 'java'
}

sourceCompatibility = '1.8'
targetCompatibility = '1.8'
version = '1.2.1'
1

To add to other answers. For me it was helpful to delete .mvn directory and then add build.gradle. New versions of IntelliJ will then automatically notice that you use Gradle.

0
1

Open the context menu by clicking on gradle.build file and then press "Link Gradle project" (or something like that - I have just done it and the menu is not available anymore in my project, because my project is now linked as a Gradle project). Then you will have Gradle tab at the right window of your Intellij Idea.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.