链接gradle任务和gradle生命周期

时间:2017-05-26 10:55:49

标签: gradle

我对Gradle生命周期有一个基本的询问。我的项目是一个多模块项目。

我注意到以下命令:

./gradlew clean bignibou-server:run

不等于按以下顺序运行以下两个命令:

./gradlew clean

./gradlew bignibou-server:run

bignibou-server是我的模块之一。

第一个命令的输出导致与以下两个命令不同的输出。

有人可以解释为什么这可能指向相关文档吗?

编辑:在第一种情况下,其中一个生成的源输出被删除,而在第二种情况下不会删除。怎么会这样?

编辑2

来自根build.gradle

import io.franzbecker.gradle.lombok.task.DelombokTask

def javaVersion = 1.8

buildscript {

    repositories {
        mavenCentral()
        maven { url "http://repo.spring.io/release" }
        maven { url "http://repo.spring.io/milestone" }
        maven { url "http://repo.spring.io/snapshot" }
        maven { url "https://plugins.gradle.org/m2/" }
    }

    ext {
        javaLanguageLevel = '1.8'
    }

    ext['mockito.version'] = "${mockitoVersion}"
    ext['hibernate.version'] = "${hibernateVersion}"

    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath("io.franzbecker:gradle-lombok:${gradleLombokVersion}")
    }
}

configure(allprojects) { project ->
    group = "com.bignibou"

    apply plugin: 'java'
    apply plugin: 'eclipse'
    apply plugin: 'idea'

    configurations {
        mapstruct
    }

    repositories {
        mavenCentral()
        maven { url "http://repo.spring.io/release" }
        maven { url "http://repo.spring.io/milestone" }
        maven { url "http://repo.spring.io/snapshot" }
        maven { url "https://artifacts.elastic.co/maven" }
    }

    compileJava {
        sourceCompatibility = javaVersion
        targetCompatibility = javaVersion
    }

    test {
        reports.html.destination = file("$reports.html.destination/unit")
        reports.junitXml.destination = file("$reports.junitXml.destination/unit")
    }

    task delombok(type: DelombokTask) {
        ext.outputDir = file("$buildDir/src-delomboked/java")
        outputs.dir(outputDir)
        sourceSets.main.java.srcDirs.each {
            inputs.dir(it)
            args(it, "-d", outputDir)
        }
    }

    // FIXME: refactor paths to generated sources
    task mapStructClean {
        delete file("build/generated-sources/mapstruct/main")
    }

    task generateMainMapperClasses(type: JavaCompile, group: 'build') {
        ext.aptDumpDir = file("${buildDir}/tmp/apt/mapstruct")
        destinationDir = aptDumpDir

        classpath = compileJava.classpath + configurations.mapstruct
        source = file("$buildDir/src-delomboked/java")
        ext.sourceDestDir = file("build/generated-sources/mapstruct/main")

        options.define(
                compilerArgs: [
                        "-nowarn",
                        "-proc:only",
                        "-encoding", "UTF-8",
                        "-processor", "org.mapstruct.ap.MappingProcessor" + ',lombok.launch.AnnotationProcessorHider$AnnotationProcessor',
                        "-s", sourceDestDir.absolutePath,
                        "-source", rootProject.javaLanguageLevel,
                        "-target", rootProject.javaLanguageLevel,
                ]
        );

        inputs.dir source
        outputs.dir "${buildDir}/generated-sources/mapstruct/main"
        doFirst {
            sourceDestDir.mkdirs()
        }
        doLast {
            aptDumpDir.delete()
        }
    }
}

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

来自bignibou-commmon模块:

description = "Bignibou Common"

apply plugin: 'org.springframework.boot'
apply plugin: 'io.franzbecker.gradle-lombok'

configurations {
    querydslapt
    mapstruct
}


dependencyManagement {
    dependencies {
        dependency "org.elasticsearch:elasticsearch:${elasticsearchVersion}"
    }
}

