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?

gvlasov
  • 18,638
  • 21
  • 74
  • 110
Gremash
  • 8,158
  • 6
  • 30
  • 44
  • 2
    To answer the question of your source directory - you need to override the default source directory with [sourceSets](https://gradle.org/docs/current/dsl/org.gradle.api.tasks.SourceSet.html). See how we did this in the MongoDB Java driver: https://github.com/mongodb/mongo-java-driver/blob/782a8c58cf13b0026ed4d58bfd6b74f1248c4b08/build.gradle#L110 – Trisha Mar 18 '15 at 10:48

9 Answers9

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

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
SuperAndrew
  • 3,013
  • 1
  • 17
  • 10
  • 7
    when attempting this solution did anyone come across the error "Module 'xx' is not backed by gradle? if so how was it resolved – Marquis Blount Aug 11 '15 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 '16 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 02 '16 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: http://stackoverflow.com/a/17168175/2517730 – Yar Jun 17 '16 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 '18 at 14:06
  • 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**... – user25 Mar 31 '18 at 03:30
  • 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'. – Jesse Barnum Sep 01 '20 at 20:43
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
Michal Kordas
  • 10,475
  • 7
  • 58
  • 103
  • How can i fix the problem, that the new project has to be in another folder? – barracuda317 Jun 23 '17 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 '18 at 14:02
  • @NOTiFY I doubt it does. It's equivalent to checking out project from scratch. – Michal Kordas Jul 11 '18 at 06: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 https://youtrack.jetbrains.com/issue/IDEABKL-6990 – NOTiFY Jul 11 '18 at 09: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.

Guildenstern70
  • 1,715
  • 18
  • 18
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.

Raniz
  • 10,882
  • 1
  • 32
  • 64
  • 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 '17 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.

GabrielTK
  • 53
  • 1
  • 7
Yar
  • 7,020
  • 11
  • 49
  • 69
  • 2
    Of interest: [Creating New Gradle Builds](https://guides.gradle.org/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 '18 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.

Muhd
  • 24,305
  • 22
  • 61
  • 78
hippoLogic
  • 304
  • 1
  • 5
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'
Gayan Weerakutti
  • 11,904
  • 2
  • 71
  • 68
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.

Tomislav Brabec
  • 529
  • 2
  • 14
  • 20
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.

Petr Kiritsev
  • 267
  • 1
  • 3
  • 6