gpt4 book ai didi

gradle - 如何重构类似任务中的重复代码?

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

我有一组看起来像这样的任务:

// task to run the groovy script
task('Ls', type: JavaExec) {

environment 'gateway', props.gateway
environment 'username', props.username
environment 'password', props.password

main = 'Ls'
classpath = sourceSets.main.runtimeClasspath
}
task('Mkdir', type: JavaExec) {

environment 'gateway', props.gateway
environment 'username', props.username
environment 'password', props.password

main = 'Mkdir'
classpath = sourceSets.main.runtimeClasspath
}

task('Put', type: JavaExec) {

environment 'gateway', props.gateway
environment 'username', props.username
environment 'password', props.password

main = 'Put'
classpath = sourceSets.main.runtimeClasspath
}

task('Example') {
dependsOn Ls, Mkdir, Put
}

有没有一种方法可以重构环境设置块,以使我没有太多重复的代码?

最佳答案

当然:

['Ls', 'Mkdir', 'Put'].each { taskName ->
task "$taskName"(type: JavaExec) {

environment 'gateway', props.gateway
environment 'username', props.username
environment 'password', props.password

main = taskName
classpath = sourceSets.main.runtimeClasspath
}
}

关于gradle - 如何重构类似任务中的重复代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36934990/

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