dependencies {

    compile("org.springframework.boot:spring-boot-starter-data-jpa") {
        exclude group: 'org.apache.tomcat', module: 'tomcat-jdbc'
    }
    compile("org.springframework.boot:spring-boot-starter-mail")
    compile('org.springframework.security:spring-security-core')
    compile('org.hibernate:hibernate-validator')
    compile("org.hibernate:hibernate-java8")
    compile("com.fasterxml.jackson.datatype:jackson-datatype-jsr310")

    //Spring cloud
    compile("org.springframework.cloud:spring-cloud-spring-service-connector")
    compile("org.springframework.cloud:spring-cloud-localconfig-connector")
    compile("org.springframework.cloud:spring-cloud-cloudfoundry-connector")

    // Relational Database
    compile("org.postgresql:postgresql:${postgresqlVersion}")
    compile("org.flywaydb:flyway-core")

    // Connection pooling
    compile("com.zaxxer:HikariCP")

    //Shield
    compile("org.elasticsearch.client:x-pack-transport:${elasticsearchVersion}")
    compile("org.elasticsearch:elasticsearch:${elasticsearchVersion}")
    compile("org.apache.logging.log4j:log4j-api")
    compile("org.apache.logging.log4j:log4j-core")

    // QueryDSL
    compile("com.querydsl:querydsl-core:${queryDslVersion}")
    compile("com.querydsl:querydsl-jpa:${queryDslVersion}")
    querydslapt("com.querydsl:querydsl-apt:${queryDslVersion}")

    // Jackson
    compile("com.fasterxml.jackson.core:jackson-core")
    compile("com.fasterxml.jackson.core:jackson-annotations")

    compile("org.mapstruct:mapstruct-jdk8:${mapstructVersion}")
    mapstruct("org.mapstruct:mapstruct-processor:${mapstructVersion}")
    compile("org.apache.httpcomponents:httpclient:${httpClientVersion}")

    compile("org.jasypt:jasypt:${jasyptVersion}")

}

sourceSets {

    main {
        ext.originalJavaSrcDirs = java.srcDirs
        java.srcDir "build/generated-sources/mapstruct/main"
        output.dir("build/generated-mail-templates")
    }

    generated {
        java {
            srcDirs = ["build/generated-sources/java", "build/src-delomboked/java", "build/generated-sources/mapstruct/main"]
        }
    }
}

bootRepackage {
    enabled = false
}

task generateQueryDSL(type: JavaCompile, group: 'build') {
    description "Generates the QueryDSL query types"
    source = sourceSets.main.java
    classpath = configurations.compile + configurations.querydslapt
    options.compilerArgs = [
            "-proc:only",
            "-processor",
            "com.querydsl.apt.jpa.JPAAnnotationProcessor" + ',lombok.launch.AnnotationProcessorHider$AnnotationProcessor'
    ]
    destinationDir = sourceSets.generated.java.srcDirs.iterator().next()
}

// FIXME: add cssFile &
// FIXME: add dependsOn processMailTemplates &
// FIXME: clean mail templates html
// FIXME: ensure build is stopped if templates are not processed

task npmInstall(type: Exec) {
    description "npm install"
    commandLine 'npm', 'install'
}

task processMailTemplates {
    description "Processes mail templates"
    dependsOn npmInstall

    def templateSrcDir = "src/main/templates/mail/"
    def templateDestDir = "build/generated-mail-templates/META-INF/templates/mail/"

    mkdir templateDestDir

    def templateNames = []

    fileTree(dir: templateSrcDir, include: '**/*.html').visit {
        FileVisitDetails details -> templateNames << details.file.name
    }

    templateNames.each { templateName -> inlineCss(templateSrcDir + templateName, templateDestDir + templateName) }

    outputs.upToDateWhen { false }
}

static def inlineCss(src, dest) {
    def juice = 'node_modules/.bin/juice'
    def juiceResourcesDir = 'src/main/templates/misc/'
    def juiceArgs = "--options-file ${juiceResourcesDir}juiceOptions.json --css ${juiceResourcesDir}mailStyle.css"
    "${juice} ${juiceArgs} ${src} ${dest}".execute(null, new File('bignibou-common'))
}


compileJava {
    dependsOn generateQueryDSL
    source generateQueryDSL.destinationDir
}

compileGeneratedJava {
    dependsOn generateQueryDSL
    options.warnings = false
    classpath += sourceSets.main.runtimeClasspath
}

idea {
    module {
        sourceDirs += file('build/generated-sources/java')
    }
}

processResources.dependsOn processMailTemplates
compileJava.dependsOn generateMainMapperClasses
generateMainMapperClasses.dependsOn mapStructClean
generateMainMapperClasses.dependsOn delombok


clean {
    delete sourceSets.generated.java.srcDirs
}

来自bignibou-server模块:

description = "Bignibou Server"

configurations {
    mapstruct
    integrationTest
    integrationTestCompile.extendsFrom testCompile
    integrationTestRuntime.extendsFrom testRuntime
}

apply plugin: 'io.franzbecker.gradle-lombok'
apply plugin: 'org.springframework.boot'
apply plugin: 'application'


dependencyManagement {
    dependencies {
        dependency "org.elasticsearch:elasticsearch:${elasticsearchVersion}"
    }
}

