【问题标题】:gradle: Execute task "type:Exec" with many arguments with spacesgradle:使用带有空格的许多参数执行任务“type:Exec”
【发布时间】:2014-01-03 23:32:32
【问题描述】:

我的 gradle 任务应该在 Windows 操作系统上创建 Websphere 配置文件

task createProfile(type:Exec) {

    def commandToExecute = new StringBuffer()
    def profile = 'AppSrv02'
    def wasHome = 'C:/IBM new/WebSphere/AppServer'

    def str = new LinkedList <String> ();
    str.add('cmd')
    str.add('/c')
    str.add(wasHome + '/bin/manageprofiles.bat')
    str.add('-create')
    str.add('-profileName')
    str.add(profile)
    //str.add('-templatePath')
    //str.add(wasHome + '/profileTemplates/default')

    println (str)
    commandLine str.toArray()

}

如果我在任务失败后取消注释注释行并告诉我:“C:/IBM”不是有效的批处理文件,则会出现问题。如果我将 profileTemplates 不在包含空格的文件夹中,则一切正常。但是模板应该位于 int wasHome( 有时 wasHome 有空格(

我现在知道为什么添加带有空格的值的模板键会影响 Gradle 尝试启动“C:/IBM”而不是指定“C:/IBM new/WebSphere/AppServer/bin/manageprofiles.bat” .似乎 java.lang.ProcessBuilder 内部可能存在问题。

我尝试通过添加 "/"" 来引用路径,但没有任何效果((((这并不奇怪,因为 ProcessBuilder 暗示在需要时自行引用。

所以,我想问是否有人有类似的问题,可以推荐如何解决这个问题?提前致谢。

【问题讨论】:

    标签: batch-file gradle exec spaces processbuilder


    【解决方案1】:

    如果有人需要它,我们找到了解决此问题的方法。任务最终看起来像:

    task createProfile(type: Exec) {
        executable = new File(wsadminLocation, manageProfilesFileName)
        def templatePath = wasHome + File.separator + "profileTemplates" + File.separator + "default"
        def argsList = ["-create", "-profileName", profile, "-templatePath", templatePath, "-nodeName", nodeName, "-cellName", wasCellName, "-enableAdminSecurity", isProfileSecured, "-adminUserName", rootProject.wasLogin, "-adminPassword", rootProject.wasPassword]
        args = argsList
    }
    

    基本思想是将参数传递给 Gradle,而不是作为长字符串,而是作为列表。因此,如果参数包含空格,这样就没有任何问题。

    【讨论】:

    • 这个解决方案对我不起作用。我有一个论点,例如--autoprefix="last 2 versions",而双引号之间的空格总是把事情扔掉。
    【解决方案2】:

    更改以下行

    def wasHome = '"C:/IBM new/WebSphere/AppServer'
    ...
    str.add(wasHome + '/bin/manageprofiles.bat"')
    

    这样,批处理文件的完整路径被引用。

    已编辑 - 正如 dbenhan 所说,有点模糊。这个“应该”类似于

    task createProfile(type:Exec) {
    
        def commandToExecute = new StringBuffer()
        def profile = 'AppSrv02'
        def wasHome = 'C:/IBM new/WebSphere/AppServer'
    
        def str = new LinkedList <String> ();
        str.add('cmd')
        str.add('/c')
        str.add('"' + wasHome + '/bin/manageprofiles.bat"')
        str.add('-create')
        str.add('-profileName')
        str.add(profile)
        str.add('-templatePath')
        str.add('"' + wasHome + '/profileTemplates/default"')
    
        println (str)
        commandLine str.toArray()
    
    }
    

    但是,虽然 gradle 和 windows 通常可以处理带有斜杠分隔符的路径,但我不知道 manageprofiles.bat 是否可以,并且您正在传递一个带有路径的参数。也许,您需要将路径更改为'c:\\IBM new\\....'

    【讨论】:

    • +1,或者可能不那么混淆:str.add('"'+wasHome+'/bin/manageprofiles.bat"')
    • @dbenham:我知道。但这允许取消注释以下两行并使用 str.add(wasHome + '/profileTemplates/default"') 重用初始报价(开玩笑,你是对的 ;-))
    • 两个都试过了,结果还是一样((((还是不行,不知怎么的蚂蚁做得很完美,用gradle我没能成功。
    • 不幸的是,没有(我添加了引号:没有结果,更改了斜杠:再次没有>甚至 println 打印 [cmd, /c, "C:\IBM new\WebSphere\AppServer\bin\manageprofiles.bat ", -create, -profileName, AppSrv02, -templatePath, "C:\IBM new\WebSphere\AppServer\profileTemplates\default"],仍然命令行说 "C:\IBM" 不是有效的命令或类似的东西跨度>
    【解决方案3】:

    试试这个

    task xyz {
    def result1 = exec {
                workingDir "D:/abc/efg"
                commandLine 'cmd', '/c', 'CDUTIL.bat', "qwe", "rty"
            }
    
            println result1.toString()
    }
    

    【讨论】:

    • 这绝对适用于 gradle 3.5 和 groovy
    猜你喜欢
    • 1970-01-01
    • 2018-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多