【问题标题】:How to pass argument from gradle exec task to commandline如何将参数从 gradle exec 任务传递到命令行
【发布时间】:2018-06-04 09:32:51
【问题描述】:

我正在尝试将 bat 和 sh 文件(取决于正在运行的操作系统)与 gradle exec 任务挂钩。但我无法弄清楚如何从 exec 任务向 bat/sh 发送参数。

task testing(type:Exec) {
    workingDir '.'

    if (System.properties['os.name'].toLowerCase().contains('windows')) {
        if ( project.hasProperty("arg1") ) {
            println arg1
            args arg1
            commandLine 'cmd', '/c', 'run.bat'
        }else{
            commandLine 'cmd', '/c', 'run.bat'
        }
   }else {
        if ( project.hasProperty("arg1") ) {
            args arg1
            commandLine './run.sh'
        }else{
            commandLine './run.sh'
        }
   }
}

如果我以gradle testing -Parg1=test 运行此任务,在println arg1 中,它会打印测试

但是我如何将此测试作为参数传递给 bat/sh 文件。

【问题讨论】:

  • 你想用 shell 文件实现什么?

标签: batch-file gradle sh build.gradle command-line-arguments


【解决方案1】:

通过commandLine传递参数

Windows

commandLine 'cmd', '/c', 'run.bat' ,arg1

Linux / mac

commandLine './run.sh' , arg1

【讨论】:

  • 这也适用于 gradle 3.5。您也可以在 groovy 文件中遵循此语法
猜你喜欢
  • 2021-03-31
  • 1970-01-01
  • 1970-01-01
  • 2012-07-26
  • 1970-01-01
  • 2021-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多