how to use execute() in groovy to run any command - ant

I usually build my project using these two commands from command line (dos)
G:\> cd c:
C:\> cd c:\my\directory\where\ant\exists
C:\my\directory\where\ant\exists> ant -Mysystem
...
.....
build successful
What If I want to do the above from groovy instead? groovy has execute() method but following does not work for me:
def cd_command = "cd c:"
def proc = cd_command.execute()
proc.waitFor()
it gives error:
Caught: java.io.IOException: Cannot run program "cd": CreateProcess error=2, The
system cannot find the file specified
at ant_groovy.run(ant_groovy.groovy:2)

Or more explicitly, I think binil's solution should read
"your command".execute(null, new File("/the/dir/which/you/want/to/run/it/from"))

According to this thread (the 2nd part), "cd c:".execute() tries to run a program called cd which is not a program but a built-in shell command.
The workaround would be to change directory as below (not tested):
System.setProperty("user.dir", "c:")

"your command".execute(null, /the/dir/which/you/want/to/run/it/from)
should do what you wanted.

Thanks Noel and Binil, I had a similar problem with a build Maven.
projects = ["alpha", "beta", "gamma"]
projects.each{ project ->
println "*********************************************"
println "now compiling project " + project
println "cmd /c mvn compile".execute(null, new File(project)).text
}

I got to fix the issue by running a command as below:
i wanted to run git commands from git folder, so below is the code which worked for me.
println(["git","add","."].execute(null, new File("/Users/xyz/test-org/groovy-scripts/$GIT_REPOS/")).text)
println(["git","commit","-m","updated values.yaml"].execute(null, new File("/Users/xyz/test-org/groovy-scripts/$GIT_REPOS/")).text)
println(["git","push","--set-upstream","origin","master"].execute(null, new File("/Users/xyz/test-org/groovy-scripts/$GIT_REPOS/")).text)

Related

Working bin/bas returns error when executed from run script Xcode

