Gradle子模块评估依赖

时间:2016-07-10 10:59:03

标签: spring spring-boot gradle spring-cloud

我有一堆项目,其配置应该基于模板(只有名称更改,不同的依赖项)。为此,我定义了一个名为" server"的父项目。它包含子项目部分中的常见配置部分。

group 'com.kbhit.orangebox.server'
version '1.0-SNAPSHOT'

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "org.springframework.boot:spring-boot-gradle-plugin:1.3.5.RELEASE"
        classpath 'com.bmuschko:gradle-docker-plugin:3.0.1'
    }
}

import com.bmuschko.gradle.docker.tasks.container.DockerCreateContainer
import com.bmuschko.gradle.docker.tasks.container.DockerStartContainer
import com.bmuschko.gradle.docker.tasks.container.DockerStopContainer
import com.bmuschko.gradle.docker.tasks.image.DockerBuildImage
import com.bmuschko.gradle.docker.tasks.image.Dockerfile


ext.defineArtifact = { Map artifact ->

    ext {
        projectName = "$artifact.name"
        projectVersion = '1.0.0'
        artifactName = "$artifact.name-artifact.version.jar"
        dockerBuildDir = "$buildDir/docker/"
    }

    springBoot {
        executable = true
    }

    docker {
        url = 'http://192.168.99.101:2376'
        certPath = file(System.getProperty('docker.cert.path') ?: "${System.getProperty('user.home')}/.docker/machine/machines/default")
    }

    jar {
        baseName = projectName
        version = projectVersion
    }

    task createDockerfile(type: Dockerfile) {
        destFile = file("$buildDir/docker/Dockerfile")
        from 'java:8u91'
        exposePort 8080
        copyFile "$buildDir/docker/app.jar", '/app/app.jar'
        workingDir '/app'
        defaultCommand 'java', '-jar', 'app.jar'
    }

    task buildImage(type: DockerBuildImage, group: 'docker') {
        dependsOn 'bootRepackage', 'createDockerfile'
        inputDir = file("$buildDir/docker")

        doFirst {
            copy {
                from "$buildDir/libs"
                into dockerBuildDir
                include artifactName
                rename artifactName, 'app.jar'
            }
        }
    }

    task createContainer(type: DockerCreateContainer, group: 'docker') {
        dependsOn buildImage
        targetImageId { buildImage.getImageId() }
        containerName projectName
    }

    task startContainer(type: DockerStartContainer, group: 'docker') {
        dependsOn createContainer
        targetContainerId { createContainer.getContainerId() }
    }

    task stopContainer(type: DockerStopContainer, group: 'docker') {
        targetContainerId { createContainer.getContainerId() }
    }

}

subprojects {

    apply plugin: 'java'
    apply plugin: 'idea'
    apply plugin: 'spring-boot'
    apply plugin: 'com.bmuschko.docker-remote-api'

    sourceCompatibility = 1.8
    targetCompatibility = 1.8

    repositories {
        maven {
            url('http://localhost:10001/content/groups/public/')
        }
    }

    dependencyManagement {
        imports {
            mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Brixton.RELEASE'
        }
    }

    dependencies {
        compile("org.springframework.boot:spring-boot-starter-web") {
            exclude module: "spring-boot-starter-tomcat"
        }
        compile group: 'io.springfox', name: 'springfox-swagger2', version: '2.5.0'
        compile group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.5.0'
        compile("org.springframework.boot:spring-boot-starter-jetty")
        compile("org.springframework.boot:spring-boot-starter-actuator")
        testCompile("junit:junit")
        testCompile("org.springframework.boot:spring-boot-starter-test")
    }

    task wrapper(type: Wrapper) {
        gradleVersion = '2.3'
    }

}


afterEvaluate {
    task startServer() {
        dependsOn subprojects.startContainer
    }
}

以下是其中一个模块的配置:

defineArtifact(name: 'orb-api-gateway', version: '1.0.0')

dependencies {
    compile 'org.springframework.cloud:spring-cloud-starter-zuul'
    compile 'org.springframework.cloud:spring-cloud-starter-eureka'
    compile 'org.springframework.cloud:spring-cloud-starter-config'
    testCompile group: 'com.jayway.restassured', name: 'rest-assured', version: '2.9.0'
}

问题在于:

  1. 儿童项目需要"注入"他们的财产" up"在任务定义中请求之前,例如。 buildImage任务 - 我使用$dockerBuildDir变量,该变量应该根据子模块提供的参数在defineArtifact方法中定义。
  2. 父模块必须能够访问为子模块创建的任务。请参阅底部的 startServer 任务。
  3. 当然,在没有创建任务的情况下,这不会起作用。如果我要解决这个问题,我必须让服务器项目在其子项之后进行评估,但之后我会无法在子项请求之前定义 defineArtifact 方法。如何解决这个问题?

0 个答案:

没有答案