如何在gradle java自定义插件中添加sourceset

时间:2018-05-28 20:45:00

标签: java unit-testing docker gradle plugins

使用gradle 4.7

我想为测试集成类添加新的sourceset。从主测试源集中分离,它将具有一些其他依赖性,并且将具有单独的任务来运行测试。

可以使用自定义Java gradle插件完成吗?

以下是使用它的代码和项目。

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

因为这些测试使用了从构建产生的docker镜像,它应该只在构建之后运行,而不像正常的测试那样。

任何样本或贡献都将受到赞赏。

        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());
            });

        });

1 个答案:

答案 0 :(得分:0)

我用groovy添加了一个新的sourceSet-我希望它可以作为您尝试使用的Java等效项的参考。考虑使用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
  }

}