craigmiller160
craigmiller160

Reputation: 6273

Ant: Is it possible to create a dynamic ant script?

So, at work, I frequently have to create virtually identical ant scripts. Basically the application we provide to our clients is designed to be easily extensible, and we offer a service of designing and creating custom modules for it. Because of the complexity of our application, with lots of cross dependencies, I tend to develop the module within our core dev environment, compile it using IntelliJ, and then run a basic ant script that does the following tasks:

1) Clean build directory

2) Create build directory and directory hierarchy based on package paths.

3) Copy class files (and source files to a separate sources directory).

4) Jar it up.

The thing is, to do this I need to go through the script line by line and change a bunch of property names, so it works for the new use case. I also save all the scripts in case I need to go back to them.

This isn't the worst thing in the world, but I'm always looking for a better way to do things. Hence my idea:

For each specific implementation I would provide an ant script (or other file) of just properties. Key-value pairs, which would have specific prefixes for each key based on what it's used for. I would then want my ant script to run the various tasks, executing each one for the key-value pairs that are appropriate.

For example, copying the class files. I would have a property with a name like "classFile.filePath". I would want the script to call the task for every property it detects that starts with "classFile...".

Honestly, from my current research so far, I'm not confident that this is possible. But... I'm super stubborn, and always looking for new creative options. So, what options do I have? Or are there none?

Upvotes: 0

Views: 130

Answers (1)

Mark O'Connor
Mark O'Connor

Reputation: 77971

It's possible to dynamically generate ANT scripts, for example the following does this using an XML input file:

Personally I would always try and avoid this level of complexity. Ant is not a programming language.

Looking at what you're trying to achieve it does appear you could benefit from packaging your dependencies as jars and using a Maven repository manager like Nexus or Artifactory for storage. This would simplify each sub-project build. When building projects that depend on these published libraries you can use a dependency management tool like Apache ivy to download them.

Hope that helps your question is fairly broad.

Upvotes: 2

Related Questions