Execute Ant action always and independently from target - ant

Is there a way to specify actions like <copy> in an Ant buildfile that get executed every time the build file gets read by ant (regardless of the target which is called)?
The background is: I want a *.properties-file to be automatically created from a template when it's not present. I know, I could specify a target which does this and then include it at the root of the dependency-tree but maybe there is a more elegant solution. Because actually the problem is a bit more complex: the ant file where the *.properties-file is read-out is imported by other build files and I don't want to cross-reference targets between them.
I hope I explained my problem sufficiently. In cases of questions do not hestitate to ask.
This is my first posting here. Hope you can help - Greetings from Germany, Ben.

Just put the code at the top of the file, outside of a target definition.
<project name="myproject" default="mytarget" basedir=".">
<echo message="Hello there." />
<target name="mytarget">
<!-- Do stuff. -->
</target>
<target name="myothertarget">
<!-- Do other stuff. -->
</target>
</project>
In this case the echo will get executed once before any target, regardless of which target is invoked.

Related

Reading property names from a properties file before loading it (ANT)

I need to retrieve all the properties' names from a properties file before loading it (using Ant)
I'll go into detail to explain the whole process:
A first properties file (let's name it as a.properties) is read and
all its properties loaded as project's properties.
#a.properties's contents
myvar1=1
myvar2=someTextHere
A second file (let's say b.properties) has to be loaded on the
project. Some already-set properties can also be contained in this
second file, so what we have to do is to update such variables with
the value found on it (by means of the ant-contrib's var target)
#b.properties's contents
myvar1=2 #updated value for a property that's is already set on the project
myvar3=1,2,3,4,5,6
So the expected subset (from a ANT project's properties perspective)
of property/value pairs would be:
myvar1=2
myvar2=someTextHere
myvar3=1,2,3,4,5,6
We cannot change the order in which those files are loaded on the project, which would be the easiest way of solving the issue (because of the behavior adopted by Ant when setting's properties)
Any feedback will be highly appreciated.
Regards
I assume that you need to read properties from different files before you build your source code
<target name=-init-const-properties description="read all properties required">
<propertyfile file="AbsolutePathToPropertyFile" comment="Write meaningfull
about the properties">
<entry value="${myvar1}" key="VAR1"/>
<entry value="${myvar2}" key="VAR2"/>
</propertyfile>
</target>
Note: you need to add proper AbsolutePathToPropertyFileand comment if required
In the target -init-const-properties you can add as many files you want to read and use this target as dependent target in which you going to use these property values. hope this will answer your question
I recommend having a standard file for build defaults called "build.properties". If you need to override any settings, then create an optional file called "build-local.properties".
My advice is to keep build logic simple. Using the ant-contrib extension to make properties act like variables is rarely needed in my experience.
Example
├── build-local.properties
├── build.properties
└── build.xml
Running the project produces the following output, where the value "two" is substituted:
$ ant
build:
[echo] Testing one, dos, three
Delete the optional file and it goes back to default values:
$ rm build-local.properties
$ ant
build:
[echo] Testing one, two, three
build.xml
The secret is the order in which the property files are loaded. If they don't exist then they don't create properties.
<project name="demo" default="build">
<property file="build-local.properties"/>
<property file="build.properties"/>
<target name="build">
<echo message="hello ${myvar1}, ${myvar2}, ${myvar3}"/>
</target>
</project>
build.properties
myvar1=one
myvar2=two
myvar3=three
build-local.properties
myvar2=dos
Finally, the approach I followed was to specify the second properties file (b.properties) from the command line:
ant <my_target> -propertyfile b.properties
So that's work fine to me...
Thanks all of you for your help.

Multiple configuration files for the ProGuard Ant task

I am using the ProGuard ant task, and everything is great, except that my ProGuard configuration file is huge. Also, different tasks use different ProGuard configuration files, and there is a lot of copy-pasting that I would like to refactor into separate .pro files.
<taskdef resource="proguard/ant/task.properties" classpath="C:/Program Files/proguard4.7/lib/proguard.jar"/>
<target name="obfuscated_jar" depends="raw_jar">
<proguard configuration="core.pro lib1.pro lib2.pro">
<outjar path="prog_obfuscated.jar"/>
</proguard>
</target>
The above doesn't work, because it treats the multiple .pro files as one big filename. I'm a known idiot w.r.t. ant, am I missing something obvious? Thanks!
You can create a single main .pro file that contains -include options pointing to your actual .pro files.
This answer isn't great, but it works...
<taskdef resource="proguard/ant/task.properties" classpath="C:/Program Files/proguard4.7/lib/proguard.jar"/>
<target name="obfuscated_jar" depends="raw_jar">
<concat destfile="proguard_temp.pro">
<filelist dir="." files="core.pro,lib1.pro,lib2.pro"/>
</concat>
<proguard configuration="proguard_temp.pro">
<outjar path="prog_obfuscated.jar"/>
</proguard>
<delete file="proguard_temp.pro"/>
</target>
Looks like only one file allowed in configuration attribute.
Edited
And attributes allowed only on <proguard> element. I have another possible solution. Try to concatenate your config-files into one with Ant concat-task and pass this temporary file to <proguard configuration=""> attribute.
Also, it's possible to modify ProGuardTask-class to accept several files as arguments and concatenate them later. And same result could be achieved with Ant macrodef .

Intellij IDEA doesn't recognized XJC task attributes

I have an ant build.xml file with XJC task definition:
<taskdef name="xjc" classname="com.sun.tools.xjc.XJCTask">
<classpath>
<fileset dir="jaxb" includes="*.jar" />
</classpath>
</taskdef>
jaxb dir cotnains jaxb-xjc.jar with XJCTask class inside.
Then I call xjc task in some target:
<target name="mytarget">
<xjc target="src" package="com.p1.Person" header="false">
<schema dir="src/com/p1" includes="Person.xsd"/>
</xjc>
</target>
Intellij IDEA doesn't recognize the structure/schema of the xjc call and highlights all attributes (target, package, header) and containing elements (schema) in red.
If I choose Ant options and add jaxb-xjc.jar to additional class path list this doesn't help.
I use bundled Ant 1.8.2
The bad thing is that when I compile it in IDEA I get a lot of related errors, but when I run build script everything works fine. I want to suppress these errors.
Any ideas?
The answer comes from this comment in a related bug in the IDEA issue tracker.
http://youtrack.jetbrains.net/issue/IDEA-11248#comment=27-57354
For the XJCTask issues with IDEA, just use XJC2Task in your taskdef.
If you look at the source of XJC2Task, it has the setters exposed so that IDEA can resolve them:
http://grepcode.com/file/repo1.maven.org/maven2/com.sun.xml.bind/jaxb-xjc/2.1.13/com/sun/tools/xjc/XJC2Task.java#XJC2Task.setPackage%28java.lang.String%29
However, XJCTask is just a class to dynamically delegate to JAXB1 or JAXB2 on the fly so IDEA is unable to resolve these properties since the class you are defining in the taskdef doesn't have the setters on it.
http://grepcode.com/file/repo1.maven.org/maven2/com.sun.xml.bind/jaxb-xjc/2.1.13/com/sun/tools/xjc/XJCTask.java#XJCTask.getCoreClassName%28%29
Edit:
Basically in JAXB2, XJCTask doesn't actually contain the task - it delegates to the actual task XJC2Task.
Here are some better links to the source:
XJCTask in JAXB 1
http://java.net/projects/jaxb/sources/version1/content/trunk/jaxb-ri/xjc/src/com/sun/tools/xjc/XJCTask.java?rev=197
XJCTask in JAXB2
http://java.net/projects/jaxb/sources/version2/content/trunk/jaxb-ri/xjc/facade/com/sun/tools/xjc/XJCTask.java?rev=3863
XJC2Task in JAXB2
http://java.net/projects/jaxb/sources/version2/content/trunk/jaxb-ri/xjc/src/com/sun/tools/xjc/XJC2Task.java?rev=3863
If you look at your jaxb-xjc-ri-2.x-xx.jar you will see that it contains a package called "1/com/sun/tools/xjc/"
This is what gets called from the XJCTask in JAXB2 if you run your ant task with setting the version to 1.0.
I expect it was put in to allow easier transitions to v2 from v1 back in the day.
XJC2Task is what is called if you are using v2.
Realistically you aren't going to set it to 1.0 so you might as just call the XJC2Task directly.

Ant: Create directory containing file if it doesn't already exist?

Basically, I get a path like "C:\test\subfolder1\subfolder2\subfolder3\myfile.txt", but it's possible that subfolders 1-3 don't exist already, which means I'd get an exception if I try to write to the file.
Is there a way to create the directory structure the target file is in, either by using some task that creates the structure when it outputs to the file and then deleting the file, or by parsing the directory part of the path and using the mkdir task first?
Ant will create the full tree of directories for you when you use the <mkdir> task. So you just need to use the <dirname> task to get the directory name from the file name.
<dirname property="directoryProperty" file="${filePathProperty}"/>
<mkdir dir="${directoryProperty}" />
The first line extracts the directory portion of your file path and stores it in the directoryProperty property. The second line creates the directory (and any parent directories that don't exist).
This task works well
<mkdir dir="${file}/../"/>
Sometimes we could have an alternate choice, using touch task
<touch file="${file}" mkdirs="true" verbose="true"/>
This task should do the job but would have a side effect to create the file with zero size
Just make failonerror=false to avoid the error to stop the whole logic.
<delete includeemptydirs="true" failonerror="false">
<fileset dir="${builder-base.dir}" includes="**/*"/>
</delete>
Using the
<mkdir dir="${dir}"/ >
inside your <target> tag should work, but I am not sure what else you want to do along with mkdir?
I'm not 100% sure it'll work but you might be able to do something like the following to make the parent directory you're after:
<mkdir dir="${file}/../"/>
If that doesn't work straight off then it might be worth defining a property using the location syntax before creating a directory with the new property:
<property name="dir" location="${file}/../" />
<mkdir dir="${dir}" />
Well-behaved Ant tasks are generally expected to create any necessary directory structures unless there is a good reason not to.
Are you writing a task? If so you should add the directory creation logic to your task. If you are getting the task from a third party you should point this fact out to them and have them fix their task. Failing that Dan's solution should work.

ant iterate over files

I want to iterate over a list of jars (undefined number) and add them all to the jar file.
To add them I plan to use something like this:
<jar id="files" jarfile="all.jar">
<zipfileset src="first.jar" includes="**/*.java **/*.class"/>
<zipfileset src="second.jar" includes="**/*.java **/*.class"/>
</jar>
but how do I iterate over them? I don't have ant-contrib
Thanks!
Just use zipgroupfileset with the Ant Zip task
<zip destfile="out.jar">
<zipgroupfileset dir="lib" includes="*.jar"/>
</zip>
This will flatten all included jar libraries' content.
If you do not have access to ant-contrib For task, you may end up to have to define your custom Task for doing what you need...
If you have ant1.6 and above, you can also try subant (see New Ant 1.6 Features for Big Projects):
If you use <subant>'s genericantfile attribute it kind of works like <antcall> invoking a target in the same build file that contains the task.
Unlike <antcall>, <subant> takes a list or set of directories and will invoke the target once for each directory setting the project's base directory.
This is useful if you want to perform the exact same operation in an arbitrary number of directories.

Resources