<?xml version="1.0"?>

<project xmlns:j="jelly:core"
         xmlns:license="license"
         xmlns:util="jelly:util"
         xmlns:ant="jelly:ant"
         xmlns:artifact="artifact">

  <!--==================================================================-->
  <!-- Default goal : Builds a war file                                 -->
  <!--==================================================================-->    
  <goal name="war" prereqs="war:war" description="Build a war file"/>

  <!--==================================================================-->
  <!-- Initializations                                                  -->
  <!--==================================================================-->    
  <goal name="war:init"
    description="Initialize the file system and attain any necessary goals">

    <ant:available property="webSourcesPresent" type="dir"
      file="${maven.war.src}"/>

    <j:if test="${sourcesPresent == 'true'}">
      <attainGoal name="java:compile"/>
      <attainGoal name="test:test"/>
    </j:if>

    <ant:property name="maven.war.final.name" 
      value="${pom.artifactId}.war"/>
 
  </goal>
  
  <!--==================================================================-->
  <!-- Builds a war file                                                -->
  <!--==================================================================-->    
  <goal name="war:war" prereqs="war:webapp" description="Build a war file">
    
    <ant:echo>Building WAR ${pom.artifactId}</ant:echo>

    <ant:mkdir dir="${maven.war.build.dir}" />
    <ant:jar 
         destfile="${maven.war.build.dir}/${maven.war.final.name}"
         basedir="${maven.war.webapp.dir}"         
         update="true"
         index="true">
      <ant:metainf dir="${licenseFile.canonicalFile.parent}">
        <ant:include name="${licenseFile.canonicalFile.name}"/>
      </ant:metainf>

      <ant:manifest>
        <ant:attribute name="Built-By" value="${user.name}" />
        <ant:section name="${pom.package}">
          <ant:attribute name="Specification-Title" value="${pom.artifactId}" />
          <ant:attribute name="Specification-Version"
                     value="${pom.currentVersion}" />
          <ant:attribute name="Specification-Vendor"
                     value="${pom.organization.name}" />
          <ant:attribute name="Implementation-Title"
                     value="${pom.package}" />
          <ant:attribute name="Implementation-Version"
                     value="${pom.currentVersion}" />
          <ant:attribute name="Implementation-Vendor"
                     value="${pom.organization.name}" />
        </ant:section>
      </ant:manifest>

    </ant:jar>

  </goal>

  <!--==================================================================-->
  <!-- Builds an expanded webapp                                        -->
  <!--==================================================================-->    
  <goal name="war:webapp" prereqs="war:init" 
    description="Build a webapp directory">

    <ant:echo>Assembling webapp ${pom.artifactId}</ant:echo>

    <ant:mkdir dir="${maven.war.webapp.dir}"/>

    <j:set var="webapp.build.webinf" value="${maven.war.webapp.dir}/WEB-INF"/>
    <ant:mkdir dir="${webapp.build.webinf}"/>

    <j:set var="webapp.build.lib" value="${webapp.build.webinf}/lib"/>
    <ant:mkdir dir="${webapp.build.lib}"/>
    
    <j:set var="webapp.build.tlds" value="${maven.war.webapp.dir}/${maven.war.tld.dir}"/>
    <ant:mkdir dir="${webapp.build.tlds}"/>
    
    <j:set var="webapp.build.classes" value="${webapp.build.webinf}/classes"/>
    <ant:mkdir dir="${webapp.build.classes}"/>

    <j:if test="${webSourcesPresent == 'true'}">
      <ant:copy todir="${maven.war.webapp.dir}">
        <ant:fileset dir="${maven.war.src}"/>
      </ant:copy>
    </j:if>
    
   
    <j:forEach var="lib" items="${pom.artifacts}">
      <j:set var="dep" value="${lib.dependency}"/>     
      <j:if test="${dep.getProperty('war.bundle')=='true'}">
          
         <j:if test="${dep.type =='jar'}"> 
           <ant:copy todir="${webapp.build.lib}" file="${lib.path}"/>  
         </j:if> 
         
         <j:if test="${dep.type =='tld'}"> 
           <ant:copy todir="${webapp.build.tlds}" file="${lib.path}"/>  
         </j:if> 
          
      </j:if>  
      <!-- TO BE REMOVED -->
      <j:if test="${dep.getProperty('war.bundle.jar')=='true'}">
         <ant:echo> 
            DEPRECATION WARNING:            
            Use : war.bundle instead of war.bundle.jar
          </ant:echo>
        <ant:copy todir="${webapp.build.lib}" file="${lib.path}"/>
      </j:if>  
      <!-- END OF TO BE REMOVED -->
    </j:forEach>            

    <j:if test="${sourcesPresent == 'true'}">
      <ant:copy todir="${webapp.build.classes}">
        <ant:fileset dir="${maven.build.dest}"
          includes="${maven.war.classes.includes}"
          excludes="${maven.war.classes.excludes}">
        </ant:fileset>
      </ant:copy>
    </j:if>

  </goal>

  <!--==================================================================-->
  <!-- Install the war in the local repository                          -->
  <!--==================================================================-->    
  <goal name="war:install"
        prereqs="war:war"
        description="Install the war in the local repository">
     
     <artifact:install
        artifact="${maven.war.build.dir}/${maven.war.final.name}"
        type="war"
        project="${pom}"/> 
  
  </goal>
  
  <!--==================================================================-->
  <!-- Install the snapshot version of the war in the local repository  -->
  <!--==================================================================-->      
  <goal name="war:install-snapshot"
        prereqs="war:war" 
        description="Install the snapshot version of the war in the local repository">
        
      <artifact:install-snapshot
        artifact="${maven.war.build.dir}/${maven.war.final.name}"
        type="war"
        project="${pom}"/> 
        
  </goal>

  <!--==================================================================-->
  <!-- Deploys the war to the remote repository                         -->
  <!--==================================================================-->      
  <goal name="war:deploy" 
        prereqs="war:war" 
        description="Deploys the war to the remote repository">

     <artifact:deploy
        artifact="${maven.war.build.dir}/${maven.war.final.name}"
        type="war"
        project="${pom}"/>   
        
  </goal>      
        
  <!--==================================================================-->
  <!-- Deploys the snapshot of the war to the remote repository         -->
  <!--==================================================================-->      
  <goal name="war:deploy-snapshot" 
        prereqs="war:war"
        description="Deploys the snapshot version of  the war to remote repository">  

     <artifact:deploy-snapshot
        artifact="${maven.war.build.dir}/${maven.war.final.name}"
        type="war"
        project="${pom}"/>    
        
  </goal>
  
  
  <!--==================================================================-->
  <!-- Clean                                                            -->
  <!--==================================================================-->  
    <goal name="war:clean"         
        description="Remove all artifacts created by war plugin">  

      <!-- TODO: delete also snapshot and md5 files -->
      <ant:delete dir="${maven.war.webapp.dir}"/>
      <ant:delete file="${maven.war.build.dir}/${maven.war.final.name}" quiet="true" failonerror="false"/>          
    </goal>
    

  <!--==================================================================-->
  <!-- Special no-op goal which can be used by other plugin which need  -->
  <!-- to get access to any of this plugin's property. This is          -->
  <!-- temporary, until we get explicit plugin dependencies.            -->
  <!--==================================================================-->    
  <goal name="war:load"/>

</project>
