gpt4 book ai didi

gradle - Gradle神秘地运行Build任务?

转载 作者:行者123 更新时间:2023-12-03 04:02:51 25 4
gpt4 key购买 nike

这正在挑战我的理智。我有以下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()
}

以某种方式,当我运行 gradle delOutput时,我得到以下输出:
DELETING
BUILDING
:delOutput

BUILD SUCCESSFUL

Total time: 2.012 secs

为什么当我运行这个看似很小的,原子性的,无依赖性的任务时,我的构建任务仍在运行?它删除了我的构建文件夹,然后立即要求再次执行构建任务(如输出“BUILDING”所示)。

这里发生了什么事?

编辑:

这个 forum post同意这里所说的。但是,似乎如果我有一个 type: Exec任务,那么我必须在配置阶段指定 commandLine,这样做似乎总是执行命令,而不管我是否实际要运行该任务。如何仅在运行任务时运行 commandLine

最佳答案

task hello << {
println 'Hello world!'
}

是短手
task hello {
doLast {
println 'Hello world!'
}
}

如果您没有 doLast,则该任务将在配置阶段运行,而不是在您显式调用它时运行。

将您的任务定义从 task build(){更改为 task build()<<{
编辑以进行编辑:

这样的事情对您有用吗?
task hello {

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

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

关于gradle - Gradle神秘地运行Build任务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33788399/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com