<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<project name="Deployer" default="compile" basedir=".">

  <property file="deployer.properties"/>

  <!-- Configure the directory into which the web application is built -->
  <property name="build"    value="${basedir}/build"/>

  <!-- Configure the folder and context path for this application -->
  <property name="webapp"   value="myapp"/>
  <property name="path"     value="/myapp"/>

  <!-- Configure properties to access the Manager application -->
  <property name="url"      value="http://localhost:8080/manager/text"/>
  <property name="username" value="tomcat"/>
  <property name="password" value="tomcat"/>

  <property name="webapp.path"     value="${build}/webapp${path}"/>

  <path id="deployer.classpath">
    <fileset dir="${basedir}/lib">
      <include name="*.jar"/>
    </fileset>
  </path>

  <!-- Configure the custom Ant tasks for the Manager application -->
  <taskdef resource="org/apache/catalina/ant/catalina.tasks"
           classpathref="deployer.classpath"/>

  <!-- Executable Targets -->
  <target name="clean" description="Removes build directory">
    <delete dir="${build}" />
  </target>

  <target name="compile" description="Compile web application"
          depends="clean">

    <copy todir="${webapp.path}">
      <fileset dir="${webapp}" />
    </copy>

    <jasper validateXml="false"
             uriroot="${webapp.path}"
             webXmlFragment="${webapp.path}/WEB-INF/generated_web.xml"
             addWebXmlMappings="true"
             outputDir="${webapp.path}/WEB-INF/classes" />

    <validator path="${webapp.path}" />

    <mkdir dir="${webapp.path}/WEB-INF/classes"/>
    <mkdir dir="${webapp.path}/WEB-INF/lib"/>

    <javac destdir="${webapp.path}/WEB-INF/classes"
           optimize="off"
           debug="${compile.debug}"
           deprecation="${compile.deprecation}"
           failonerror="false"
           srcdir="${webapp.path}/WEB-INF/classes"
           encoding="UTF-8"
           excludes="**/*.smap">
      <classpath>
        <fileset dir="${webapp.path}/WEB-INF/lib">
          <include name="*.jar"/>
        </fileset>
        <fileset dir="${basedir}/lib">
          <include name="*.jar"/>
        </fileset>
      </classpath>
      <include name="**" />
      <exclude name="tags/**" />
    </javac>

    <jar destfile="${webapp.path}.war"
         basedir="${webapp.path}" />

  </target>

  <target name="deploy" description="Deploy web application">
    <deploy url="${url}" username="${username}" password="${password}"
            path="${path}" war="${webapp.path}.war" update="true" />
  </target>

  <target name="undeploy" description="Undeploy web application">
    <undeploy url="${url}" username="${username}" password="${password}"
              path="${path}"/>
  </target>

  <!-- Webapp lifecycle control -->
  <target name="start" description="Start web application">
    <start url="${url}" username="${username}" password="${password}"
           path="${path}"/>
  </target>
  <target name="reload" description="Reload web application">
    <reload url="${url}" username="${username}" password="${password}"
            path="${path}"/>
  </target>
  <target name="stop" description="Stop web application">
    <stop url="${url}" username="${username}" password="${password}"
          path="${path}"/>
  </target>

</project>
