Batch file gradle:执行任务“;类型:Exec";有许多带空格的参数
我的gradle任务应该是在Windows操作系统上创建Websphere概要文件Batch file gradle:执行任务“;类型:Exec";有许多带空格的参数,batch-file,gradle,exec,spaces,processbuilder,Batch File,Gradle,Exec,Spaces,Processbuilder,我的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> ();
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()
}
task createProfile(类型:Exec){
def commandToExecute=新的StringBuffer()
def配置文件='AppSrv02'
def wasHome='C:/IBM new/WebSphere/AppServer'
def str=newlinkedlist();
str.add('cmd')
str.add('/c')
str.add(wasHome+'/bin/manageprofiles.bat')
str.add('-create')
str.add('-profileName')
str.add(配置文件)
//str.add('-templatePath')
//str.add(wasHome+'/profileTemplates/default')
println(str)
命令行str.toArray()
}
如果在it任务失败后,我取消注释注释注释行,并告诉我:“C:/IBM”不是有效的批处理文件,则会出现问题。如果我不将profileTemplates放在包含空格的文件夹中,那么一切都会恢复正常。但模板应该位于wasHome(有时wasHome有空格(
我现在有了一些想法,为什么添加带有空格影响的值的模板会导致Gradle试图启动“C:/IBM”,而不是指定“C:/IBM new/WebSphere/AppServer/bin/manageprofiles.bat”。这似乎是java.lang.ProcessBuilder内部的问题
我通过添加“/”
尝试引用路径,但没有任何效果(((这并不奇怪,因为ProcessBuilder意味着在需要时自行引用)
因此,我想问是否有人有类似的问题,可以建议如何解决这个问题?提前谢谢。更改以下行
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()
}
task createProfile(类型:Exec){
def commandToExecute=新的StringBuffer()
def配置文件='AppSrv02'
def wasHome='C:/IBM new/WebSphere/AppServer'
def str=newlinkedlist();
str.add('cmd')
str.add('/c')
str.add(“”+wasHome+/bin/manageprofiles.bat“)
str.add('-create')
str.add('-profileName')
str.add(配置文件)
str.add('-templatePath')
str.add(“”+wasHome+“/profileTemplates/default“)
println(str)
命令行str.toArray()
}
但是,尽管gradle和windows一般都可以使用斜杠分隔符处理路径,但我不知道
manageprofiles.bat是否可以,并且您正在传递一个包含路径的参数。也许,您需要将路径更改为'c:\\IBM new\\\..'
,如果有人需要,我们找到了解决此问题的方法m、 任务最终看起来像:
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,而不是作为长字符串,而是作为列表。这样,如果参数包含空格,就不会有任何问题。试试这个
task xyz {
def result1 = exec {
workingDir "D:/abc/efg"
commandLine 'cmd', '/c', 'CDUTIL.bat', "qwe", "rty"
}
println result1.toString()
}
+1,或者可能有点模糊:str.add(““+wasHome+”/bin/manageprofiles.bat“)
@dbenham:我知道。但这允许取消对以下两行的注释,并使用str.add(wasHome+“/profileTemplates/default”)
重用初始引用(只是开个玩笑,你是对的;-)尝试了这两种方法,结果仍然相同((((仍然不起作用,不知何故,ant做得很完美,但gradle没有成功。不幸的是,没有(我添加了引号:无结果,更改了斜杠:再次无任何内容>甚至println prints[cmd,/c,“c:\IBM new\WebSphere\AppServer\bin\manageprofiles.bat”,-create,-profileName,AppSrv02,-templatePath,“C:\IBM new\WebSphere\AppServer\profileTemplates\default”],但命令行仍然指出“C:\IBM”不是有效的命令,或者此解决方案不适用于我。我有一个参数,例如--autoprefix=“last 2 versions“
,而双引号之间的空格总是有问题。这在gradle 3.5和groovy中绝对有效