<!--
 Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com

 The software in this package is published under the terms of the CPAL v1.0
 license, a copy of which has been included with this distribution in the
 LICENSE.txt file.
-->

<project name="Mule Examples: WebApp" default="package">

  <property name="app.name" value="mule-examples"/>

    <property name="dir.java" value="src"/>
    <property name="dir.conf" value="conf"/>
    <property name="dir.resources" value="src/main/resources"/>
    <property name="dir.webapp" value="src/main/webapp"/>
    <property name="dir.build" value="target"/>
    <property name="dir.classes" value="${dir.build}/classes"/>

    <target name="clean"
            description="Clean the output directory (start from scratch).">
        <delete dir="${dir.build}"/>
    </target>

    <target name="sanity.check" depends="clean">
        <property environment="env"/>
        <property name="dir.mule.home" value="${env.MULE_HOME}"/>
        <property name="dir.user.lib" value="${dir.mule.home}/lib/user/"/>

        <fail message="MULE_HOME environment variable has not been set.">
            <condition>
                <or>
                    <equals arg1="${dir.mule.home}" arg2=""/>
                    <!-- Using a trick here. If there was no MULE_HOME environment variable set,
                        ant will set the value to the unprocessed template string -->
                    <equals arg1="${dir.mule.home}" arg2="$${env.MULE_HOME}"/>
                </or>
            </condition>
        </fail>
    </target>

    <target name="init"
            depends="sanity.check">

        <!-- Create the build directory if it doesn't exist -->
        <mkdir dir="${dir.classes}"/>

        <!-- Configure Mule classpath (mirrors wrapper.conf settings -->
        <path id="classpath.mule">
            <pathelement location="${dir.mule.home}/conf"/>
            <fileset dir="${dir.mule.home}/lib/user">
                <include name="**/*.jar"/>
            </fileset>
            <fileset dir="${dir.mule.home}/lib/mule">
                <include name="**/*.jar"/>
            </fileset>
            <fileset dir="${dir.mule.home}/lib/opt">
                <include name="**/*.jar"/>
            </fileset>
            <fileset dir="${dir.mule.home}/lib">
                <include name="**/*.jar"/>
            </fileset>
        </path>
    </target>

    <target name="compile"
            depends="init"
            description="Compile the application.">

        <javac srcdir="${dir.java}"
               destdir="${dir.classes}"
               debug="true"
               source="1.5"
               target="1.5">
            <classpath refid="classpath.mule"/>
        </javac>

        <!-- Copy all resources to the output folder as well -->
        <copy todir="${dir.classes}">
            <fileset dir="${dir.resources}"/>
        </copy>
    </target>

   <target name="package"
           depends="compile"
           description="Package the application">

        <war destfile="${dir.build}/${app.name}.war" webxml="${dir.webapp}/WEB-INF/web.xml">
            <lib dir="${dir.mule.home}/lib/user">
                <include name="**/*.jar"/>
            </lib>
            <lib dir="${dir.mule.home}/lib/mule">
                <include name="**/*.jar"/>
            </lib>
            <lib dir="${dir.mule.home}/lib/opt">
                <include name="**/*.jar"/>
                <exclude name="**/com.springsource.javax.servlet*.jar"/>
                <exclude name="**/servlet-api*.jar"/>
            </lib>
            <lib dir="${dir.mule.home}/lib/endorsed">
                <include name="xercesImpl-*"/>
            </lib>
            <!-- This libs decl is needed for building the example on Mule EE -->
            <lib dir="${dir.mule.home}/lib/boot">
                <include name="*slf4j*"/>
                <include name="log4j-*.jar"/>
                <include name="commons-codec*"/>
            </lib>
            <fileset dir="${dir.webapp}"/>
            <classes dir="${dir.classes}"/>
            <classes dir="${dir.conf}"/>
        </war>
   </target>
</project>
