My Java app displays its version number in a few places, so I store that as a public final variable in the app's main class. Is there any way to access that in my Ant build tasks? In the spirit of automating the build process, I'd like Ant to automatically name the zipped distributable with the current version number, but I'm not sure how. I'm guessing it would look something like…
<target name="createZip" depends="build">
<zip destfile="../dist/MyApp_${MyApp.version}.zip">
<fileset dir="../dist">
<include name="**/MyApp"/>
</fileset>
</zip>
</target>
I'm just not sure what to put in the place of ${MyApp.version}. I realize I could put this in the build properties file, but it would be a lot more convenient to be able to pull it straight from the class file I'm already storing it in.
Best Answer
I don't think there are any built in Ant tasks that do what you want. However, you could roll your own, taking advantage of the extensible nature of Ant.
I've hacked up a really (and I do mean really) dirty example you can use as a spring board to a proper Task.
An example build file using this task:
DummyMain.java:
The result of a build using the Ant file:
There are a number of random problems with this task: it relies on deprecated methods, it takes files instead of class names, and the error reporting leaves a bit to be desired. However, you should get the gist of what is needed for a custom task that solves your problem.