views:

42

answers:

1

I am converting ant to maven2. I meet a problem. Please help me: In build.xml, i have

<target name="clean">
  <delete file="${dir.dist}/${api.jarfile}" />
  <delete dir="${dir.build}/**" />
/target>
<target name="prepare1" depends="clean">
  <mkdir dir="${dir.build}" />
  <mkdir dir="${dir.generated.code.junit}/build" />
  <mkdir dir="${dir.dist}" />
</target>
<target name="prepare2" depends="clean">
  <mkdir dir="${dir.deploy}" />
  <mkdir dir="${dir.dist}" />
</target>

In maven i am using plugin antrun. Antrun is working fine with the first target. But for the second target, antrun does not support "depends". I can do a trick like: put the first target in clean-phase and the second in post-clean phase. So the second target will be run after the first target. But For the 3rd target, this trick will not work because the 3rd target should be run only after the 1st target (i dont want 2nd target run when 3rd run).

Anyway, this trick will not work because my build.xml is a big file with many targets and the relationship between them is complicated.

Is there anyway that can solve this problem? is there any thing in maven that make one task depend on another task?

Thanks.

A: 

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.

seanizer
Thanks seanizer. It helps me.
David