gradle: Execute task “type:Exec” with many arguments with spaces

无人久伴 提交于 2019-12-10 13:22:38

问题


I have the gradle task that should create Websphere profile on Windows OS

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()

}

And the problem appears if I uncomment commented lines, after it task fails and say me that: "C:/IBM" is not valid batch file. If I put profileTemplates not in the folder that contains spaces, everything works fine again. But templates should lies int wasHome( And sometimes wasHome has spaces(

I have, now ideas why adding templates key with value with spaces influence in such way that Gradle tries to start "C:/IBM" instead specified 'C:/IBM new/WebSphere/AppServer/bin/manageprofiles.bat'. It seems that, possibly, problem inside java.lang.ProcessBuilder.

I tries to quote paths, by adding "/"" but nothing works(((( what isn't surprise, because ProcessBuilder implies quoting by itself if it is needed.

So, I am asking if anybody had the similar problem and could recommend how to work around this issue? Thanks in advance.


回答1:


If somebody needed it, we found a workaround for this problem. The task finally looks like:

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
}

The basic idea is to pass the arguments to the Gradle not as long string, but as a list. So in this way there aren't any problems if an argument contains a space.




回答2:


Change following lines

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

That way, the full path to the batch file is quoted.

EDITED - As stated by dbenhan, a little obfuscated. This "should" be something like

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()

}

BUT, while gradle in particular and windows in general can handle paths with slash separators, i have no idea if manageprofiles.bat can, and you are passing a parameter with a path in it. Maybe, you will need to change your paths to 'c:\\IBM new\\....'




回答3:


Try This

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

        println result1.toString()
}


来源:https://stackoverflow.com/questions/20613244/gradle-execute-task-typeexec-with-many-arguments-with-spaces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!