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

gkn4icbw  于 2023-10-19  发布在  Java
关注(0)|答案(2)|浏览(122)

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

        });
qv7cva1a

qv7cva1a1#

我用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
  }

}
vyu0f0g1

vyu0f0g12#

使用The JVM Test Suite官方Gradle插件:

plugins {
    java
    `jvm-test-suite`
}

testing {
    suites {
        dependencies {
            // add deps for all test source sets
        }

        register("integrationTest", JvmTestSuite::class) {
            dependencies {
                // add for new test source set
            }
        }
    }
}

相关问题