4

Using IntelliJ 14 and the 'idea' plugin in Gradle 2.2 for generating IntelliJ projects. I'm able to add a new Test Sources Root for integration tests in the following way:

idea {
    module {
        testSourceDirs += file('src/integrationTest/java')
    }
}

However, I have not found a way to add a corresponding Test Resources Root located at 'src/integrationTest/resources'. Any ideas on how to do this? Many thanks in advance.

-Daniel

Daniel
  • 985
  • 2
  • 14
  • 18

2 Answers2

1

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')
                }
            }
        }
    }
}
LazerBanana
  • 6,865
  • 3
  • 28
  • 47
  • Can confirm that this works pretty well (at least with IntelliJ 2016.1.3). Note that this needs to be applied to each individual subproject in a multi-project build. – Oliver Charlesworth Aug 25 '16 at 09:39
  • What about adding it into subprojects {} ? in the root projects - build.gradle ? Or even beter create a script with name of your choice lets say config.gradle and put it there then just apply it to what you need. – LazerBanana Aug 26 '16 at 13:02
  • 1
    Yup, that's what I'm doing :) – Oliver Charlesworth Aug 26 '16 at 13:02
0

Just went through this trying to use the Cucumber plugin. The plugin correctly marks the src/cucumber/java and src/cucumber/groovy but not src/cucumber/resources, so the IntelliJ cucumber support gets confused. Played around with the above suggestions and this is working now:

idea {
    // Workaround to make sure the cucumber resources folder is
    // marked as "Test Resource Root" in IntelliJ. Otherwise the
    // IntelliJ cucumber integration gets confused wrt looking
    // up step definitions, highlighting, etc.
    module {
        testSourceDirs += file('src/cucumber/resources')
    }
    module.iml.withXml {
        def node = it.asNode()
        def content = node.component.find { it.'@name' == 'NewModuleRootManager' }.content[0]

        def cucumberSources = content.sourceFolder.findAll { it.@url?.contains('/src/cucumber/resources') }
        cucumberSources.each {
            it.@type = 'java-test-resource'
        }
    }
}
Dan
  • 293
  • 1
  • 3
  • 8