Migrate plugin build script to a java plugin

plugins

(Gadi Eichhorn) #1

I am using testcontainers docker test runner and built this gradle script I use across my multimodule project. I would like to move it to a formal java plugin. here are some of my requirments:

  1. seperate sourceset
  2. include dependencies
  3. test task to run the tests on this sourceset

the build cycle is:

  1. java build
  2. java test
  3. build docker images (plugin)
  4. integration tests (with new plugin)

I used this doc to get starting but I am stuck now… -> https://guides.gradle.org/implementing-gradle-plugins/?_ga=2.79110996.1380187462.1527197136-786375745.1522570666

here is the build scrip I whish to migrate

sourceSets {
    integration {  // the new source set next to src {main , test, ...) 
        java {
            compileClasspath += main.output + test.output
            runtimeClasspath += main.output + test.output
            srcDir file('src/integration/java')

            integration.runtimeClasspath += integration.output
        }
        resources.srcDir file('src/integration/resources')
    }
}

configurations {
    integrationImplementation.extendsFrom testImplementation
    integrationRuntimeOnly.extendsFrom testRuntimeOnly

  /// the dependency I need for the tests
    integrationCompile group: 'org.testcontainers', name: 'testcontainers', version: testcontainers
}

task e2e(type: Test) {
    testClassesDirs += sourceSets.integration.output.classesDirs
    classpath = sourceSets.integration.runtimeClasspath
}

thanks in advance.

code on github -> https://github.com/gadieichhorn/gradle-java-multimodule/blob/master/buildSrc/src/main/java/com.rds.example.gradle/IntegrationPlugin.java

contributions are welcomed :slight_smile:


(Gadi Eichhorn) #2

Making some progress, still getting errors in build.

here is the plugin class

@Slf4j
public class IntegrationPlugin implements Plugin<Project> {

    public void apply(Project project) {

        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.setCompileClasspath(project.files(main.getOutput(), test.getOutput()));
                sourceSet.setRuntimeClasspath(project.files(main.getOutput(), test.getOutput()));
                sourceSet.getJava().srcDir(Arrays.asList("src/integration/java"));
                sourceSet.setRuntimeClasspath(sourceSet.getOutput());
                sourceSet.getResources().srcDir("src/integration/resources");
            });

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

        });


    }

}

and here is buil derros I get.
I dont think I got the class and dependencies right.

...
/home/geichhorn/Develop/github/gradle-java-multimodule/deploy/rest/src/integration/java/com/rds/example/gradle/deploy/DeployTest.java:16: error: cannot find symbol
    private static GenericContainer main = new GenericContainer("com.rds.example.gradle/rest:latest")
                   ^
  symbol:   class GenericContainer
  location: class DeployTest
...