how to call an ant target when overriding the target in a child file

nheid picture nheid · Sep 12, 2011 · Viewed 9.6k times · Source

i have a project that uses a parent ant file

similar to this:

<project name="some-portlet" basedir="." default="deploy">  
  <import file="../build-common-portlet.xml" />
  <target name="test">
    <echo message="do foo"/>
    RUN TEST FROM PARENT HERE
  </target>
  </project>

now i want to override the parent test target in this way:

  • do some copying of jars needed
  • run the test target from the parent file

the first part is no problem, but i do not see a way to call test from the parent file

i want the target to be named test also, so that CI can simply run the test target.

is there a way to call test in ../build-common-portlet.xml ?

Answer

Alexander Pogrebnyak picture Alexander Pogrebnyak · Sep 12, 2011

The simplest way is to use dependency on parent's test.

For that it's important that you keep <project> name attribute in sync with its file name ( OK that's not, strictly speaking, necessary, but greatly improves your script's readability and maintainability ).

So in build-common-portlet.xml:

<project
  name="build-common-portlet" <-- note the name
  ...
>
  <target name="test">
    <echo message="Calling parent test target"/>
    ...
  </target>
</project>

That way you can just do:

<project name="some-portlet" basedir="." default="deploy">  
  <import file="../build-common-portlet.xml" />
  <target name="test"
    depends="build-common-portlet.test" <-- note parent specification
  >
    <echo message="do foo"/>
    RUN TEST FROM PARENT HERE
  </target>
</project>

>> In reply to comment

If you want to do some work before running parent's test, just create a new target and put dependency on it before parent's test:

<project name="some-portlet" basedir="." default="deploy">  
  <import file="../build-common-portlet.xml" />

  <target name="copy-jars">
    <echo message="copying jars"/>
  </target>

  <target name="test"
    depends="
      copy-jars,
      build-common-portlet.test
    "
  />
</project>