单独的functionalTest任务以在多项目gradle构建中运行docker功能测试

时间:2019-06-24 11:02:01

标签: gradle build.gradle

我有一个多项目gradle java设置,从根级别开始,我想运行应在“ project-1”中运行编译并运行单元测试的构建,然后创建一个docker映像,然后运行功能使用该docker映像进行测试。命令行是:

./gradlew clean build dockerBuild functionalTest

当运行上述gradle build命令时,它会在单元测试阶段执行功能测试,或者在我将“ testImplementation”用于依赖项时执行,或者在我将“ functionalTestImplementation”用于“ project-1-ft”时引发编译错误< / p>

目录结构为:

root
- project-1
- functional-tests
-- project-1

和settings.gradle是:

include ':project-1'
include ':project-1-ft'
project(':project-1-ft').projectDir = new File('functional-tests/project-1')

and build.gradle是:

import com.bmuschko.gradle.docker.tasks.image.DockerBuildImage
import com.commercehub.gradle.plugin.avro.GenerateAvroJavaTask

buildscript {
    dependencies {
        classpath 'com.avast.gradle:gradle-docker-compose-plugin:0.9.1'
    }
}

plugins {
    id "java"
    id "org.springframework.boot" version "2.1.4.RELEASE" apply false
    id 'pl.allegro.tech.build.axion-release' version "1.10.0"
    id 'com.bmuschko.docker-remote-api' version "3.2.1" apply false
    id 'com.commercehub.gradle.plugin.avro-base' version "0.9.1"
}

group {
    'my.group'
}

ext {
    springBootVersion = '2.1.4.RELEASE'
    springFrameworkVersion = '4.0.6.RELEASE'
    kafkaVersion = '2.2.5.RELEASE'
    avroVersion = '1.8.2'
    avroSerializerVersion = '5.2.1'
    hazelcastVersion = '3.12'
    cucumberVersion = '1.2.4'
}

allprojects {
    repositories {
        mavenLocal()
        maven {
            name 'nexus'
            url "${System.getenv('NEXUS_URL') ?: mavenRepo_url}/repo"
            credentials {
                username = "${System.getenv('NEXUS_USER')"
                password = "${System.getenv('NEXUS_PASSWORD')"
            }
        }
        maven {
            name "confluent"
            url "http://packages.confluent.io/maven"
        }
    }
}

project(':project-1') {
    apply plugin: 'java'
    apply plugin: 'org.springframework.boot'
    apply plugin: 'com.bmuschko.docker-remote-api'

    sourceCompatibility = 1.8
    targetCompatibility = 1.8

    dependencies {

        implementation "org.projectlombok:lombok:1.16.2"
        implementation "com.hazelcast:hazelcast:${hazelcastVersion}"
        implementation "com.hazelcast:hazelcast-spring:3.12"
        implementation ("io.confluent:kafka-avro-serializer:${avroSerializerVersion}") {
            exclude group:'org.slf4j', module:'slf4j-log4j12'
        }
        implementation 'joda-time:joda-time:+'
        implementation "org.apache.avro:avro:${avroVersion}"
        implementation "org.springframework.boot:spring-boot-starter-web:${springBootVersion}"
        implementation "org.springframework.boot:spring-boot-configuration-processor:${springBootVersion}"
        implementation "org.springframework.kafka:spring-kafka:${kafkaVersion}"

        testImplementation 'junit:junit:4.12'
        testImplementation "org.springframework.boot:spring-boot-starter-test:${springBootVersion}"
    }

    String gitHash = 'git rev-parse --short HEAD'.execute().text.trim()
    ext.getImageTag = "${System.env.DOCKER_REGISTRY_PUSH_HOST ?: "local"}/${project.name}:${gitHash}"

    bootJar {
        mainClassName = 'my.group.Application'
    }

    scmVersion {
        tag {
            prefix = project.name
        }
    }

    project.version = scmVersion.version

    def repository = 'artifacts.myrepo.com:5001/mygroup'
    def artifactId = "${project.name}-${project.version}"
    def projectTag = "${repository}/${project.name}"

    task dockerBuild(type: DockerBuildImage) {
        dependsOn build
        description 'Builds the docker image'
        dockerFile project.file('docker/Dockerfile')
        inputDir = projectDir
        buildArgs = ['JAR_FILE': "${artifactId}.jar"]

        tags.add("${projectTag}:latest".toString())
        tags.add("${projectTag}:${project.version}".toString())
    }

    tasks.add(dockerBuild)
}


project(':project-1-ft') {
    apply plugin: 'java'
    apply plugin: 'docker-compose'

    configurations {
        functionalTestImplementation.extendsFrom testImplementation
    }

    sourceCompatibility = 1.8
    targetCompatibility = 1.8

    sourceSets {
        functionalTest {
            java {
                compileClasspath += main.output + test.output
                runtimeClasspath += main.output + test.output
                srcDir file('functional-tests/project-1/src/test/java')
            }
            resources.srcDir file('functional-tests/project-1/src/test/resources')
        }
    }

    dependencies {
        functionalTestImplementation "ch.qos.logback:logback-classic:1.2.3"
        functionalTestImplementation 'org.awaitility:awaitility:3.0.0'

        functionalTestImplementation "org.springframework.boot:spring-boot-starter-web:${springBootVersion}"
        functionalTestImplementation "org.springframework.boot:spring-boot-starter-test:${springBootVersion}"
        functionalTestImplementation "info.cukes:cucumber-core:${cucumberVersion}"
        functionalTestImplementation "info.cukes:cucumber-java:${cucumberVersion}"
        functionalTestImplementation "info.cukes:cucumber-junit:${cucumberVersion}"
        functionalTestImplementation "info.cukes:cucumber-spring:${cucumberVersion}"
    }

    task functionalTest(type: Test) {
        testClassesDirs = sourceSets.functionalTest.output.classesDirs
        classpath = sourceSets.functionalTest.runtimeClasspath
        outputs.upToDateWhen { false }
    }

    def dockerBuildTask = project(':project-1').dockerBuild

    dockerCompose {
        dependsOn(dockerBuildTask)
        useComposeFiles = ['docker/docker-compose.yml']
        removeContainers = true
        removeVolumes = true
        buildBeforeUp = true
        waitForTcpPorts = false
    }

    dockerCompose.isRequiredBy(functionalTest)

    functionalTest.mustRunAfter dockerBuildTask
    functionalTest.mustRunAfter test
}

task generateAvroClasses(type: GenerateAvroJavaTask) {
    source("src/main/avro")
    outputDir = file("src/main/java")
}

我想要gradle任务执行以下操作:

test - should only run unit tests in "project-1" (working but also runs functional tests).
dockerBuild - should generate docker image
functionalTest - should run docker compose and run Cucumber based functional tests.

0 个答案:

没有答案