开发者

Reuse and execution control in Ant

I think I must not "get" Ant. I am having a hard time figuring out how to implement reuse and control execution of a sequence of targets. Please help.

I need my build script to create two builds: a debug build and a production build. Currently I am using antcall to hack-around my misunderstanding of Ant.

Let me use pseudo imperative code to describe how I want my builds to go:

//this is my entry point
function build-producti开发者_运维知识库on-and-debug() =
  prepare()
  build-production()
  build-debug()
  cleanup()

function build-production() =
  pre-process()
  compile()
  post-process()
  package("production")

function build-debug() =
  compile()
  package("debug")

How am I suppose to approach this with Ant?


Maybe you can put your ant code to give a better answer. But one way to do that is to use the depends attribute

<target name="prepare">
     //do something to prepare
</target>
<target name="cleanup">
    //do something to cleanup
</target>

<target name="build-production">
    //build production
</target>

<target name="build-debug">
    //build debug
</target>

<target name="build-production-debug" depends="prepare,build-production, build-debug, cleanup">
      //do something or nothing
</target>

With this you are telling to ant that before execute the "build-production-debug" target you want to first run all the targets listed on "depends" attribute an do it on that order.


Here's the outline of what I came up with, I am still using antcalls but only as an entry point in order to parameterize my builds. The key discovery for me was using the if condition on a target, to control whether the target gets executed, noting that the targets in its depends chain still execute. The condition and isset tasks also helped out in certain places.

<project>
    <target name="-init">
    </target>

    <target name="-prod-preprocess" depends="-init" if="production">
    </target>

    <target name="-compile" depends="-prod-preprocess">
    </target>

    <target name="-package" depends="-compile">
    </target>

    <target name="build-prod">
        <property name="production" value="true" />
        <property name="package.dir" location="${production.package.location}"/>
        <antcall target="-package" />
    </target>

    <target name="build-debug">
        <property name="package.dir" location="${debug.package.location}"/>
        <antcall target="-package" />
    </target>

    <target name="build-both">
        <antcall target="build-debug" />
        <antcall target="build-prod" />
    </target>
</project>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