Mark src/main/resources directories and src/test/resources at 'resource Folder' and 'test resource folder' in IntelliJ

Continuing the discussion from The idea plugin breaks the new Intellij 13 .iml configuration:

This does not work for me. Does it need to be whenMerged?

I am using gradle 2.11 and intelliJ 14.1. Any pointers?

Something like this should work:

idea { 
    module.iml.withXml {
        def node = it.asNode()
        def content = node.component.find { it.'@name' == 'NewModuleRootManager' }.content[0]
        content.sourceFolder.each { sourceFolder ->
            if(sourceFolder.@url?.endsWith('/resources')) {
                sourceFolder.attributes().with {
                    boolean isTestSource = (remove('isTestSource') == 'true')
                    put('type', isTestSource ? 'java-test-resource' : 'java-resource')
1 Like

Thanks! That worked.

But if I understand correctly (from the answer to this question, for example), changes applied via withXml won’t take effect if you import the project using IntelliJ’s Gradle plugin, and yet using IntelliJ’s Gradle plugin appears to be the officially recommended way to work with IntelliJ since at least 2013 (based on the comments on the original question).
As I understand it, doing this workaround requires that we run gradle ideaModules but don’t import the project as a Gradle project in IntelliJ, but then most of the IDE integration niceties won’t work. Or, we can first import the project as a Gradle project in IntelliJ, then go back to the command line and run the ideaModules task, but then anytime we use the IntelliJ Gradle plugin’s “refresh project” option we’d lose the change to the modules unless we went back and ran gradle ideaModules again. Is that correct?

Correct. IntelliJ Gradle integration is developed and maintained by JetBrains. I recommend bringing up the issue on IntelliJ forums and/or issue tracker.

What issue are you referring to @lhotari ? The issue I’m seeing here appears to be firmly with Gradle, not Jetbrains.
Gradle’s IdeaModule has no way of specifying that a resource or test resource directory. IntelliJ has a way to indicate it in the .iml files – as the workarounds linked to. Gradle just doesn’t support it unless you explicitly fix the XML file that Gradle writes with a withXml block.
I don’t know what Jetbrains could do to differently, except perhaps run withXml hooks when importing. But that would just be supporting the really kludgy workaround. The root issue, though – that you can specify sourceDirs and testSourceDirs but not resourceDirs or testResourceDirs within Gradle’s Idea plugin – is a Gradle issue.

@ninethirty , yes you are right. There currently isn’t a way to express that a source set is type of resources in the Idea model of Gradle. I was assuming that it would be possible to determine this by the fact that resources in the Gradle Java plugin are in the source set that is called “resources”. However the current model doesn’t expose this information.

@Rene Are there some plans to improve the IDE integration in this area?

I’ve played with this some more, so in case others find this thread after seeing issues: if you’re importing a Gradle project via IntelliJ’s Gradle plugin (vs. using Gradle to generate IntelliJ project files), there are some cases where IntelliJ actually does the right thing and guesses that a directory is a resource directory using the information in the source set. It looks like many cases when the module also has the java plugin applied, it behaves correctly.
There are some other cases, if the module is not a java module, when IntelliJ fails to import it at all, giving an error like Can't register given path of type 'SOURCE' because it's out of the content root (even though the path in the Gradle project is actually inside the content root). This appears to be an IntelliJ bug; I raised IDEA-150844. We’ll see what they say.

It’s still actual. Any update?