How to get own $UID in ant? - ant

$ echo $UID # return my user ID
I want to use this information within an ant-buildfile. A solution is to set this variable explicitly:
ant some-target -DOWNER_UID=$UID
This way, in my buildfile "${OWNER_ID}" is available for usage.
Is there a way to get this information within the buildfile in an "internal" way, without the need to pass the $UID as parameter?

Not a cross-platform solution, but the following is likely to work in most Unix-like environments:
<exec executable="id" failonerror="true" outputproperty="uid">
<arg value="--user"/>
</exec>
<echo>uid: ${uid}</echo>

If you are talking about accessing an environment variable ($UID) in Ant, you can do like this:
<property environment="env"/>
<echo>UID: ${env.UID}</echo>
See the documentation for the Property task.

Related

passing command line arguments to ant

I am relatively new in ant, at school I have an assignment to do a build file. One of my questions is to copy to "/foldercopy" the file whose name(or path) is taken as argument for ant. I need to do something like:
ant cpfile file.txt
So ant will copy the file.txt to /foldercopy. I searched a lot on google but all I could find was something with "-Darg", but my teacher said that it's not correct. Is there any way to do it?
Plain command line arguments to ant are considered to be target names, so if you want to pass arguments to your target you need to use properties, via -D:
ant -Dfile=file.txt cpfile
and access the value as ${file} inside build.xml
This will help you:
<target name="copytask" >
<copy file="file.txt" todir="path-od-dir" failonerror="false" />
</target>

How to run a ruby file from ant script?

I need to run a ruby file(EG: d:\ruby\ruby file.rb) in command prompt by calling the file from ant script. I need the ant script. Can anyone help me?
You would probably just use <exec> on ruby with the rb file as the first argument.
Something like this:
<exec executable="c:\Ruby192\bin\ruby.exe" dir="d:\ruby">
<arg value="d:\ruby\ruby file.rb"/>
</exec>
It depends on where ruby.exe is installed, and if it's in your PATH.

How to execute Ant without buildfile

I'm working on a project to develop a custom Ant task.
As part of automated acceptance testing, I'd like to execute Ant from JUnit (the opposite of the usual desire) and pass it a string containing certain build XML to be tested via a command line param or stdin or pipe or something like that, rather than referring it to a buildfile on disk.
Is there any feasible way to do this?
Ant expects a file as input. You can however use the -f parameter to specify a tempfile as input:
$ cat <<EOF > tmp1.xml
<project name="demo" default="hello">
<target name="hello">
<echo>hello world</echo>
</target>
</project>
EOF
$ ant -f tmp1.xml
Obviously from Junit you're more likely the write the XML from Java :-)

Ant rule/task that can forward a variable number of cmd line arguments to a task

I want an ant task that includes command line passed arguments. The command line arguments can vary in number.
Specifically, for the <java> task within ant.
I would like to do this on the command line:
$ ant run foo bar ...
Ideally, "foo" and "bar" and other arguments "...", would be passed as trailing arguments to the java instance created in the <java> task.
java would see:
$ java -classpath ./output Foobar foo bar ...
In other words, I would like the same ant <java> task do the following:
$ ant run foo
# executes "java -classpath ./output Foobar foo"
$ ant run foo bar
# executes "java -classpath ./output Foobar foo bar"
$ ant run foo bar baz
# executes "java -classpath ./output Foobar foo bar baz"
I imagined this might look something like :
<project name="Foobar" basedir=".">
<property name="build" location="output"/>
<target name="run" >
<java failonerror="true" classname="Foobar" fork="true">
<classpath>
<dirset dir="${build}" />
</classpath>
<arg line="$#"/>
</java>
</target>
</project>
Notice the line
<arg line="$#"/>
I imagined something like the above would pass all remaining arguments to the java instance. (The purpose of this Question is to find that particular ant mechanism).
The methods I have seen for this require preconfigured ant variables. That is,
$ ant run -DARG1="foo" -DARG2="bar" ...
But that method precludes a variable length argument list.
Does anyone know a method for a variable number of argument that could be forwarded to an ant <java> task (preferably doesn't require writing a complex set of ant rules)?
That's not easy because Ant's command line looks like this:
ant [options] [target [target2 [target3] ...]]
That means your variable arg list foo bar baz items would be treated as targets, leading to similarly named Ant targets being run, or Ant throwing errors if they don't exist.
One option is to use the -Doption=value syntax to pass the variable argument list by means of a wrapper script. Perhaps:
ant -classpath ./output -Dmy_args=\"$#\" Foobar
in a shell script, then make use of the passed arg in the java task:
<arg line="${my_args}"/>
Another option would be to write a Java main() class that will accept the arguments as you want them to be, and then call Ant for you.

How can I ensure all output from Ant's exec task goes to stdout?

The Ant exec task has an output property which can be used to tell Ant where the output goes. I've used it to redirect the output to a file. The thing is, if I don't do something with the output, the stuff that Ant prints isn't that much of a help - it's not complete.
Is there someway of setting the output property to System.out?
When executing a batch file with ant's apply or exec tasks on Windows, I found there are special cases where some of the stdout and stderr is not captured by ant. (For example: if you call a batch file that in turn calls other commands (like node.exe), then the stdout and stderror from the child node.exe process is lost.)
I spent a long time trying to debug this! It seems that the batch file's stdout and stderr is captured, however commands called by the batch file are somehow not seen by ant. (perhaps because they are separate child processes). Using the output and error attributes as suggested above doesn't help because only some of the stdout and/or stderr is captured.
The solution I came up with (a hack) is to add these arguments at the end of the command:
<!--Next arg: forces node's stderror and stdout to a temporary file-->
<arg line=" > _tempfile.out 2<&1"/>
<!--Next arg: If command exits with an error, then output the temporary file to stdout, -->
<!--delete the temporary file and finally exit with error level 1 so that -->
<!--the apply task can catch the error if #failonerror="true" -->
<arg line=" || (type _tempfile.out & del _tempfile.out & exit /b 1)"/>
<!--Next arg: Otherwise, just type the temporary file and delete it-->
<arg line=" & type _tempfile.out & del _tempfile.out &"/>
Because this hack only applies to windows, remember to add #osfamily="windows" to the apply or exec task. And create similar task(s) for `#osfamily="unix", etc but without these extra arguments.
The output of exec does go to standard out unless you specify the output attribute.
If you want to output to System.out, then simply do not specify the "output" attribute. If you would like to redirect to a file AND print it to System.out, you can use the tee command, which will redirect output to a given file and also echo it to standard out... I do not know if Windows supports "tee" or an equivalent.
Maybe you want to look at the error, logError, and errorproperty attributes of the exec task too. These deal with the handling of the standard error stream from the exec'd process. There may be useful information there that is going awol for some reason - which might account for the incompleteness you see.
But, if the exec'd process decides to close stdout or stderr and send them elsewhere - there's little you can do.
I have faced similar problem: the output of command execution was suppressed. Perhaps that is the side effect when running cmd under WinXP (I an using maven-antrun-plugin). Anyway setting output="con" worked out perfectly:
<configuration>
<target>
<exec executable="cmd" output="con">
<arg value="/c" />
<arg value="..." />
</exec>
</target>
</configuration>
Working with Ant and Gruntjs:
For anyone trying to get this to work using Gruntjs. I was able to get it working by doing the following (in combination with darcyparker's answer).
In my Ant Build File:
<target description="run grunt js tasks" name="grunt">
<exec dir="/path/to/grunt" executable="cmd" failonerror="true">
<arg value="/c"/>
<arg value="jshint.bat"/> // I broke each task into it's own exec
<arg line=" > jshint.log 2<&1"/>
<arg line=" || (type jshint.log & del jshint.log & exit /b 1)"/>
<arg line=" & type jshint.log & del jshint.log &"/>
</exec>
<exec dir="/path/to/grunt" executable="cmd" failonerror="true">
// another grunt task (IE: uglify, cssmin, ect..)
</exec>
</target>
jshint.bat
#echo off
pushd "C:\path\to\grunt\"
#ECHO _____________________________________________
#ECHO GRUNT JSHINT
#ECHO _____________________________________________
grunt jshint --stack >>jshint.log
NOTE: Path to grunt would be where your Gruntfile.js is located. Also note, I had to initially create the log file (to get it to work with darcyparker's answer) which would output the stack trace from that particular task. This would then give me the grunt task stack output from wherever I call my ant target.
Finally note that pushd "C:\path\to\grunt\" won't be necissary if your bat files are in the same directory as your Gruntfile.js.
I was experiencing this same kind of issue trying to get the build process to fail in Ant after Karma tests intentionally failed, and executing them with "grunt test".
Just added /c before "grunt test", and it worked like a charm
<target name="unittest">
<echo>*** KARMA UNIT TESTING ***</echo>
<exec dir="api_ui" executable="cmd" osfamily="windows" logError="yes" failonerror="true">
<arg value="/c grunt test"/>
</exec>
</target>

Resources