Hello,
I’m trying to execute commands on linux command line through gradle,
I simply want to add a password to a zip. I wrote this task:
task encodeZip(type: Exec) {
workingDir path_target //variable in my script
commandLine "zipcloak", "zip_name.zip"
commandLine "psw"
commandLine "psw"
}
The command zipcloak asks twice to enter the password, but the script returns this error:
A problem occurred starting process 'command 'psw' '
Any idea about how to do this? Thanks
The Exec
task will only execute a single command. You are setting commandLine
3 times which is having the effect of overriding the first two values and executing the third. I’m not familiar with zipCloak but perhaps there’s the option to pass the username/password as command line arguments?
If not, you still won’t execute 3 commands (you’ll execute one command followed by values on the inputStream). To simulate this you’ll likely need to set the inputSream
property
Could you provide an example? I’m trying to pass the password with
setStandardInput (“psw”)
but i receive an error.
zipcloak doen’t accept the psw as argument, the only way is really to write it when requested.
Thanks for you help
I’m not 100% sure how zipCloak reads from the InputStream but likely something like
ByteArrayOutputStream bytes = new ByteArrayOutputStream()
PrintWriter out = new PrintWriter(bytes)
out.println("psw")
out.println("psw")
out.flush()
standardInput = new ByteArrayInputStream(bytes.toByteArray())
Now the zipcloak command is executed but I’m not still able to send the password to the console
Can you paste the whole task definition?
This is the task
task encodeZip() {
workingDir path_target_workspace
commandLine "zipcloak", "file.zip"
ByteArrayOutputStream bytes = new ByteArrayOutputStream()
PrintWriter out = new PrintWriter(bytes)
out.println("psw")
out.flush()
ByteArrayInputStream input = new ByteArrayInputStream(bytes.toByteArray())
setStandardInput(input)
}
I think you’ll need to call out.println("psw")
twice
I tried but the input is not sent to the consolle
Lance_Java
(Lance Java)
April 28, 2016, 11:02am
10
It’s not going to go to the console… it’s going to the InputStream of the zipcloak process
@see ProcessBuilder.redirectInput(ProcessBuilder.Redirect)
But the consolle still waits for inputs, if I don’t put anything the script doesn’t go to the end
Hmm… this thread seems to suggest the input stream should be initialized in a doFirst. Eg:
task encodeZip(type: Exec) {
workingDir path_target_workspace
commandLine "zipcloak", "file.zip"
doFirst {
ByteArrayOutputStream bytes = new ByteArrayOutputStream()
PrintWriter out = new PrintWriter(bytes)
out.println("psw")
out.println("psw")
out.flush()
standardInput = new ByteArrayInputStream(bytes.toByteArray())
}
}
Still not working
I definitely create a zip with passowrd, instead of adding it to an existing one
task encodeZip(type: Exec) {
workingDir path_target_workspace
commandLine "zip", "-P", package_password, "-r", packageName + ".zip", "doc", "plugin", packageName + ".tar"
}
After the parameter packageName.zip
there are all the files I want to add to the zip, in this case the folders “doc”, “plugin” and a tar
This WORKS!
In any case, thank you very much for your support