tags:

views:

1341

answers:

5

I know how to run a single target in ANT, but it also checks the "depends" attribute and runs those before the target. Is there a way to prevent this or a way to structure my ANT file so that I can do this more easily?

A: 

I think your only simple choice here would be to just make a copy of the target in question and make it not have dependencies.

yalestar
I am just removing the depends attributes now (which is almost the same thing), but I am wondering for a possibly cleaner solution.
GreenieMeanie
+4  A: 

Instead of actually copying and pasting, you can also use Antcall task instead of dependencies. Gives a cleaner and more coherent build.xml.

As a bonus, you can also parametrize the calls (suitable for more build.xml refactoring). For instance:

<target name="default">
  <antcall target="doSomethingElse">
    <param name="param1" value="value"/>
  </antcall>
</target>

<target name="doSomethingElse">
  <echo message="param1=${param1}"/>
</target>
Daniel Ribeiro
+6  A: 

Create a "withoutdeps" version of the target. If you had

<target name="A" depends="B">
   ...
</target>

Change to

<target name="A" depends="B,AwithoutDeps"/>

<target name="AwithoutDeps">
    ...
</target>

Now you can call A as normal (which will fire off B then AwithoutDeps) or just call AwithoutDeps explicitly and no deps fired. [Note that "depends" calls the dependencies in order]

Of course, choose some better names than these ;)

Scott Stanchfield
Very similar to my solution - I think the difference is just a question of style. This exploits the dependency functionality of ant, my solution exploits the "encapsulation" functionality.
Jared
Thanks! This seems cleaner than copying and pasting.
GreenieMeanie
@Jared - I thought about of antcall as well, but it's more expensive AFAIK, as it sets up a copy of the properties and such. Not that it's a huge deal though...
Scott Stanchfield
@Scott - if only my build were sufficiently quick/efficient that I was worried about the cost of a copy of the properties :P...oh to have that problem :P
Jared
+1  A: 

I would do something like this:

<target name="doSomethingNoDeps">
   ...
</target>

<target name="doSomething" depends="doSomeOther">
  <antcall target="doSomethingNoDeps"/>
</target>
Jared
+1  A: 

One possibility is to use the if or unless attribute on the dependency target(s). For example:

<target name="dependency1" unless="dependency1.disabled">
  <echo>Hello from dependency 1!</echo>
</target>

<target name="dependency2" unless="dependency2.disabled">
  <echo>Hello from dependency 2!</echo>
</target>

<target name="main-target" depends="dependency1, dependency2">
  <echo>Hello from the main target!</echo>
</target>

Now you can run Ant with -Ddependency1.disabled=true and/or -Ddependency2.disabled=true to leave out the dependencies you don't want, but the default will still be to include them.

And of course, you could simply have a "global" dependencies.disabled property if that's easier for you.

If you want to do the converse of this behavior (where the dependencies are excluded by default), just use the if instead of unless (and have property names like "dependency1.enabled" instead of "disabled").

Matt Solnit