如何将参数从 gradle exec 任务传递到命令行

Posted

技术标签:

【中文标题】如何将参数从 gradle exec 任务传递到命令行【英文标题】:How to pass argument from gradle exec task to commandline 【发布时间】: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 文件实现什么? 【参考方案1】:

通过commandLine传递参数

Windows

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

Linux / mac

commandLine './run.sh' , arg1

【讨论】:

这也适用于 gradle 3.5。您也可以在 groovy 文件中遵循此语法

以上是关于如何将参数从 gradle exec 任务传递到命令行的主要内容,如果未能解决你的问题,请参考以下文章