Ken*_*Ken 23 windows macos gradle
如果命令采用不同的形式,是否有办法在Windows和Mac上执行任务?例如:
task stopTomcat(type:Exec) {
// use this command line if on Windows
commandLine 'cmd', '/c', 'stop.cmd'
// use the command line if on Mac
commandLine './stop.sh'
}
Run Code Online (Sandbox Code Playgroud)
你会怎么在Gradle做到这一点?
Mar*_*ira 39
您可以commandLine根据系统属性的值有条件地设置属性.
if (System.getProperty('os.name').toLowerCase(Locale.ROOT).contains('windows')) {
commandLine 'cmd', '/c', 'stop.cmd'
} else {
commandLine './stop.sh'
}
Run Code Online (Sandbox Code Playgroud)
如果脚本或可执行文件在 windows 和 linux 上相同,那么您将能够执行以下操作,以便您只需通过调用如下函数来定义参数一次:
import org.apache.tools.ant.taskdefs.condition.Os
task executeCommand(type: Exec) {
commandLine osAdaptiveCommand('aws', 'ecr', 'get-login', '--no-include-email')
}
private static Iterable<String> osAdaptiveCommand(String... commands) {
def newCommands = []
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
newCommands = ['cmd', '/c']
}
newCommands.addAll(commands)
return newCommands
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7789 次 |
| 最近记录: |