Search code examples
gradlesource-sets

Define a sourceset for all java sub-projects


I wonder if it is possible to define a source set in gradle (e.g. integrationTests) that can be applied to all sub-projects that use the 'java' plugin?

Something like

sourceSets {
    integTest {
        java.srcDir file('src/integration-test/java')
        resources.srcDir file('src/integration-test/resources')
        compileClasspath = sourceSets.main.output + configurations.integTest
        runtimeClasspath = output + compileClasspath
    }
}

as mentioned here applied to all java sub-projects?

Thanks for useful hints!


Solution

  • You can filter the subprojects of your build by applied plugin. In your example this would look like this:

    def javaProjects() {
        subprojects.findAll { project -> 
            project.plugins.hasPlugin('java') 
        }
    }
    
    configure(javaProjects()) {
        sourceSets {
            integTest {
                java.srcDir file('src/integration-test/java')
                resources.srcDir file('src/integration-test/resources')
                compileClasspath = sourceSets.main.output + configurations.integTest
                runtimeClasspath = output + compileClasspath
            }
        }
    }