7

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.

serg
  • 1,003
  • 3
  • 16
  • 26

3 Answers3

11

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.

Re Captcha
  • 3,125
  • 2
  • 22
  • 34
serg
  • 1,003
  • 3
  • 16
  • 26
  • This solution did not work for me. I had an argument such as `--autoprefix="last 2 versions"`, and the spaces inbetween the doublequotes were always throwing things off. – Michael R Feb 03 '16 at 21:07
3

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

MC ND
  • 69,615
  • 8
  • 84
  • 126
  • +1, or perhaps a bit less obfuscated: `str.add('"'+wasHome+'/bin/manageprofiles.bat"')` – dbenham Dec 16 '13 at 14:53
  • @dbenham: i know. But this allows to uncomment the following two lines and use `str.add(wasHome + '/profileTemplates/default"')` reusing the initial quote (just joking, you are right ;-) ) – MC ND Dec 16 '13 at 15:03
  • tried both, still the same result(((( still not working, somehow ant do it perfectly, with gradle I am not managed to succeed. – serg Dec 16 '13 at 15:11
  • unfortunately, no( I added quotes: no result, changed slashes: again nothing> Even println prints [cmd, /c, "C:\IBM new\WebSphere\AppServer\bin\manageprofiles.bat", -create, -profileName, AppSrv02, -templatePath, "C:\IBM new\WebSphere\AppServer\profileTemplates\default"], still command line says that "C:\IBM" is not valid command or something this – serg Dec 16 '13 at 15:48
1

Try This

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

        println result1.toString()
}
Singhak
  • 8,508
  • 2
  • 31
  • 34