<?xml version="1.0"?>
<!-- 
/*
 * Copyright 2001-2004 The Apache Software Foundation.
 * 
 * Licensed 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 
  xmlns:j="jelly:core"
  xmlns:ant="jelly:ant"
  xmlns:license="license"
  xmlns:maven="jelly:maven"
  xmlns:deploy="deploy"
  xmlns:util="jelly:util"
  xmlns:doc="doc">

  <!-- ================================================================== -->
  <!-- J A R                                                              -->
  <!-- ================================================================== -->
  <goal name="jar" prereqs="jar:jar" 
    description="Create the deliverable jar file."/>
    
  <goal name="jar:jar"
        description="Create the deliverable jar file."
        prereqs="test:test">

    <j:choose>
      <j:when test="${pom.currentVersion == null}">
        <ant:fail>You must define currentVersion in your POM.</ant:fail>
      </j:when>
    </j:choose>
    
    <ant:available property="maven.jar.manifest.available" 
      file="${maven.jar.manifest}"/>

    <!-- See http://java.sun.com/j2se/1.3/docs/guide/jar/jar.html -->
    <!-- See http://java.sun.com/j2se/1.4.1/docs/guide/jar/jar.html -->
    <ant:jar
      jarfile="${maven.build.dir}/${maven.final.name}.jar"
      basedir="${maven.build.dest}"
      index="${maven.jar.index}"
      compress="${maven.jar.compress}"
      excludes="${maven.jar.excludes}"> 
      
      <j:if test="${maven.jar.manifest.available}">
        <ant:setProperty name="manifest" value="${maven.jar.manifest}" />
      </j:if>

      <j:if test="${context.getVariable('maven.jar.includes') != null}">
        <ant:setProperty name="includes" value="${maven.jar.includes}" />
      </j:if>

      <j:set var="licenseFileName"><license:fileName/></j:set>
      <util:file name="${licenseFileName}" var="licenseFile"/>
      <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:attribute name="Created-By" value="Apache Jakarta Maven"/>
        <ant:attribute name="Package" value="${pom.package}"/>
        <ant:attribute name="Build-Jdk" value="${java.version}"/>

        <j:set var="jarClasspath" value="${maven.jar.manifest.classpath.add}"/>
        <j:if test="${jarClasspath}">
            <j:forEach var="dep" items="${pom.dependencies}">
                <j:if test="${dep.getProperty('jar.manifest.classpath')=='true'}">
                    <j:set var="maven.jar.classpath" value="${maven.jar.classpath} ${dep.artifact}" /> 
                </j:if>
            </j:forEach>
            <j:if test="${maven.jar.classpath.length() != 0}">
                <ant:attribute name="Class-Path" value="${maven.jar.classpath}"/>
            </j:if>
        </j:if>
        <!-- added supplementary entries -->
        <ant:attribute name="Extension-Name" value="${pom.artifactId}"/>
        <ant:attribute name="Specification-Title" value="${pom.shortDescription}"/>
        <ant:attribute name="Specification-Vendor" value="${pom.organization.name}"/>
        <ant:attribute name="Specification-Version" value="${pom.currentVersion}"/>
        <ant:attribute name="Implementation-Title" value="${pom.package}"/>
        <ant:attribute name="Implementation-Vendor" value="${pom.organization.name}"/>
        <ant:attribute name="Implementation-Version" value="${pom.currentVersion}"/>
        <j:set var="mainclass" value="${maven.jar.mainclass}" />
        <j:if test="${!empty(mainclass)}">
            <ant:attribute name="Main-Class" value="${mainclass}"/>
        </j:if>
        
        <j:set var="jarExtensions" value="${maven.jar.manifest.extensions.add}"/>
        <j:if test="${jarExtensions == 'true'}">
          <j:set var="extensionList" value=""/>
          <j:forEach var="artifact" items="${pom.artifacts}">
            <j:set var="dep" value="${artifact.dependency}"/>
            <j:set var="extensionList" value="${extensionList} ${dep.artifactId}"/>
          </j:forEach>

		  <j:if test="${extensionList.length() != 0}">        
            <j:set var="extensionList" value="${extensionList.substring(1)}"/>
            <ant:attribute name="Extension-List" value="${extensionList}"/>
          </j:if>

          <j:forEach var="artifact" items="${pom.artifacts}">
            <j:set var="dep" value="${artifact.dependency}"/>
	        <ant:attribute name="${dep.artifactId}-Extension-Name" value="${dep.artifactId}"/>
            <ant:attribute name="${dep.artifactId}-Implementation-Version" value="${dep.version}"/>
            <ant:attribute name="${dep.artifactId}-Implementation-URL" value="http://www.ibiblio.org/maven${artifact.urlPath}"/>
          </j:forEach>        
        </j:if>

        <j:if test="${context.getVariable('maven.jar.manifest.attributes.list') != null}">
            <util:tokenize var="attributeList" delim="," trim="true">${maven.jar.manifest.attributes.list}</util:tokenize>
            <j:forEach var="attribute" items="${attributeList}">
                <j:set var="name" value="maven.jar.manifest.attribute.${attribute}"/>
                <j:set var="value" value="${context.findVariable(name)}"/>
                <ant:attribute name="${attribute}" value="${value}"/>
            </j:forEach>
        </j:if>

        <j:if test="${context.getVariable('maven.jar.manifest.groups.list') != null}">
            <util:tokenize var="groupList" delim="," trim="true">${maven.jar.manifest.groups.list}</util:tokenize>
            <j:forEach var="group" items="${groupList}">
                <j:set var="nameVar" value="maven.jar.manifest.${group}.name"/>
                <j:set var="groupName" value="${context.findVariable(nameVar)}"/>
                <j:set var="attributeListVar" value="maven.jar.manifest.${group}.attributes.list"/>
                <j:set var="groupAttributes" value="${context.findVariable(attributeListVar)}"/>
                <util:tokenize var="attributeList" delim="," trim="true">${groupAttributes}</util:tokenize>
                <ant:section name="${groupName}">
                    <j:forEach var="attribute" items="${attributeList}">
                        <j:set var="name" value="maven.jar.manifest.${group}.attribute.${attribute}"/>
                        <j:set var="value" value="${context.findVariable(name)}"/>
                        <ant:attribute name="${attribute}" value="${value}"/>
                    </j:forEach>
                </ant:section>
            </j:forEach>
         </j:if>
      </ant:manifest>
    </ant:jar>
  </goal>

  <!-- ================================================================== -->
  <!-- S N A P S H O T  J A R                                             -->
  <!-- ================================================================== -->

  <goal
    name="jar:snapshot"
    description="Create a snapshot jar, ie 'id-YYYYMMDD.hhmmss.jar'">

    <maven:snapshot project="${pom}"/>    

    <j:set var="oldMavenFinalName" value="${maven.final.name}" />
    <j:set var="maven.final.name" value="${snapshotSignature}"/>
    <ant:echo>Building snapshot JAR: ${maven.final.name}</ant:echo>
    
    <attainGoal name="jar:jar"/>
    
  </goal>

  <!-- ================================================================== -->
  <!-- D E P L O Y  S N A P S H O T                                       -->
  <!-- ================================================================== -->

  <goal
    name="jar:deploy-snapshot"
    description="Deploy a snapshot jar to the remote repository">

    <maven:user-check user="${maven.username}"/>
    <attainGoal name="jar:snapshot"/>
    <ant:property name="maven.jar.to.deploy" value="${maven.final.name}.jar"/>
    <ant:property name="dir" 
      value="${maven.repo.central.directory}/${pom.artifactDirectory}/jars"/>
    
    <util:replace var="jarToDeploy" oldChar="\" newChar="/">${maven.build.dir}/${maven.jar.to.deploy}</util:replace>
    <util:replace var="forwardSlashBaseDir" oldChar="\" newChar="/" value="${basedir}"/>
    <j:if test="${!forwardSlashBaseDir.endsWith('/')}">
      <j:set var="base" value="${forwardSlashBaseDir}/" />
    </j:if>
    <j:set var="relativePath">${jarToDeploy.substring(base.length())}</j:set>
    
    <deploy:artifact
      artifact="${relativePath}"
      type="jars"
      assureDirectoryCommand="mkdir -p"
      siteCommand="cd @deployDirectory@; chmod g+w ${maven.jar.to.deploy}; chgrp ${maven.remote.group} ${maven.jar.to.deploy}; ln -sf ${maven.jar.to.deploy} ${pom.artifactId}-SNAPSHOT.jar; echo ${snapshotVersion} > ${pom.artifactId}-snapshot-version"
    />
    <j:set var="maven.final.name" value="${oldMavenFinalName}" />
                    
  </goal>

  <!-- ================================================================== -->
  <!-- D E P L O Y  J A R                                                 -->
  <!-- ================================================================== -->

  <goal
    name="jar:deploy"
    description="Deploy a jar to the remote repository">

    <maven:user-check user="${maven.username}"/>
    <attainGoal name="jar:jar"/>
    <ant:property name="maven.jar.to.deploy" value="${maven.final.name}.jar"/>
    
    <j:set var="sl" value="/"/>
    <util:replace var="jarToDeploy" oldChar="\" newChar="/">${maven.build.dir}/${maven.jar.to.deploy}</util:replace>
    <util:replace var="forwardSlashBaseDir" oldChar="\" newChar="/" value="${basedir}"/>
    <j:if test="${!forwardSlashBaseDir.endsWith('/')}">
      <j:set var="base" value="${forwardSlashBaseDir}/" />
    </j:if>
    <j:set var="relativePath">${jarToDeploy.substring(base.length())}</j:set>

    <deploy:artifact
      artifact="${relativePath}"
      type="jars"
      assureDirectoryCommand="mkdir -p"
      siteCommand="cd @deployDirectory@; chmod g+w ${maven.jar.to.deploy}; chgrp ${maven.remote.group} ${maven.jar.to.deploy}"
    />

  </goal>

  <!-- ================================================================== -->
  <!-- I N S T A L L   J A R                                              -->
  <!-- ================================================================== -->

  <goal name="jar:install" prereqs="jar:jar"
    description="Install the jar in the local repository">
    
    <ant:echo>
    id = '${pom.artifactId}'
    groupId = '${pom.groupId}'
    artifactDirectory = '${pom.artifactDirectory}'
    </ant:echo>
    
    <ant:property name="jardir__" value="${maven.repo.local}/${pom.artifactDirectory}/jars"/>
    <ant:mkdir dir="${jardir__}"/>    
    <ant:copy 
      file="${maven.build.dir}/${maven.final.name}.jar"
      todir="${jardir__}"
      overwrite="true"
    />
  </goal>

  <!-- ================================================================== -->
  <!-- I N S T A L L  S N A P S H O T                                       -->
  <!-- ================================================================== -->

  <goal
    name="jar:install-snapshot" prereqs="jar:snapshot"
    description="Install a snapshot jar in the local repository">

    <ant:property name="jardir__" value="${maven.repo.local}/${pom.artifactDirectory}/jars"/>
    <ant:mkdir dir="${jardir__}"/>
    <ant:copy
      file="${maven.build.dir}/${maven.final.name}.jar"
      tofile="${jardir__}/${pom.artifactId}-SNAPSHOT.jar"
      overwrite="true"
    />
    <ant:copy
      file="${maven.build.dir}/${maven.final.name}.jar"
      tofile="${jardir__}/${maven.final.name}.jar"
    />

    <j:set var="maven.final.name" value="${oldMavenFinalName}" />
  </goal>
</project>
