Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include build directory as source directory in intellij when compiling with gradle

By default, the gradle idea plugin marks the build folder as excluded. How do I include this folder as source folder? (or avoid excluding it, as it appears to be by default...)

In my module build.gradle file I tried with the two following configurations:

idea {
    module {
        excludeDirs -= file('build/generated')
    }
}

and:

idea {
    module {
        sourceDirs += file('build/generated')
    }
}

With those two configurations the folder build/generated always appears as excluded folders in IntelliJ after compilation. In IntelliJ I always have to go in "Project Settings", "Modules" and then in the tab "Sources" to remove the build folder from the excluded folders and let my project run.

like image 207
RiRomain Avatar asked Jan 08 '14 17:01

RiRomain


People also ask

How do I change the source directory in IntelliJ?

From the main menu, select File | Project Structure Ctrl+Alt+Shift+S and click Project Settings | Modules. Select the necessary module and then open the Sources tab in the right-hand part of the dialog. Click Add Content Root and specify the folder that you want to add as a new content root.

How do I create a source folder in IntelliJ?

In the Project tool window ( Alt+1 ), right-click the node in which you want to create a new directory and select New | Directory. Alternatively, select the node, press Alt+Insert , and click Directory. Name the new directory and press Enter .


1 Answers

You definitely want the build directory to be excluded in IntelliJ. Otherwise, indexing will take longer, you'll get duplicates in searches, etc. Since IntelliJ doesn't support including a subdirectory of an excluded directory, my preferred solution is to put generated files into a directory outside build. For example, you could put them into generated (relative to the project directory), and configure the clean task accordingly:

clean {
    delete "generated"
}

Another option is to exclude all subdirectories of build except build/generated. However, given that the directories to be excluded will need be listed explicitly, this takes more effort, and bears the risk of being fragile. (You don't want this to break every time a plugin/task/etc. adds a new subdirectory.)

like image 123
Peter Niederwieser Avatar answered Sep 22 '22 09:09

Peter Niederwieser