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>
Related
Is it possible to run an executable from ant using a working directory other than the one which contains the executable? This small ant project demonstrates what I'm trying to achieve.
Folder structure
ant test
| build.xml
| bin
| | debug
| | | program.exe
| | release
| | | program.exe
| | inputs
| | | ...
| | outputs
| | | ...
build.xml
<project name="test" basedir="./">
<target name="run">
<exec executable="${configuration}\program.exe" dir="bin"/>
</target>
</project>
${configuration} is set to release.
bin needs to be the working directory so the executable can reference files in inputs and outputs correctly, so I want to be able to run the executable contained in bin/release from the bin directory. However, ant fails to find the executable:
BUILD FAILED
D:\ant test\build.xml:6: Execute failed: java.io.IOException: Cannot run program "release\program.exe" (in directory "D:\ant test\bin"): CreateProcess error=2, The system cannot find the file specified
I am able to work around this on windows by launching cmd.exe in the bin directory and then passing it the parameter release\program.exe, but I need to be able to use the ant file on multiple platforms, so I was hoping to find a consistent syntax.
One option I considered was using conditional tasks and having separate syntaxes for windows and unix. However, in the actual project, the exec statement occurs inside a macro, and the target calls the macro a large number of times. Since conditions can only affect things at the target level, I would have to duplicate the long list of macro calls for each syntax I wanted to target, e.g.
<target name="runAll" depends="runAll-win,runAll-unix"/>
<target name"runAll-win" if="win">
<myMacro-win .../>
<!-- Huge list of macro calls -->
...
<myMacro-win .../>
</target>
<target name"runAll-unix" if="unix">
<myMacro-unix .../>
<!-- Huge list of macro calls -->
...
<myMacro-unix .../>
</target>
The dir attribute specifies the working directory but the executable path is resolved against the project basedir. Hence the task will search for the executable D:\ant test\release\program.exe.
A simple solution is to add resolveexecutable="true" to the task call:
<target name="run">
<exec executable="${configuration}\program.exe" dir="bin" resolveexecutable="true" />
</target>
From the exec task documentation:
When this attribute is true, the name of the executable is resolved firstly against the project basedir and if that does not exist, against the execution directory if specified. On Unix systems, if you only want to allow execution of commands in the user's path, set this to false. since Ant 1.6
$ 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.
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.
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 :-)
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.