dependencies {
    compile project(":bignibou-common")
    compile project(":bignibou-client")

    //Spring boot
    compile("org.springframework.boot:spring-boot-starter-actuator")
    compile("org.springframework.boot:spring-boot-starter-data-jpa") {
        exclude group: 'org.apache.tomcat', module: 'tomcat-jdbc'
    }
    compile("org.springframework.boot:spring-boot-starter-data-redis") {
        exclude group: 'redis.clients', module: 'jedis'
    }
    compile("biz.paluch.redis:lettuce:${lettuceVersion}")
    compile("org.springframework.boot:spring-boot-starter-web")
    compile("org.springframework.boot:spring-boot-starter-security")
    compile("org.springframework.boot:spring-boot-starter-thymeleaf")
    compile("org.springframework.boot:spring-boot-starter-cache")

    // Miscellaneous
    compile("commons-collections:commons-collections")
    compile("org.apache.commons:commons-lang3:${commonLangVersion}")
    mapstruct("org.mapstruct:mapstruct-processor:${mapstructVersion}")

    //Caching
    compile("org.hibernate:hibernate-ehcache")
    compile("com.github.ben-manes.caffeine:caffeine:${caffeineVersion}")

    // Spring Session
    compile("org.springframework.session:spring-session")

    // Testing
    testCompile project(":bignibou-test")
}

run {
    systemProperty "spring.cloud.propertiesFile", System.getProperty("user.dir") + "/spring-cloud.properties"
}

springBoot {
    mainClassName = "com.bignibou.Application"
}

sourceSets {

    main {
        ext.originalJavaSrcDirs = java.srcDirs
        java.srcDir "build/generated-sources/mapstruct/main"
    }

    generated {
        java {
            srcDirs = ["build/src-delomboked/java", "build/generated-sources/mapstruct/main"]
        }
    }

    integrationTest {
        java.srcDirs = ['src/it/java']
        resources.srcDirs = ['src/it/resources', 'src/main/resources']
        compileClasspath = sourceSets.main.output + configurations.testRuntime
        runtimeClasspath = output + compileClasspath
    }
}

task integrationTest(type: Test) {
    description "Run the integration tests."
    testClassesDir = sourceSets.integrationTest.output.classesDir
    classpath = sourceSets.integrationTest.runtimeClasspath
    reports.html.destination = file("$reports.html.destination/integration")
    reports.junitXml.destination = file("$reports.junitXml.destination/integration")
}

compileJava.dependsOn generateMainMapperClasses
generateMainMapperClasses.dependsOn mapStructClean
generateMainMapperClasses.dependsOn delombok

check.dependsOn integrationTest
integrationTest.shouldRunAfter test

编辑3 :第一个命令而不是第二个命令被删除的文件夹是以下(来自bignibou-common模块):

build/generated-mail-templates

编辑4 :我注意到在注释掉output.dir(...电话后(见下文),build/generated-mail-templates未被删除,但遗憾的是模板未包含在类路径中...

   sourceSets {
      main {
        ext.originalJavaSrcDirs = java.srcDirs
        java.srcDir "build/generated-sources/mapstruct/main"
        //output.dir("build/generated-mail-templates")
      }

1 个答案:

答案 0 :(得分:2)

task processMailTemplates {
    description "Processes mail templates"
    dependsOn npmInstall

    def templateSrcDir = "src/main/templates/mail/"
    def templateDestDir = "build/generated-mail-templates/META-INF/templates/mail/"

    mkdir templateDestDir

    def templateNames = []

    fileTree(dir: templateSrcDir, include: '**/*.html').visit {
        FileVisitDetails details -> templateNames << details.file.name
    }

    templateNames.each { templateName -> inlineCss(templateSrcDir + templateName, templateDestDir + templateName) }

    outputs.upToDateWhen { false }
}

所有这些代码都在配置阶段执行,无论您要求gradle执行的任务如何。它应该在执行阶段执行:

task processMailTemplates {
    description "Processes mail templates"
    dependsOn npmInstall

    outputs.upToDateWhen { false }

    doLast {
        def templateSrcDir = "src/main/templates/mail/"
        def templateDestDir = "build/generated-mail-templates/META-INF/templates/mail/"

        mkdir templateDestDir

        def templateNames = []

        fileTree(dir: templateSrcDir, include: '**/*.html').visit {
            FileVisitDetails details -> templateNames << details.file.name
        }

        templateNames.each { templateName -> inlineCss(templateSrcDir + templateName, templateDestDir + templateName) }
    }
}