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

Gad*_*adi 6 java plugins unit-testing gradle docker

使用 gradle 4.7

我想为测试集成类添加新的源集。与主测试源集分开,它将有一些其他依赖项,并且将有单独的任务来运行测试。

可以使用自定义 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());
            });

        });
Run Code Online (Sandbox Code Playgroud)

Zas*_*asz 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
  }

}
Run Code Online (Sandbox Code Playgroud)