181

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

229

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

Sign up to request clarification or add additional context in comments.

7 Comments

when attempting this solution did anyone come across the error "Module 'xx' is not backed by gradle? if so how was it resolved
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.
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.
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
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...
|
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 Comments

How can i fix the problem, that the new project has to be in another folder?
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 I doubt it does. It's equivalent to checking out project from scratch.
@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
51

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.

Comments

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 Comment

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.
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 Comment

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

Comments

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' 

Comments

4

Open the context menu by clicking on build.gradle 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.

1 Comment

haha, I gues I've been using IntelliJ as a wordPad because I wasn't aware there were "context menus"... anyway, I still don't see the same thing you do, because "open the context menu by clicking"... clicking what?? Where are these context menus?
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.