Gradle神秘地运行构建任务?

Gradle神秘地运行构建任务?,gradle,Gradle,这是对我理智的挑战。我有以下build.gradle: import org.gradle.api.tasks.Exec import org.apache.tools.ant.taskdefs.condition.Os defaultTasks 'build' // not specifying .cmd on windows will attempt to // run the extensionless executable which will fail ext { npmCom

这是对我理智的挑战。我有以下
build.gradle

import org.gradle.api.tasks.Exec
import org.apache.tools.ant.taskdefs.condition.Os

defaultTasks 'build'

// not specifying .cmd on windows will attempt to
// run the extensionless executable which will fail
ext {
  npmCommand = Os.isFamily(Os.FAMILY_WINDOWS) ? 'npm.cmd' : 'npm'
  tscCommand = Os.isFamily(Os.FAMILY_WINDOWS) ? 'tsc.cmd' : 'tsc'
}

// Get the path for the locally installed binaries
task npmBin << {
    new ByteArrayOutputStream().withStream { os ->
        exec {
            executable = npmCommand
            args = ['bin']
            standardOutput = os
        }
        ext.binPath = os.toString().trim() + File.separator
    }
}

task copyVendor(type: Copy) {
  from 'node_modules/systemjs/dist/system.src.js',
       'node_modules/angular2/bundles/angular2.dev.js',
       'node_modules/angular2/bundles/http.dev.js'
  into 'build/app/scripts/vendor'
}

task copyNonTS(type: Copy) {
  from('app') {
    exclude '**/*.ts', '**/*.js.map'
  }
    filter { line -> line.replaceAll('(node_modules\\/systemjs\\/dist)|(node_modules\\/angular2\\/bundles)', 'app/scripts/vendor') }
  into 'build/app'
}

// Install packages from package.json
task npm(type: Exec) {
    description = "Grab NodeJS dependencies (from package.json)"
    commandLine = [npmCommand, "install"]
    inputs.file "package.json"
    outputs.dir "node_modules"

    tasks.npmBin.execute()
}

task cleanDev(type: Delete) {
    outputs.upToDateWhen { false }
  delete fileTree(dir: 'app', include: ['**/*.js', '**/*.js.map'])
}

task delOutput(type: Delete) {
    outputs.upToDateWhen { false }
    println "DELETING"
    delete 'build/'
}

task clean(type: Delete) {
    outputs.upToDateWhen { false }
    cleanDev.execute()
    delOutput.execute()
}

task build(dependsOn: 'npm', type: Exec) {
    println "BUILDING"
    if (file(new File("${npmBin.binPath}${tscCommand}")).isFile()) {
        // runs non-globally installed tsc from node_modules
        commandLine "${npmBin.binPath}${tscCommand}"
    } else {
        // runs globally installed tsc
        commandLine = [tscCommand]
    }
  copyVendor.execute()
  copyNonTS.execute()
}
为什么当我运行这个看起来很小的、原子的、没有依赖关系的任务时,我的构建任务会运行?它删除我的build文件夹,然后立即调用build任务再次运行(正如输出“BUILDING”所示)

这里发生了什么事

编辑:

这与这里所说的是一致的。然而,如果我有一个
类型的任务:Exec
,那么我必须在配置阶段指定
命令行
,这样做似乎总是执行命令,而不管我是否真的想运行该任务。如何仅在运行任务时运行
命令行

task hello << {
    println 'Hello world!'
}
如果没有
doLast
,任务将在配置阶段运行,而不是显式调用它


将任务定义从
task build(){
更改为
task build()
defaultTasks'build'
可能与此有关?尝试注释这一行。我尝试注释它,但没有区别。我认为只有在您只运行
gradle
而不指定任务的情况下,这一点才会起作用。
task build(){
应该是
任务构建()与您的其他任务定义相同。这似乎起到了作用。
这样做会导致
gradle build
失败。我被告知
execCommand==null!
很抱歉让您挂断。我以为我理解了gradle,昨天指出的一切都与我的理解背道而驰。谁知道呢这是一个配置阶段?!嗯,显然不是我。我道歉,你帮我纠正了记录。NP,如果我说gradle没有时不时地挑战我对幕后工作的理解,那我就是在撒谎。事情仍然在发生变化,这无助于我的理解!
task hello << {
    println 'Hello world!'
}
task hello {
    doLast {
        println 'Hello world!'
    }
}
task hello {

    def commandline = 'something' //in the config phase

    doLast {
        println 'Executing!' //in the exec phase
    }
}