46

Recently, I found all my gradle projects in Idea import separated modules for main and test. The modules look like this:

enter image description here

As you can see, there is a "main" module which content root is the src/main and includes only main classes and resources, and there is a "test" module as well. The modules just don't look right. Is this an expected behavior?

The Idea is Intellij Idea 2016.1.1 and the gradle is 2.11

Here is the content of build.gradle

apply plugin: 'idea'
apply plugin: 'java'
apply plugin: 'spring-boot'
apply plugin: "jacoco"

version = getVersion()

sourceCompatibility = 1.8
targetCompatibility = 1.8

configurations {
    provided
}


sourceSets {
    main {
        compileClasspath += configurations.provided
    }
    test {
        resources {
            srcDir 'src/test/data'
        }

        compileClasspath += configurations.provided
    }
}

processResources {
    filter { String line -> line.replace("{version}", getVersion()) }
}

processTestResources {
    filter { String line -> line.replace("{version}", getVersion()) }
}

idea {
    module {
        scopes.PROVIDED.plus += [configurations.provided]
    }
}

repositories {
    mavenCentral()
}
Yugang Zhou
  • 7,123
  • 6
  • 32
  • 60

3 Answers3

36

UPDATE:

It appears Jetbrains removed the checkbox I was referring to since I posted this. galcyurio's answer looks like the correct way to disable this functionality in current builds.

Original Answer:

You can turn this off in Gradle Settings. Preferences -> Build, Execution, Deployment -> Build Tools -> Gradle: uncheck create separate modules per source set.

I had to turn it off as it was causing build failures because Make was only pulling in the main source set. This is likely a bug in Intellij because gradle can still build the projects just fine.

Alex Spence
  • 1,478
  • 3
  • 17
  • 22
  • 3
    After 4 days of searching to resolve Querydsl error which generated classes can't find beans and vice-versa, this solves the problem. Thanks. – cmlonder Aug 09 '17 at 14:35
  • 9
    Seems like there's no more `create separate modules per source set` option in IDEA 2019.2 – Dzmitry Lazerka Aug 07 '19 at 20:29
13

I have the same effect which is not what I expected especially as IntelliJ 14 Ultimate behaves completely different on the same project.

This is the new behavior of IntelliJ introduced with version 2016.1 to handle complex Gradle projects (which never worked completely before when you introduced new source sets).

It looks a little bit strange but works (better than before). And you can see the dependencies for each source set in the gradle project window now.

See also What's New in IntelliJ IDEA 2016.1 and Gradle Goodness: Source Sets As IntelliJ IDEA Modules.

Arne Burmeister
  • 20,046
  • 8
  • 53
  • 94
12

Above 2019.2

I looked for intellij's settings here and there, but couldn't find options. So I checked the source of the community version on GitHub. This option is deprecated. Not sure, but it seems to be set invisible.

This setting is deprecated and remains only for troubleshooting since it's not fully compatible with Gradle's model. Please consider restoring it to the default (checked)

See lines 20-22 of this file.


Environment

  • IntelliJ IDEA 2017.2 (Build #IU-172.3317.76, built on July 15, 2017)
  • Windows 10
  • Gradle project

Trouble shooting

In my case, I cannot find the option which 'Alex Spence' mentioned.

You can turn this off in Gradle Settings. Preferences -> Build, Execution, Deployment -> Build Tools -> Gradle: uncheck create separate modules per source set.

I searched about that option, fortunately a lot of other answers showed me where is that option.

Solution

We can uncheck this option when we import or create module and import project.

when import project

  • File -> New -> Project from existing sources... -> select file -> Gradle -> uncheck create separate modules per source set
  • Welcome page -> Import project -> select file -> Gradle -> uncheck create separate modules per source set

when import module

  • Project Structure -> Add -> import module -> select file -> Gradle -> uncheck create separate modules per source set

when create new module

  • You can also uncheck this option when you create new module.
galcyurio
  • 1,776
  • 15
  • 25
  • This checkbox doesn't exist anymore, anywhere, in IntelliJ IDEA 2019.2... Does someone have a solution working with this version ? – Eria Sep 13 '19 at 11:28
  • 2
    @Eria there is a workaround here https://youtrack.jetbrains.com/issue/IDEA-222172 – Aaron Oct 16 '19 at 00:26