views:

450

answers:

3

I want to create an ant-buildscript, that includes some files as a sort of plugin.

So if I want to activate a feature in a project - say pmd-checking - I copy a pmd.xml in a directory and the build.xml get on the start the idea, that pmd.xml exists and imports it, so that new targets can be available to the build.

But the import-task can only be used as a toplevel-task, so I have no idea how to relize this functionality. Is this possible with ant and if yes, how can I do it?

EDIT: I would prefer a solution, that allows new targets to show up in the listing presented by 'ant -p'.

+2  A: 

You can use the ant task and even parameterize the target name. Here's an example:

<ant antfile="plugins/pmd.xml" target="${pmd-target}"/>

If you want more flexibility, I recommend checking gant or gradle.

Miguel Ping
That is a possible solution, but the new targets aren't present in the listing of 'ant -p'.
Mnementh
A: 

I'm not sure want you want is conceptually possible. The -p command-line argument doesn't execute any tasks, it just parses the file. What you want would require something to be executed.

But, I'd give the ant-contrib project a look. It has a conditional <if> task, which might make the top-level import work only when you want it to.

sblundy
+2  A: 

In the documentation for the import task, note the optional attribute. Set this to true and missing includes won't break the build.

So pmd.xml is included if found, but won't break the build if it isn't.

Not tested, so I'm not positive about ant -p including targets in the imported file if it is found.

Ken Gentle
'ant -p' lists target defined in imported files. I didn't see this attribute, this helps a bit. But I would prefer, if I can obtain the plugins at runtime.
Mnementh
How is the build file to know when to include the file? You'll either need to pass a property as a parameter and put some conditional targets in the build file. That will only work on execution, not with the "-p" option".Please give more detail on the use case.
Ken Gentle
I want to include all antfiles in a given directory or matched by a pattern. I see, there is no perfect solution, but I can combine your solution and the answer from Miguel Ping to catch most cases.
Mnementh