简体   繁体   English

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

[英]how to add sourceset in gradle java custom plugin

using gradle 4.7使用 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?可以使用自定义 java gradle 插件来完成吗?

Here is the code and the project using it.这是代码和使用它的项目。

https://github.com/gadieichhorn/gradle-java-multimodule/tree/master/buildSrc 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.因为这些测试使用从构建生成的 docker 镜像,所以它应该只在构建之后运行,而不是像正常测试那样。

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

        });

I had added a new sourceSet with groovy - I hope it can act as reference for the java equivalent you are trying.我添加了一个带有 groovy 的新 sourceSet - 我希望它可以作为您正在尝试的 java 等价物的参考。 Consider writing the plugin itself in groovy.考虑用 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
  }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM