Alternative way of copying resources from src/main/java, since idea doesn't handle sourceSets.main.resources.srcDir


(eirik.lygre) #1

We have for a while modified the resources srcDir to make sure resources are copied from the java directories, like this:

sourceSets.main.resources.srcDir 'src/main/java'
    sourceSets.test.resources.srcDir 'src/test/java'

This works nicely in gradle. However, I now want to try intellij idea on this project, and this leads to a non-working project, as seen in the below fragment from the .iml-file. Presumably idea sees the src/main/java-directory both as a srcDir and as a resourceDir, and gets confused:

<sourceFolder url="file://$MODULE_DIR$/src/main/java" type="java-resource" />
      <sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
      <sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
      <sourceFolder url="file://$MODULE_DIR$/src/test/java" type="java-test-resource" />

We are looking at the following workaround, and the question is whether this looks like a reasonable solution, or whether there are more-or-less obvious gotchas…

task processMainJavaResources(type:ProcessResources) {
    from sourceSets.main.java.srcDirs
    into sourceSets.main.output.resourcesDir
    exclude '**/*.java'
}
task processTestJavaResources(type:ProcessResources) {
    from sourceSets.test.java.srcDirs
    into sourceSets.test.output.resourcesDir
    exclude '**/*.java'
}
processResources.dependsOn processMainJavaResources
processTestResources.dependsOn processTestJavaResources

The issue has been reported to JetBrains: http://youtrack.jetbrains.com/issue/IDEA-118280.

Eirik


(eirik.lygre) #2

Or, perhaps the much simpler solution like this:

// JetBrains IDEA automatically copies resources from the main java directory
if (! System.getProperty("idea.active")) {
    sourceSets.main.resources.srcDir 'src/main/java'
    sourceSets.test.resources.srcDir 'src/test/java'
}