仅在运行特定任务时执行任务

时间:2019-06-25 11:05:48

标签: gradle gradle-task

我希望任务setupDB仅在运行任务fatJarslimJar时执行。

但是在添加gradle.taskGraph.whenReady之后,commandLine './scripts/configureSQLiteDB.sh'从未针对任何任务运行。

这是我的setupDB任务代码:

//download bigg.sqlite if not present
task setupDB {
    gradle.taskGraph.whenReady { graph ->
        if (!project.file("resources/edu/ucsd/sbrg/bigg/bigg.sqlite").exists() 
                && (graph.hasTask(slimJar)|| graph.hasTask(fatJar))) {
            doFirst { 
                exec {
                    println "Setup DB"
                    commandLine './scripts/configureSQLiteDB.sh'
                } 
            }
        } 
    }
}

您还可以查看build.gradle文件:

apply plugin: "java"

defaultTasks "clean", "fatJar"
// Java versions for compilation and output
sourceCompatibility = "1.8"
targetCompatibility = "1.8"
archivesBaseName = "ModelPolisher"

version = "1.7"

sourceSets {
    main.java.srcDirs = ["src"]
    main.resources.srcDirs = ["resources"]
    main.resources.excludes = ["**/bigg.zip"]
    test.java.srcDirs = ["test"]
}

repositories {
    mavenCentral()
    maven { url "http://www.ebi.ac.uk/~maven/m2repo" }
    maven { url "http://jsbml.sourceforge.net/m2repo/" }
    // local dependencies
    flatDir {
        dirs "lib/de/zbit/SysBio/1390"
    }
}

dependencies {
    compile "org.sbml.jsbml:jsbml:1.4"
    compile "de.zbit:SysBio:1390"
    compile "org.xerial:sqlite-jdbc:3.21.0"
    compile "org.postgresql:postgresql:42.2.2"
    compile "org.biojava:biojava-ontology:5.0.0"
    compile "com.diffplug.matsim:matfilerw:3.0.1"
    compile "com.fasterxml.jackson.core:jackson-core:2.9.9"
    compile "com.fasterxml.jackson.core:jackson-databind:2.9.9"
    testCompile "org.junit.jupiter:junit-jupiter-engine:5.1.0"
}

// config for all jar tasks
tasks.withType(Jar) {
    dependsOn test
    destinationDir = file("$rootDir/target")
    manifest {
        attributes(
                "Version": version,
                "Implementation-Title": "ModelPolisher",
                "Implementation-Version": version,
                "Specification-Vendor": "University of California, San Diego",
                "Specification-Title": "ModelPolisher",
                "Implementation-Vendor-Id": "edu.ucsd.sbrg",
                "Implementation-Vendor": "University of California, San Diego",
                "Main-Class": "edu.ucsd.sbrg.bigg.ModelPolisher"
        )
    }
}

// with dependencies
task fatJar(type: Jar) {
    baseName = project.name + "-fat"
    from {
        configurations.compile.collect {
            it.isDirectory() ? it : zipTree(it)
        }
    }
    with jar
}

//with dependencies, without bigg.sqlite
task lightJar(type: Jar) {
    exclude("**/bigg.sqlite")
    baseName = project.name + "-noDB"
    from {
        configurations.compile.collect {
            it.isDirectory() ? it : zipTree(it)
        }
    }
    with jar
}

// without dependencies and bigg.sqlite
task bareJar(type: Jar) {
    exclude("**/bigg.sqlite")
    baseName = project.name + "-slim-noDB"
    with jar
}

// without dependencies, bigg.sqlite included
// not included in release
task slimJar(type: Jar) {
    baseName = project.name + "-slim"
    with jar
}


// zip lib folder for release
task zipLibs(type: Zip) {
    from "lib"
    into "lib"
    include "**/**"
    archiveName = "lib.zip"
    destinationDir = file("target/")
}

// zip script files for release
task zipScripts(type: Zip) {
    from "scripts"
    into "scripts"
    include "**/**"
    archiveName = "scripts.zip"
    destinationDir = file("target/")
}

// create all three jars for release
task release() {
    dependsOn fatJar
    dependsOn bareJar
    dependsOn lightJar
    dependsOn tasks["zipLibs"]
    dependsOn tasks["zipScripts"]
    // necessary, as order is not defined by dependsOn
    bareJar.mustRunAfter classes
    // slimJar.mustRunAfter bareJar
    lightJar.mustRunAfter slimJar
    fatJar.mustRunAfter lightJar
}

// clean up target directory
clean.doFirst {
    file(".gradle").deleteDir()
    file("target").deleteDir()
}

//download bigg.sqlite if not present
task setupDB {
    gradle.taskGraph.whenReady { graph ->
        if (!project.file("resources/edu/ucsd/sbrg/bigg/bigg.sqlite").exists() 
                && (graph.hasTask(slimJar)|| graph.hasTask(fatJar))) {
            doFirst { 
                exec {
                    println "Setup DB"
                    commandLine './scripts/configureSQLiteDB.sh'
                } 
            }
        } 
    }
}

// bump jar version in travis.yml
if (project.file(".travis.yml").exists()) {
    task bumpVersionTravis() {
        replaceVersion(".travis.yml")
    }
    processResources.dependsOn bumpVersionTravis
}

// bump jar version in ModelPolisher.sh
if (project.file("./scripts/ModelPolisher.sh").exists()) {
    task bumpVersionMP() {
        replaceVersion("./scripts/ModelPolisher.sh")
    }
    processResources.dependsOn bumpVersionMP
}

def replaceVersion(path) {
    ArrayList<String> content = new ArrayList<>()
    File travisFile = new File(path)
    String MPVersion = /ModelPolisher.*\d{1,2}(.\d{1,2}){1,2}.jar/

    travisFile.eachLine {
        line ->
            content.add(line.replaceAll(MPVersion, "ModelPolisher-fat-" +
                    "${version}.jar"))
    }
    BufferedWriter writer = new BufferedWriter(new FileWriter(travisFile))
    content.each {
        line -> writer.writeLine(line)
    }
    writer.close()
}

1 个答案:

答案 0 :(得分:1)

您似乎对gradle配置有错误的认识。您不应该基于输入来更改任务行为。您应该配置任务之间的依赖关系,还应该配置任务输入/输出以控制“最新”行为。

例如:

task slimJar(type:Jar) {
   dependsOn 'setupDB' 
   ... 
} 
task fatJar(type:Jar) {
   dependsOn 'setupDB' 
   ... 
} 
task setupDB {
    outputs.upToDateWhen { file("resources/edu/ucsd/sbrg/bigg/bigg.sqlite").exists() } 
    doFirst { 
        println "Setup DB"
        exec {           
            commandLine './scripts/configureSQLiteDB.sh' 
        }
    }
}

以下也是不好的做法

if (project.file(".travis.yml").exists()) {
    task bumpVersionTravis() {
        replaceVersion(".travis.yml")
    }
    processResources.dependsOn bumpVersionTravis
}

可用任务不应根据文件系统中的文件而改变。无论您的文件系统如何,它们都应始终存在。相反,您应该设置“已启用”标志(或使用任务输出)来控制任务是执行还是被跳过

例如:

task bumpVersionTravis {
    enabled = project.file(".travis.yml").exists()
    doLast {
       replaceVersion(".travis.yml")
    } 
}
processResources.dependsOn bumpVersionTravis