I am working on the problem of writing Ant build files in a test-driven way. One thing I found myself needing was a “dry run” mode, like many Unix tools have. For example, make has the -n or –dry-run option, which shows what it would have done, but doesn’t really do it.
Today I found a partial solution to this problem, so that you can at least see which dependencies will be run when you run a particular ant target.
It’s an horrific hack, but it’s the best I can do at the moment.
We write some code in a <script> tag to hack all the targets in our project (at runtime). We modify the targets so they all have an “unless” attribute, set to a property name of “DRY-RUN”. Then we set the “DRY-RUN” property, and execute our target.
Ant prints out the names of all the targets in the dependency chain, even if they are not executed because of an unless attribute.
Running this build file normally, the tasks in the targets execute, so we can see that the <echo> happens:
$ ant Buildfile: build.xml targetA: targetB: [echo] DON'T RUN ME build: BUILD SUCCESSFUL Total time: 0 seconds
But when we run the dry-run target, only the target names are printed, and the <echo> task (and any other tasks) don’t:
$ ant dry-run Buildfile: build.xml dry-run: targetA: targetB: build: BUILD SUCCESSFUL Total time: 0 seconds
A lot of pain, for a partial implementation of very simple functionality that you’d expect to be a built-in feature? I couldn’t possibly comment.
2 thoughts on “Dry run mode for Ant (ant -n, ant –dry-run)”
Isn’t the real problem here that you are using Ant? Perhaps try a modern build tool such as Gradle? (http://www.gradle.org).
Hi Russell, yes it is. But, in case others are similarly doomed, I hope this might help them.