Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why my gradle projects creates separated modules for main and test in Intellij Idea

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()
}
like image 603
Yugang Zhou Avatar asked May 04 '16 04:05

Yugang Zhou


People also ask

How do I sync Gradle project in IntelliJ?

If you have some custom plugins that require you to import your project from the IntelliJ IDEA model, press Ctrl+Shift+A and search for the Project from Existing Sources action. In the dialog that opens, select a directory containing a Gradle project and click OK. IntelliJ IDEA opens and syncs the project in the IDE.

What is the difference between project and module in IntelliJ?

In IntelliJ IDEA, a module is an essential part of any project – it's created automatically together with a project. Projects can contain multiple modules – you can add new modules, group them, and unload the modules you don't need at the moment.


3 Answers

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.

like image 50
Alex Spence Avatar answered Oct 06 '22 00:10

Alex Spence


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.

like image 43
Arne Burmeister Avatar answered Oct 06 '22 00:10

Arne Burmeister


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.

  • IntelliJ IDEA and Gradle: Why there are 3 modules per sub-module?
  • Control automatic creation of IDEA modules from Gradle sourceSets in 2016.1?

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.
like image 22
galcyurio Avatar answered Oct 06 '22 00:10

galcyurio