Can I find and import files into an Ant build file at runtime?

I want to create an Ant build file that includes some files as a kind of plugin.

So, if I want to activate a feature in a project - say pmd-check - I will copy pmd.xml in the directory and build.xml will start realizing that pmd.xml exists and will import it so that new targets can be buildable.

But the "import" task can only be used as a top-level task, so I have no idea how to change this functionality. Is this possible with Ant, and if so, how can I do it?

EDIT: I would prefer a solution that allows new targets to be shown in the listing provided ant -p

.

+1


source to share


4 answers


It is not explicitly listed in the documentation of the import task, but the task accepts a set of files as an alternative to a single file. Hence this at the top level should do the trick, and the targets created are listed ant -p

:

<property name="plugins.dir" value="plugins" />
<fileset id="plugin.modules" dir="${plugins.dir}">
    <include name="**/*.xml" />
</fileset>

<import>
    <fileset refid="plugin.modules" />
</import>

      

One wrinkle with this is that there must be at least one plugin in the "plugins" directory, or the import will fail. You can simply create a placeholder file - for example empty.xml

:



 <project />

      

After that, you just need to add new plugins to the plugins directory and they will be imported by future builds.

+2


source


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

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

      



If you need more flexibility I recommend checking out gant or gradle .

+3


source


In the documentation for the import task, notice the attribute optional

. Set this value to true

, and missing enabled features won't break the build.

So, pmd.xml

included if found, but won't break the assembly if not.

Untested, so I'm not sure about ant -p

including targets in the imported file if found.

+3


source


I'm not sure what you want, it's possible. The command line argument -p

does nothing, it just parses the file. For which you need to do something.

But I would give the ant-contrib project a project. It has a conditional <if>

task that could make the top-level import only work when you want it to.

0


source







All Articles