a) Why not just throw them together? When you run ant with maven, you can't call individual targets anyway, you are binding an ant run to a maven phase. So you could do the following in the clean phase:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<phase>clean</phase>
<configuration>
<tasks>
<delete file="${dir.dist}/${api.jarfile}" />
<delete dir="${dir.build}/**" />
<mkdir dir="${dir.build}" />
<mkdir dir="${dir.generated.code.junit}/build" />
<mkdir dir="${dir.dist}" />
<mkdir dir="${dir.deploy}" />
<mkdir dir="${dir.dist}" />
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
and if you need some more work in other phases, add more executions:
</execution> <!-- end tag from above -->
<execution>
<phase>generate-sources</phase>
<configuration>
<tasks>
<!-- some more ant tasks -->
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
b) if you use maven, you should think like maven does. A lot of what you are doing in your ant task is default maven behavior.
In the clean phase, the clean:clean goal is executed, which deletes the target directory (and the jar along with it). If you put all of the dirs you are using underneath target (which is considered good practice in maven), maven will delete them (and create them) automatically.
Maven is all about convention:
- anything that is generated goes inside target
- classes and resources for the main artifact go inside target/classes
- classes and resources for testing go inside target/test-classes
- generated resources go inside target/generated-resources/yourfoldername
- generated sources go inside target/generated-sources/yourfoldername
All major plugins expect you to follow these conventions (they have defaults according to them) and using them will make maven life a lot easier.
E.g. mvn clean
will ensure that you delete everything that was generated.
There are a few exceptions, plugins that have to store data in different places and therefore can't be picked up by clean:clean. These plugins usually provide alternative clean mojos (eclipse:clean and idea:clean are examples), but try not to create more exceptions.
Also, according to maven convention, nothing should be created in phase clean. There are plenty of phases to generate stuff, like generate-resources
, generate-sources
, generate-test-resources
, generate-test-sources
. Also: creating folders is usually unnecessary, all maven plugins I know create folders as needed when writing files.