Best practice for code modification during ant build?

I think a simpler approach would be to have your Version. Java class read from a simple . Properties file included in the JAR, and just generate this .

Properties file at build-time in the Ant build. For example just generate: build. Number = 142 build.

Timestamp = 5/12/2011 12:31 The built-in buildnumber task in Ant does half of this already (see the second example).

I think a simpler approach would be to have your Version. Java class read from a simple . Properties file included in the JAR, and just generate this .

Properties file at build-time in the Ant build. For example just generate: build. Number = 142 build.

Timestamp = 5/12/2011 12:31 The built-in task in Ant does half of this already (see the second example).

1 agreed, another benefit of this is that browsing the jar file of your app a person can see what build number it is. In fact, you may want to consider putting this stuff in the jar's manifest. – MeBigFatGuy May 12 at 16:31 sounds good, makes perfect sense.

I guess I was on the wrong track by trying to modify the source itself. – kostja May 12 at 16:33 1 Ant provides a "manifest" task for putting information into the manifest -- and Java provides a Manifest class that can get information out. – Andy Thomas-Cramer May 12 at 19:44 @Andy Thomas-Cramer thank you, we ended up using the manifest, but with MyClass.class.getPackage().

GetImplementationVersion(). It was in one of the answers to this thread – kostja May 13 at 15:20 @kostja - oh, right, I'd forgotten that, despite using it in the past. – Andy Thomas-Cramer May 13 at 15:30.

My solution would be to: use on a token in the class: This replacement should only take place in the build steps performed by your build system, never within a compile/debug session inside an IDE. The build system setup should not submit changed source code back to the source repository anyway, so the problem of changed code does not exist with this approach. In my experience it does not help when you place the build information in a property file, as administrators tend to keep property files while upgrading - replacing the property file that came out of the install.(Build information in a property file is informational to us.It gives an opportunity to check during startup if the property file is in synch with the code version.

).

The build system setup should not submit changed source code back to the source repository...". Any well designed build system should adhere to this. +1 Changes should only go in one direction, and when cleaning you should only have to remove build targets and intermediate build artifacts (such as generated sources).

– Dev May 12 at 17:05 Thank you, the problem with this approach (was our first shot) is that the class either remains changed and is shown as such by the VCS and on more than one occasion was checked in with the tokens already replaced...so there has to be some kind of a rollback, either a as in 1. Or a copy switch like in 3. With all the resulting problems – kostja May 12 at 17:13 You're right though, considering our CI build - checkout, modify, build, delete...the token replace works perfect.

But we would like to be able to produce release-identical builds on our dev machines too – kostja May 12 at 17:18 @kostja, produce release-identical builds on our dev machines this is a dangerous wish imho, dev machines as good as always differ from the controlled build system, being in a constant state of flux. For developers it should not be a problem that the {{BUILD}} token is shown as build version. Testing (other than by teh developer) should always be based on versions build by your build system, not from a developers' machine.

– rsp May 12 at 18:53.

2 is generally the way I've seen it done, except that your not-ready-to-compile sources should be in a separate place from you ready-to-compile sources. This avoids the temporal issues you talk about as it should only be compiled once. This is a common pattern that shows up all the time in software build processes.

The pattern being: Generate source from some resource and then compile it. This applies to many things from filtering sources before compilation to generating interface stubs for RMI, CORBA, Web Services, etc... Copy the source to a designated 'generated sources' location and do the token replacement on the copies files to generate sources, then compile the generated sources to your compiled classes destination. The order of compilation will depend on whether or not your other sources depend on the generated sources.

Thank you for the detailed answer. This sounds like a clean and sane solution too. +1 – kostja May 12 at 17:07.

I remember we used the 4th approach in a little different way. You can pass release number to the ant script while creating a release. Ant script should include that in the release(config/properties file) and your class should read it from there may be using properties file or config file.

– kostja May 12 at 16:36 oh ya.. I think we both posted at nearly same time – sudmong May 12 at 16:52.

I always recommend to create some sort of directory and put all built code there. Don't touch the directories you checked out. I usually create a target directory and place all files modified and built there.

If there aren't too many *. Java files (or *. Cpp files), copy them to target/source' and compile there.

You can use thetask with a` to modify this file one file with the build number as you copy it. This way, you're making no modification in the checked out source directory, so no one will accidentally check in the changes. Plus, you can do a clean by simply deleting the target directory.

If there are thousands, if not millions of *. Java files, then you can copy the templates to target/source and then compile the source in both {$basedir}/source and target/source. That way, you're still not mucking up the checked out code and leaving a chance that someone will accidentally check in a modified version.

And, you can still do a clean by simply removing target.

You can use thetask with a` to modify this file one file with the build number as you copy it. This way, you're making no modification in the checked out source directory, so no one will accidentally check in the changes. Plus, you can do a clean by simply deleting the target directory.

If there are thousands, if not millions of *.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions