Ant - How can I run the same thing depends on multiple targets

Is there a way to get ant to try to run multiple targets multiple times. Consider this:

<target name="buildall" depends="mycommon,myDAO" />

<target name="myCommon" depends="initCommon, clean, makedir, compile" description="">
    <echo> Build completed for myCommon </echo>
</target>

<target name="myDAO" depends="initDAO, clean, makedir, compile" description="">
    <echo> Build completed for myDao </echo>
</target>

      

I would like buildAll to call myCommon, which calls initCommon, clean up, makedir, compile, and then call myDAO, which calls initDAO, clean, makedire, compile.

So, I want the cleanup, makedir and compilation tasks to be done multiple times. They are generic and are executed based on properties set in the initXXX task.

I've tried this:

<target name="buildall">
    <antcall  target="myCommon" />
    <antcall target="myDao" />
</target>

      

but every time that's not what I want, everything outside of the tasks gets executed. Any thoughts?

0


source to share


1 answer


First: Don't use <antcall/>

, this is usually a sign that you did something wrong.

Now understand that Ant is not a programming language where you tell Ant what you want to do and the order you want to do. Ant is a matrix dependency language. You just tell Ant what you want (I want to build this jar) and let Ant figure out what it should do. Ant is careful not to run the target multiple times.

For example, both myCommon

and myDAO

cause the target clean

. Ant duly notes that both require clean

target, and then calls it clean

once and only once before it completes both of your targets. Let's assume it is running Ant.

So let Ant do its job. First, you shouldn't be cleaning under normal circumstances. The joints are meant to minimize rebuilding to speed up the task. If you haven't changed the file *.java

, why should you force me to rebuild the corresponding file *.class

?

Second: don't double dependencies: for example, if I want to create a target myDAO

, I want to compile the code (maybe build a jar or war). This should depend on all my goals myDAO

. Now when I compile, I may need to make my directory, and when I create my directory, I may need to run init:

<target name="clean">
    <echo>Clean up my working directory to be nice and sparkly</echo>
</target>
<target name="initDAO">
    <echo>Initialize stuff for my DAO build</echo>
    <echo>Maybe setup some properties?</echo>
</target>
<target name="makedir"
     depends="initDAO">
    <echo>I need my directories for building.</echo>
    <echo>But first, I need to setup stuff"</echo>
</target>
<target name="compile"
    depends="makedir">
    <echo>I need to compile my dao source"</echo>
    <echo>But first, I need to make the necessary directories</echo>
<target>
<target name="myDAO"
    depends="compile">
    <echo>Here where I package up my DAO</echo>
    <echo>But I have to compile stuff before I can package it</echo>
</target>

      



Pay attention to the above structure. If I ran a goal myDAO

, Ant will look at addiction, and then run initDAO

, makedir

, the compile and finally myDAO

to pack everything. Again, I have a target clean

that will restore my workspace to the original (before anything is created) condition, but I don't call it part of the package because I don't want to repeat the work.

"Ah!", You say, "But I need to clean up because myCommon

both myDAO

use the same directories for build and package."

Okay, don't do this. Instead, make sure your two packages are using different build and packaging target directories. This way, you don't have to clear the clutter from one to the other. And you can change one original file, rebuild and not recompile all over again.

You can get rid of the problems by specifying macros to handle things in between. For example, you can define a compilation macro that takes a source directory name as parameters, and it will create destdir based on that source directory name and compile your generals and DAO

targets.

So, let Ant work its magic. Use dependencies not as a means of telling Ant how to do something, but simply by telling Ant that a particular target depends on another target. Let Ant determine the order of execution. Also, don't tweak your tasks to require you to clean out directories and reinitialize everything. You want Ant to help you minify your build by not rebuilding or rewriting files that haven't changed.

+6


source







All Articles