Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gradle: Execute task "type:Exec" with many arguments with spaces

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.

like image 716
serg Avatar asked Dec 16 '13 14:12

serg


2 Answers

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.

like image 134
serg Avatar answered Sep 22 '22 23:09

serg


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\\....'

like image 35
MC ND Avatar answered Sep 21 '22 23:09

MC ND