将内置 gradle 任务移至 doLast/内置任务快捷方式
我想创建一个简单的同步任务,根据构建类型(例如调试/发布)稍微改变它的行为,并且我使用在 gradle.taskGraph.whenReady 中声明的布尔变量“虚拟”:
gradle.taskGraph.whenReady {taskGraph ->
dummy = false
if (taskGraph.hasTask(':dummybuild')) {
dummy = true
}
}
问题是通过以下方式配置的任务具有配置范围,即在whenReady之前,因此它无法访问“虚拟”变量:
task copySkins(type: Sync) {
from skinsFrom
into skinsInto
rename skinsRename
exclude symbianExclude
if (!dummy) exclude dummyExclude
}
现在我正在使用此解决方法
task copySkins {
inputs.dir skinsFrom
outputs.dir skinsInto
doLast {
task skins(type: Sync) {
from skinsFrom
into skinsInto
rename skinsRename
exclude symbianExclude
if (!dummy) exclude dummyExclude
}
skins.execute()
}
}
除了whenReady之外,是否可以
- 在其他地方检测/设置一些构建属性
- 将同步任务工作移至doLast
- 或者至少有一些同步任务的快捷方式(.execute() 看起来很丑陋)
I want to create a simple sync task that slightly change it behaviour depending on build type (e.g. debug/release) and I use boolean variable 'dummy' decrared in gradle.taskGraph.whenReady:
gradle.taskGraph.whenReady {taskGraph ->
dummy = false
if (taskGraph.hasTask(':dummybuild')) {
dummy = true
}
}
The problem is that task configured by the following way has configuration scope, i.e. before whenReady so it doesn't have access to the 'dummy' variable:
task copySkins(type: Sync) {
from skinsFrom
into skinsInto
rename skinsRename
exclude symbianExclude
if (!dummy) exclude dummyExclude
}
Right now I'm using this workaround
task copySkins {
inputs.dir skinsFrom
outputs.dir skinsInto
doLast {
task skins(type: Sync) {
from skinsFrom
into skinsInto
rename skinsRename
exclude symbianExclude
if (!dummy) exclude dummyExclude
}
skins.execute()
}
}
Is it possible to
- detect/setup some build properties in some other place except whenReady
- move sync task work to doLast
- or at least have some shortcut for sync task (.execute() looks quite ugly)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
1) whenReady 事件允许用户访问完全初始化的任务图:所有初始化已完成并且任务已准备好运行。当您需要在此处检测/设置构建属性时,唯一的情况是您需要内省当前构建设置。
如果您不需要此信息,您可以将初始化放在构建脚本中的任何位置。最后,这只不过是绝妙的脚本。
2) 您不能将同步任务工作移至 doLast。但你总是可以将你的操作添加到 doFirst ;)我认为,这应该有效:
3)正如之前所说,缺少同步任务快捷方式不应该那么痛苦
1)
whenReady
event allows user to access fully-initialized task graph: all initialization is finished and tasks are ready to run. The only situation, when you need to detect/setup build properties here, is when you need to introspect current build setup.If you do not need this information, you can place your initialization anywhere in your build script. At the very end, it is nothing but groovy script.
2) You can not move sync task work to doLast. But you can always add your actions to doFirst ;) I think, this should work:
3) With all said before, missing sync task shortcut should not be that painfull