How to get the ant build xml file's own name? - ant

let's say I have a file named build_dev_linux.xml.
My question is
How can I find the ant script XML file's own name, build_dev_linux.xml
so I can put it on variable or property in that XML file.?

Ant defines a useful list of built-in properties:
basedir the absolute path of the project's basedir (as set
with the basedir attribute of <project>).
ant.file the absolute path of the buildfile.
ant.version the version of Ant
ant.project.name the name of the project that is currently executing;
it is set in the name attribute of <project>.
ant.project.default-target
the name of the currently executing project's
default target; it is set via the default
attribute of <project>.
ant.project.invoked-targets
a comma separated list of the targets that have
been specified on the command line (the IDE,
an <ant> task ...) when invoking the current
project.
ant.java.version the JVM version Ant detected; currently it can hold
the values "1.2", "1.3",
"1.4", "1.5" and "1.6".
ant.core.lib the absolute path of the ant.jar file.
The ant.file property is what you need. If you want just the file name without the path then you can use the basename task like
<basename file="${ant.file}" property="buildfile"/>

Related

Unable to create property file using Ant script

I am trying to create a new Property File with the below snippet of Ant script.
<propertyfile file="${path}/sample.properties">
<entry key="k1" value="v1"/>
</propertyfile>
It tries create the property file and I get the below error
(The system cannot find the path specified). Here the path includes the file name as well. I confirmed the parent folder where the property files needs to be created exists.
I created a build file with your snippet and couldn't see an issue. If the directory specified by the path property doesn't exist an exception is thrown but this seems like the correct behaviour.
I suggest you add some tests to confirm the actual value of the path property. Maybe it doesn't hold the value you expect.
build.xml
<project>
<property name="path" value="./test"/>
<propertyfile file="${path}/sample.properties">
<entry key="k1" value="v1"/>
</propertyfile>
</project>
Test Case 1 - 'test' directory not present - expected result FAIL
$ ls test
ls: cannot access test: No such file or directory
$ ant build.xml
Buildfile: /home/owen/stackoverflow/build.xml
[propertyfile] Creating new property file: /home/owen/stackoverflow/test/sample.properties
BUILD FAILED
/home/owen/stackoverflow/build.xml:3: java.io.FileNotFoundException: /home/owen/stackoverflow/test/sample.properties (No such file or directory)
Total time: 0 seconds
Test Case 2 - 'test' directory now available - expected result SUCCESS
$ mkdir test
$ ant
Buildfile: /home/owen/stackoverflow/build.xml
[propertyfile] Creating new property file: /home/owen/stackoverflow/test/sample.properties
BUILD SUCCESSFUL
Total time: 0 seconds
EDIT: Sorry didn't read the section mentioning that the parent folder exists. For me the given snippet works (in Eclipse). What does your path variable contains and which environment you use exactly ?
All folders specified by your path variable must exist before Ant can create the property file, even before any target is executed.

get error with bind tag in ant file

i got this error while build application jar with ant
Problem: failed to create task or type bind
Cause: The name is undefined.
Action: Check the spelling.
Action: Check that any custom tasks/types have been declared.
Action: Check that any / declarations have taken place.
and this a snippet is my build.xml file:
Your <bind verbose="true" load="true" binding="mdb-src/com/arj/sms/xmlHandler/mapping/binding.xml">
requires task definition of JiBX binding compiler. Just like any other custom Ant task. To make it more clear bind is not a "ant tag" like you call it and isn't "part" of ant syntax. You need to define it:
<taskdef name="bind" .. + classname/classpaths/filesets etc..
You have it?
If you have it, then make sure that task definition has proper path to the lib directory of your JiBX installation. Also note that some versions of Ant may not work properly if the JiBX installation path contains a space character.
Good luck.

listing all files and subdirectories using ant

I am trying to create a rpm package using ant task for that I need to create specfile which will have all the file names in the following format
%attr(0755, root, root) %dir dir1
%attr(0755, root, root) %dir dir1/dir2
%attr(0755, root, root) %dir dir1/dir2/dir3
%attr(0500, root, root) dir1/file1
%attr(0500, root, root) dir1/dir2/file1
I have such directory structure created during my build process but using ant I am not able to list all the files and directories which I can then write into my specfile
following is what I have tried to list the files but it does not differentiate between files and directory , moreover I need some way to iterate over the list.
<fileset id="dist.contents" dir="${nativePackageDir}" includes="**"/> |
<property name="prop.dist.contents" refid="dist.contents"/> | <target name="javaobject-library" depends="props">
<echo>${prop.dist.contents}</echo>
<dirset id="dist.contents" dir="${nativePackageDir}" includes="*"/>
<property name="prop.dist.contents" refid="dist.contents"/>
<echo>${prop.dist.contents}</echo>
Using dirset instead of fileset should fix your problem.
You simply have to write in java an ant task implementation, to which you'll provide as parameters the input directory and the path of the specfile you want to be written.
I find it better and more manageable to have reusable ant tasks in java, instead of having gigantic ant xml files.

ant zip; exclude all sub-directories and files

When creating a zip from ant, how can I exclude all sub directories and files from a given directory?
I have tried the following but it doesn't seem to prevent them from being included in the zip
<target name="zip">
<zip destfile="C:\Projects\example\builds\.zip"
excludes="C:\Projects\example\logs\**\*.*">
...
...
</zip>
</target>
From reading the documentation, and from reading the ant definitive guide I would assume that **\ should exclude any directory, and *.* would exclude any file of any extension
I want to include the logs directory, but nothing inside it.
I would recommend the following:
Change the name of your destfile to "C:\Projects\example\builds\logs.zip"
Set your basedir to "C:\Projects\example\"
Change your excludes value to "C:\Projects\example\logs\**\*" (that means any file)
Another option might be to use the project-defined basedir, and change all your paths to relative UNIX-like values.

Which Ant property contains the CWD when the ant script is run?

I don't want to get the basedir -- that appears to contain the build.xml script -- I want the CWD of the call to ant itself.
Basically, I want to do this:
$ cd /home/chrisr/projects/some_project
$ ant -f ../../tools/ant-build-rules/library.xml build-library
At this point, I need two things:
The path to ant-build-rules in absolute form; this is currently found in the basedir property, so I'm set there.
The path of some_project, in absolute form. This is what I don't know how to get.
Which property contains this information?
The java property user.dir contains the current directory
<project name="demo" default="printCWD">
<target name="printCWD">
<echo message="${user.dir}"/>
</target>
</project>
There is no such property, but you can run a script to get it.
${bsh:WorkDirPath.getPath()}
See urbancode.com.

Resources