我们的构建有一些通用逻辑来从配置数据创建任务。还有几个Gradle文件,其执行顺序我不想依赖。
现在我需要向任务添加一个任务依赖项,而不知道在我的部分构建脚本运行之前(在配置时)或在我之后是否定义了该任务。
[编辑:正如@canillas写道,我可以说myTask.dependsOn "otherTask"
和“懒惰”取决于尚未定义的“otherTask”,但我需要反过来。]
由于我在定义“otherTask”之前无法编写tasks["otherTask"].dependsOn myNewTask
,因此我创建了以下辅助函数:
void safelyDoWithTask(String taskName, Closure func) {
def task = tasks.findByName(taskName)
if (task != null) {
func(task)
} else {
project.tasks.whenTaskAdded { task_ ->
if (taskName.equals(task_.getName())) {
func(task_)
}
}
}
}
safelyDoWithTask('otherTask', { it.dependsOn myNewTask })
现在我想知道是否有更多的规范方法来实现这一目标?
答案 0 :(得分:1)
请考虑以下事项:
Sub Categories()
Dim Locale As String, State(1 To 50) As Variant
Dim Serial(1 To 50) As Single, i As Single
Dim path As String, j As Single
Dim Score(1 To 50, 1 To 7) As Single
Dim IndexGrade(1 To 50) As Single
Dim Total(1 To 50) As Single
Locale = ActiveWorkbook.path
path = Locale & "US_States.txt"
Open path For Input As #1
For i = 1 To 50 Step 1
Input #1, Serial(i), State(i)
Sheet1.Cells(1 + i, 1).Value = Serial(i)
Sheet1.Cells(1 + i, 2).Value = State(i)
For j = 1 To 7 Step 1
Input #1, Score(i, j)
Total(i) = Total(i) + Score(i, j)
Sheet1.Cells(1 + i, 3).Value = Total(i)
Next j
Total(i) = Sheet1.Cells(1 + i, 3).Value
If 0 <= Total(i) < 100 Then
Sheet1.Cells(1 + i, 4).Value = "A"
ElseIf 100 <= Total(i) < 200 Then
Sheet1.Cells(1 + i, 4).Value = "B"
ElseIf 200 <= Total(i) < 300 Then
Sheet1.Cells(1 + i, 4).Value = "C"
ElseIf 300 <= Total(i) Then
Sheet1.Cells(1 + i, 4).Value = "D"
End If
Next i
Close #1
End Sub
要点是:
// section 1
def dynamicDependencies = [:]
dynamicDependencies['otherTask'] = 'myNewTask'
// section 2
afterEvaluate { project ->
// taskA dependsOn taskB
dynamicDependencies.each { taskA, taskB ->
def task = project.tasks.findByName(taskA)
if (task) { task.dependsOn "${taskB}" }
}
}
// section 3
task myNewTask() {
doLast {
println 'myNewTask !'
}
}
task otherTask() {
doLast {
println 'otherTask !'
}
}
挂钩来处理地图