Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add sourceset in gradle java custom plugin

using gradle 4.7

I would like to add new sourceset for test integration classes. Seperated from the main test sourceset it will have some other dependencies and would have seperate task to run the tests.

Can it be done using a custom java gradle plugin?

Here is the code and the project using it.

https://github.com/gadieichhorn/gradle-java-multimodule/tree/master/buildSrc

beacuse these tests use the docker image produced from the build it should only run after the build, not like the normal tests would.

any sample or contribution will be appreciated.

        project.getPlugins().withType(JavaPlugin.class, javaPlugin -> {

            JavaPluginConvention javaConvention = project.getConvention().getPlugin(JavaPluginConvention.class);

            SourceSet main = javaConvention.getSourceSets().getByName(SourceSet.MAIN_SOURCE_SET_NAME);
            SourceSet test = javaConvention.getSourceSets().getByName(SourceSet.TEST_SOURCE_SET_NAME);

            final Configuration integrationImplementation = project.getConfigurations().create("integrationImplementation")
                    .setExtendsFrom(Arrays.asList(project.getConfigurations().getByName("testImplementation")))
                    .setVisible(false)
                    .setDescription("Integration Implementation");

            project.getDependencies().add(integrationImplementation.getName(), "org.testcontainers:testcontainers:1.7.1");

            final Configuration integrationRuntimeOnly = project.getConfigurations().create("integrationRuntimeOnly")
                    .setExtendsFrom(Arrays.asList(project.getConfigurations().getByName("testRuntimeOnly")))
                    .setVisible(false)
                    .setDescription("Integration Runtime Only ");

//            project.getDependencies().add(integrationRuntimeOnly.getName(), "org.testcontainers:testcontainers:1.7.1");

            final SourceSet integration = javaConvention.getSourceSets().create("integration", sourceSet -> {
                sourceSet.getJava().srcDir(Arrays.asList("src/integration/java"));
                sourceSet.getResources().srcDir("src/integration/resources");
                sourceSet.setCompileClasspath(project.files(main.getOutput(), test.getOutput()));
                sourceSet.setRuntimeClasspath(project.files(main.getOutput(), test.getOutput()));
                sourceSet.setRuntimeClasspath(sourceSet.getOutput());
            });

            project.getTasks().create("e2e", Test.class, e2e -> {
                e2e.setTestClassesDirs(integration.getOutput().getClassesDirs());
                e2e.setClasspath(integration.getRuntimeClasspath());
            });

        });
like image 276
Gadi Avatar asked May 31 '26 09:05

Gadi


1 Answers

I had added a new sourceSet with groovy - I hope it can act as reference for the java equivalent you are trying. Consider writing the plugin itself in groovy.

class CustomPlugin implements Plugin<Project> {
  @Override
  void apply(final Project project) {   
        // add a source set
        File sourcesDir = project.file("/some/path")    
        project.sourceSets {
          myNewEndToEndTest {
            java.srcDirs += [sourcesDir]
          }
        }

        project.configurations.create('yourNewConfig')
        project.dependencies {
            // Add some dependencies here that your e2e test run needs
            // Example: yourNewConfig "org.junit:junit-core:5.0"
        }              
        // you can also use the project object to create tasks, taskDependencies, configurations etc
  }

}
like image 177
Zasz Avatar answered Jun 02 '26 23:06

Zasz