如何在Gradle / IntelliJ中添加测试资源Root

时间:2015-01-03 14:24:01

标签: intellij-idea gradle

使用IntelliJ 14和#39;' Gradle 2.2中的插件用于生成IntelliJ项目。我可以通过以下方式为集成测试添加新的测试源根目录:

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

但是,我还没有找到一种方法来添加位于&#s; /rc / integrationTest / resources'的相应测试资源 Root。关于如何做到这一点的任何想法?非常感谢提前。

-Daniel

2 个答案:

答案 0 :(得分:1)

这应该有效:)

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 :(得分:0)

刚刚尝试使用Cucumber插件。插件正确标记src/cucumber/javasrc/cucumber/groovy但不标记src/cucumber/resources,因此IntelliJ黄瓜支持变得混乱。玩了上述建议,现在正在运作:

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'
        }
    }
}