<?xml version="1.0"?>

<project 
  xmlns:j="jelly:core"
  xmlns:velocity="jelly:velocity"
  xmlns:artifact="artifact">

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

  <!--==================================================================-->
  <!-- Initializations                                                  -->
  <!--==================================================================-->    
  <goal name="uberjar:init">

    <property name="maven.uberjar.final.name"  value="${maven.build.dir}/${maven.final.name}-uber.jar"/>
    <property name="dest" value="${maven.build.dir}/uberjar"/>    
    <property name="inf" value="${dest}/WORLDS-INF"/>
    <property name="libdir" value="${inf}/lib"/>
    <property name="confdir" value="${inf}/conf"/>
    <mkdir dir="${libdir}"/>
    <mkdir dir="${confdir}"/>

  </goal>

  <!--==================================================================-->
  <!-- Build the uberjar                                                -->
  <!--==================================================================-->    
  <goal 
    name="uberjar:uberjar"
    prereqs="jar:jar, uberjar:init"
    description="Build an uberjar">

    <j:set var="uberconf" value="${maven.uberjar.classworlds.conf}"/>

    <!-- Create the classworlds conf if user hasn't specified one -->
    <j:choose> 
      <j:when test="${empty(uberconf)}">
        <attainGoal name="uberjar:classworlds-conf"/>
      </j:when>
      <j:otherwise>
        <copy file="${uberconf}" tofile="${confdir}/classworlds.conf"/>
      </j:otherwise>
    </j:choose>

    <!-- Copy the project's complete jar -->
    <copy 
      file="${maven.build.dir}/${maven.final.name}.jar"
      todir="${libdir}"/>

    <!-- Copy each dep to our working dir -->
    <j:forEach var="lib" items="${pom.artifacts}">
      <copy 
        todir="${libdir}"
        file="${lib.path}"/>
    </j:forEach>

    <!-- Copy the classworlds.jar -->
    <copy 
      tofile="${inf}/classworlds.jar"
      file="${plugin.getDependencyPath('classworlds')}"/>

    <!-- Unjar the bootstrap code -->
    <unjar 
      dest="${dest}"
      src="${plugin.getDependencyPath('classworlds')}">
      <patternset>
        <include name="com/werken/classworlds/boot/*.class"/>
        <include name="com/werken/classworlds/protocol/jar/*.class"/>
      </patternset>
    </unjar>

    <!-- Jar it all up -->
    <jar
      jarfile="${maven.uberjar.final.name}"
      basedir="${dest}">
      <manifest>
        <attribute name="Built-By" value="${user.name}"/>
        <attribute name="Created-By" value="Classworlds"/>
        <attribute name="Package" value="${pom.package}"/>
        <attribute name="Main-Class" value="com.werken.classworlds.boot.Bootstrapper"/>
      </manifest>
    </jar>

  </goal>

  <!--==================================================================-->
  <!-- Generate a classworlds conf file                                 -->
  <!--==================================================================-->    
  <goal 
    name="uberjar:classworlds-conf" 
    prereqs="uberjar:init"
    description="Generates a classworlds conf">

    <j:set var="ubermain" value="${maven.uberjar.main}"/>

    <j:if test="${empty(ubermain)}">
      <fail message="">

The maven.uberjar.main property has not been set.  This value should
be set to the value of the class that contains the main method
to be invoked.
      
      </fail>
    </j:if>
    
    <!-- Need to add the ant property 'maven.final.name' to the context
         those are preloaded into the jelly context which is supposed to
         happen soon. -->
    <j:set var="self" value="${maven.final.name}"/>
    <velocity:merge 
      name="${confdir}/classworlds.conf"
      basedir="${plugin.resources}/conf"
      template="classworlds.conf"/>

  </goal>
  

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

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

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

     <artifact:deploy-snapshot
        artifact="${maven.uberjar.final.name}"
        type="uberjar"
        project="${pom}"/>    
        
  </goal>  
  

</project>
