34

I just added a new test source directory to my Gradle project and want IntelliJ to recognize it. I can close the project and reopen the build.gradle file and chose "Delete Existing Project and Import"; is there a way to update the module sources without deleting the existing project?

idea {
    module {
        // just added these two lines        
        testSourceDirs += file('src/integration-test/java')
        testSourceDirs += file('src/integration-test/resources')
    }
}

I don't want to just manually add the sources, instead I want IntelliJ to re-import the Gradle project structure from the 'build.gradle' file.

Details: Gradle v1.7, IntelliJ v12

Rylander
  • 19,449
  • 25
  • 93
  • 144

7 Answers7

66

IntelliJ 13 supports "refreshing" the Gradle project. When the project is refreshed any dependency changes are picked up as well as any changes to the project structure.

In order to do so go to View -> Tool Windows and open the Gradle Tool Window you find the refresh button in the upper bar.

Rylander
  • 19,449
  • 25
  • 93
  • 144
11

I'm using intellij 2017 and was unable to locate the "refresh" option as described in previous versions of intellij. For me,

./gradlew idea corrected the issue. gradle idea should also work.

UPDATE

As noted in the comments, when using intellij 2017 open the build.gradle file as a project and enable the "auto import" command to avoid this issue all together!

P.Brian.Mackey
  • 43,228
  • 68
  • 238
  • 348
  • 1
    The refresh option is still there, however the most recent version supports an auto refresh that you can enable when you first open the project (by opening the `build.gradle` file as a project). With the auto refresh, there is no longer any need to manually trigger the refresh. – Rylander Jun 06 '17 at 23:03
9

Right click on the build.gradle file and select import gradle project

kazuar
  • 1,094
  • 1
  • 12
  • 14
8

Steps that helped me:

  1. Close the project
  2. Delete ‘gradle’ and '.gradle' folders in the project root
  3. Import the project as gradle project

See images of Gradle tab before and after

Community
  • 1
  • 1
stkotok
  • 101
  • 1
  • 5
3

The Gradle support in IntelliJ 12 is limited. (I've heard 12.4 is somewhat better, but I don't know if it supports updating the project.) Either check out the new Gradle plugin in IntelliJ 13 (EAP), or generate IDE files using Gradle's IntelliJ plugin.

Peter Niederwieser
  • 121,412
  • 21
  • 324
  • 259
3

For IntelliJ 2020, right-click the build.gradle file and press the Import Gradle Project option. This will let IntelliJ know it's a Gradle project and be able to interpret it.

Tyler Moen
  • 81
  • 5
0

In newer versions of IDEA, there is a dedicated button from the Gradle sidebar

Reload Gradle project

Yeikel
  • 854
  • 10
  • 18