I have a working shell script that works with no problems but when I executed it with a run script i get the following error:
The script will scan all the proto files in a directory and convert it to Swift using ProtoBuf. After that I will move the swift files into an App folder. The script code is the following
#!/bin/bash
for protoFile in ./*.proto;
do
protoc --swift_out=. $protoFile
done
for file in ./*.swift;
do
mv $file ../Convert\ AV/Model/USBDongle/Proto/
done
Any ideas?
Thank you
I was calling the script from the directory it was located. When Xcode executes the the script that assumption is false. So I am getting the directory where the script is located and I do the logic with that path
#!/bin/bash
#Get Directory where the script is located
baseDirectory=$(dirname "$0")
echo "$baseDirectory"
for protoFile in "$baseDirectory"/*.proto
do
echo $protoFile
protoc --swift_out="$baseDirectory" -I "$baseDirectory" "$protoFile"
done
for protoFileSwift in "$baseDirectory"/*.swift;
do
echo $protoFile
mv "$protoFileSwift" "$baseDirectory"/../Convert\ AV/Model/USBDongle/Proto
done
* /* Makes like is a comment... *

nix-build: bash permission denied

I'm trying to learn to write Nix expressions, and I thought about doing my own very simple "Hello World!" (as is tradition).
So I have my dir with only this default.nix file :
{pkgs ? import <nixpkgs> {}}:
derivation {
system = "x86_64-linux";
name = "simple-bash-derivation-helloworld";
builder = pkgs.bash;
args = [ "-c" "echo 'Hello World' > $out" ];
}
Here is what I get when I try to build it:
nix-build
these derivations will be built:
/nix/store/3grmahx3ih4c50asj84p7xnpqpj32n5s-simple-bash-derivation-helloworld.drv
building path(s) ‘/nix/store/6psl3rc92311w37c1n6nj0a6jac16hv1-simple-bash-derivation-helloworld’
while setting up the build environment: executing ‘/nix/store/wb34dgkpmnssjkq7yj4qbjqxpnapq0lw-bash-4.4-p12’: Permission denied
builder for ‘/nix/store/3grmahx3ih4c50asj84p7xnpqpj32n5s-simple-bash-derivation-helloworld.drv’ failed with exit code 1
error: build of ‘/nix/store/3grmahx3ih4c50asj84p7xnpqpj32n5s-simple-bash-derivation-helloworld.drv’ failed
Removing the args line yields the same issue.
Why do I get a permission issue?
What would be the correct way to make a simple derivation just doing a bash echo?
Please note that this is a learning exercise: I do not want to use stdenv.mkDerivation here for example.
I am running nix-env (Nix) 1.11.9 on an Ubuntu 16.04 system.
Thanks in advance.
Try running ls command on /nix/store/wb34dgkpmnssjkq7yj4qbjqxpnapq0lw-bash-4.4-p12 and you will see it's a directory rather than an executable file (pointing to $out of the pkgs.bash derivation). If you wanted to refer to bash binary you would use:
builder = "${pkgs.bash}/bin/bash";

How to use file parameter in jenkins

I am executing parameterised build in jenkins to count no. of lines in file which has 1 file parameter. Its file location is pqr. The name of the script file is linecount.sh which is saved at remote server. When i tried to execute it using command sh linecount.sh filename, it works perfectly from jenkins. But as i remove filename from the argument and execute same script as parameterised build it is showing below error on console :
Started by user Prasoon Gupta
[EnvInject] - Loading node environment variables.
Building in workspace users/Prasoon/sample_programs
Copying file to pqr
[sample_programs] $ /bin/sh -xe /tmp/hudson3529902665956638862.sh
+ sh linecount.sh
PRASOON4
linecount.sh: line 15: parameterBuild.txt: No such file or directory
Build step 'Execute shell' marked build as failure
Finished: FAILURE
I am uploading file (parameterBuild.txt) from my local machine. Why is it giving this error?
My doubt is in shell script I used argument as $1. How can I refer this when I am taking file as parameter.
The uploaded file will not retain the same name as it has on your local computer. It will be named after the File location argument specified in the file parameter settings:
In this example I will get a file called file.txt in my workspace root, regardless of what I call it on my computer.
So if I now build my job and enter the following in the parameter dialog (note that my local filename is table.html):
Then I get the following in the log (I have a build step which does ls -l):
Building on master in workspace /var/lib/jenkins/workspace/fs
Copying file to file.txt
[fs] $ /bin/sh -xe /tmp/hudson845437350739055843.sh
+ ls -l
total 4
-rw-r--r-- 1 jenkins jenkins 292 Feb 15 07:23 file.txt
Finished: SUCCESS
Note that table.html now is called file.txt, e.g. what I entered as File location.
So in you're case the command should be:
sh linecount.sh pqr
There is a a bug since ages that makes impossible to use fileParameter:
Handle file parameters
file parameter not working in pipeline job
There is a workaround for this issue https://github.com/janvrany/jenkinsci-unstashParam-library
and in a pipeline script you do:
library "jenkinsci-unstashParam-library"
node {
def file_in_workspace = unstashParam "file"
sh "cat ${file_in_workspace}"
}
If it's to do with Free-Style job & if your configuration looks similar to this - https://i.stack.imgur.com/vH7mQ.png then you can run simply do sh linecount.sh ${pqr} to get what you are looking for?

Execute Groovy script with arguments from Grails app

In reference to the question asked here
How to execute a Groovy Script from my Grails app?
which works except ...how do I pass arguments ?
def cmd = ['groovy.bat', 'C:\\my path\\mysript.groovy']
for a script that is run from the command line like
groovy myscript.groovy -name params.name -project params.name
using CliBuilder for arguments and params from form submitted
Groovy provides a simple way to execute command line processes. Yo can write the command line as a stringm and call the execute() method. Example:
"groovy myscript.groovy -name nancy -project testproj".execute()
More information in this link.
In case of arguments with whitespaces:
["groovy",
"my script with spaces.groovy",
"-name",
"nancy",
"-project",
"testproj"].execute()

Windows commands in ruby

How do I run a Windows command in an Ruby app?
I am trying to run something like:
output = `cd #{RAILS_ROOT}/lib && java HelloWorld #{param1} #{param2}`
I print the result of the line above and paste it to a command prompt in Windows and it works just fine. However, when i run app and hit this code, output is blank rather than have a string I get back from HellowWorld. In HelloWorld I do a System.out.print("helloworld")
The following:
output = `cmd.exe /C dir`
puts "OUTPUT #{output}"
Returns:
OUTPUT
Issue in JRuby 1.5.3 fixed in JRuby 1.5.5:
http://www.jruby.org/2010/11/10/jruby-1-5-5.html
Try to use File#join here. It will generate crossplatform path for you
http://apidock.com/ruby/File/join/class
my_path = File.join(RAILS_ROOT, "lib")
output = `cd #{my_path} && java HelloWorld #{param1} #{param2}`
Also you can execute your system commands this way:
`cd #{my_path} && java HelloWorld #{param1} #{param2}`
system("cd #{my_path} && java HelloWorld #{param1} #{param2}")
%x[cd #{my_path} && java HelloWorld #{param1} #{param2}]
Related topic: System call from Ruby
Backticks work fine for me. Try:
output = `dir`
to prove to yourself that it's working. At that point, your question is how to run a Java app from the command line, or why your particular app isn't work. Note that you can temporarily change the working directory like this:
Dir.chdir(File.join(RAILS_ROOT,'lib')) do
output = `...`
end

Resources