Reputation: 42239
Scenario
I am building a Kotlin application using Gradle modules in IntelliJ IDEA.
I have encountered a problem where for every Gradle module I create in the project, IntelliJ IDEA automatically creates and/or re-creates directory structures for source sets that I don't want.
I am running IntelliJ IDEA 2018.1.2 on Windows 10 x64.
Steps To Reproduce
The initial project structure should look like this. I've expanded only the directories that are relevant to this question.
template-app
├ .idea
├ gradle
├ src
│ ├ main
│ │ ├ java
│ │ ├ kotlin
│ │ └ resources
│ └ test
│ ├ java
│ ├ kotlin
│ └ resources
├ build.gradle
├ gradlew
├ gradlew.bat
└ settings.gradle
I don't need the src
directory so I've deleted it and now the project structure looks like this.
template-app
├ .idea
├ gradle
├ build.gradle
├ gradlew
├ gradlew.bat
└ settings.gradle
Next I create a new Gradle module (mod-one) in my project, and now the project structure looks like this.
template-app
├ .idea
├ gradle
├ mod-one
│ ├ src
│ │ ├ main
│ │ │ ├ java
│ │ │ ├ kotlin
│ │ │ └ resources
│ │ └ test
│ │ ├ java
│ │ ├ kotlin
│ │ └ resources
│ └ build.gradle
├ src
│ ├ main
│ │ ├ java
│ │ ├ kotlin
│ │ └ resources
│ └ test
│ ├ java
│ ├ kotlin
│ └ resources
├ build.gradle
├ gradlew
├ gradlew.bat
└ settings.gradle
Notice that mod-one now contains a src
directory, but not only that, the src
directory I deleted previously has also been recreated, including it's sub-directories.
My Intended Structure
The parent module (e.g. template-app) does not require a src
directory at all. There will be no code at this level of the project.
Each module (e.g. mod-one) will not contain any tests, and therefore will only require a main
source-set/module but not a test
source-set/module.
Tests will be contained in a single module (e.g. mod-tests), and therefore will only require a test
source-set/module but not a main
source-set/module.
There will be no Java code, therefore I do not require java
directories.
For the most part, I will not require resources
and I am happy to add these manually where necessary.
Based on the points above, the structure should look like this.
template-app
├ .idea
├ gradle
├ mod-one
│ ├ src
│ │ ├ main
│ │ │ └ kotlin
│ └ build.gradle
├ mod-test
│ ├ src
│ │ └ test
│ │ └ kotlin
│ └ build.gradle
├ build.gradle
├ gradlew
├ gradlew.bat
└ settings.gradle
Question
Why is IntelliJ IDEA creating and/or recreating directories I don't want, and how can I stop this?
Upvotes: 1
Views: 803
Reputation: 14493
Even if this behavior should not occur with the option from the answer of @Andrey deactivated, you could try to prevent IntelliJ from creating the directories by removing the language plugin from Gradle for your root project, since you don't want use it for sources anyhow.
When you create a new Gradle project or module in IntelliJ and choose a language as additional framework (e.g. Java or Kotlin), the according Gradle plugin will be added to the build script automatically, the build.gradle
file will contain something like:
apply plugin: 'java'
// OR
apply plugin: 'kotlin'
However, you don't need to apply these plugins to a Gradle project that won't have any sources. If you later add a module (subproject), just specify the additional language framework for this module.
Regarding your desired project structure: Please note that you are violating conventions, which is possible, but please reconsider if you really want to. While it is plain simple to add and extend source sets in Gradle, removing the predefined source sets may be harder to achieve (try the removeIf
method of the underlying Collection
of the SourceSetContainer
).
Last but not least, you should consider not caring at all about empty directories. It is plain simple to hide them via your IDE and Git won't add them to version control, if they stay empty. Don't overengineer your Gradle build for a little to no additional value!
Edit: I just remembered that you can apply the java-base
plugin instead of the java
plugin to create a source set container without adding default source sets. However, I am not sure whether there exists a similar base plugin for Kotlin development.
Upvotes: 1
Reputation: 16381
Un-check the Create directories for empty content roots automatically option in Settings(Preferences) | Build, Execution, Deployment | Build Tools | Gradle.
With this option enabled on every re-import from gradle IDE automatically creates the directory structure according to your build.gradle files.
Upvotes: 1