How to include build directory as source directory

2019-04-05 18:07发布

问题:

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.

回答1:

change code from

file('build/generated')

to

file("$buildDir/generated")

I use here is a working code:

ext {
    cxfOutputDir = file("$buildDir/generated-sources/cxf")
}
idea.module {
    excludeDirs -= file("$buildDir")
    sourceDirs += cxfOutputDir
}


回答2:

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.)



回答3:

This works for me!

apply plugin: 'idea'

idea {
    module {
        excludeDirs -= buildDir
    }
}


回答4:

Use standard location for generated source code - supported without additional configuration:

${project.buildDir}/generated-sources/something

or

${project.buildDir}/generated-test-sources/something

for generated code for tests only.

something means technology, example: jpamodel, cxf etc.



回答5:

First method

['integration/src/generated'].each {
            idea.module.sourceDirs += file(it)
            sourceSets.main.java.srcDir it
            compileJava.source file(it)
    }

second method

project.ext {
        jaxbTargetDir = file("src/generated/java")
}

idea.module {
        excludeDirs -= file("$buildDir")
        sourceDirs += jaxbTargetDir
}