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

serg picture serg · Dec 16, 2013 · Viewed 13k times · Source

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.

Answer

serg picture serg · Apr 23, 2014

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.