Matthew Layton
Matthew Layton

Reputation: 42239

IntelliJ IDEA - creates or recreates directories I don't want, or have already deleted

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

  1. Open IntelliJ IDEA and "Create New Project"
  2. Create a Gradle project
  3. From "Additional Libraries and Frameworks" check "Kotlin (Java)" only
  4. Give the project a GroupId, ArtifactId and Version
  5. Check "Use auto-import" and un-check "Create separate module per source set"
  6. Finish. At this point the IDE should display and Gradle should sync.

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

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

Answers (2)

Lukas Körfer
Lukas Körfer

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

Andrey
Andrey

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

Related Questions