<!-- 
/*
 * 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.
 */
 -->
<j:jelly
  xmlns:ant="jelly:ant"
  xmlns:j="jelly:core" 
  xmlns:u="jelly:util"
  xmlns:x="jelly:xml"
  xmlns:maven="jelly:maven"
  xmlns:test="test"
  xmlns="dummy">

  <ant:tstamp />
  <!-- get first repo in the list -->
  <u:tokenize var="repos" delim=",">${maven.repo.remote}</u:tokenize>
  <j:set var="repo">${repos[0]}</j:set>
  <!-- set up relative paths -->
  <maven:makeRelativePath var="srcDir" basedir="${basedir}" path="${pom.build.sourceDirectory}" separator="/"/>

  <j:if test="${unitTestSourcesPresent == 'true'}">
    <maven:makeRelativePath var="testSrcDir" basedir="${basedir}" path="${pom.build.unitTestSourceDirectory}" separator="/"/>
  </j:if>

  <x:comment>
  build.xml generated by maven from project.xml version ${pom.currentVersion}
  on date ${TODAY}, time ${TSTAMP}
  </x:comment>

<project name="${pom.artifactId}" default="jar" basedir=".">
  
  <property name="defaulttargetdir" value="${maven.build.dir}"/> 
  <property name="libdir" value="${maven.build.dir}/lib" />
  <property name="classesdir" value="${maven.build.dest}" />

  <j:if test="${unitTestSourcesPresent == 'true'}">
    <maven:pluginVar plugin="maven-test-plugin" property="maven.test.dest" var="testclassesdir" />
    <property name="testclassesdir" value="${testclassesdir}" />
    <maven:pluginVar plugin="maven-test-plugin" property="maven.test.reportsDirectory" var="testreportdir" />
    <property name="testreportdir" value="${testreportdir}" />
  </j:if>

  <property name="distdir" value="dist"/>
  <property name="javadocdir" value="dist/docs/api"/>
  <property name="final.name" value="${maven.final.name}"/>
  
  <target 
    name="init"
    description="o Initializes some properties">
    <mkdir dir="$${libdir}"/>
    <condition property="noget">
      <equals arg1="$${build.sysclasspath}" arg2="only"/>
    </condition>
  </target>

  <target
    name="compile"
    depends="get-deps"
    description="o Compile the code">
    
    <mkdir dir="$${classesdir}"/>

    <javac
      destdir="$${classesdir}"
      excludes="**/package.html"
      debug="true"
      deprecation="true"
      optimize="false">
      <src>
        <pathelement location="${srcDir}"/>
      </src>
      <classpath>
        <fileset dir="$${libdir}">
          <include name="*.jar"/>
        </fileset>
      </classpath>
    </javac>
    
    <!-- 
     | Copy any resources that must be present in the deployed
     | JAR file.
    -->
    <j:forEach var="resource" items="${pom.build.resources}">
 
    <copy todir="$${classesdir}">
      
      <maven:makeRelativePath var="dir" basedir="${basedir}" path="${resource.directory}" separator="/"/>

      <j:if test="${empty dir}">
        <j:set var="dir" value="."/>
      </j:if>
      
      <fileset dir="${dir}">
      
      <j:forEach var="res" items="${resource.includes}">
        <include name="${res}"/>
      </j:forEach>
      <j:forEach var="res" items="${resource.excludes}">
        <exclude name="${res}"/>
      </j:forEach>
      </fileset>
    </copy>
    
    </j:forEach>

    <!-- Copy any resources required for unit testing -->
    <j:forEach var="resource" items="${pom.build.unitTest.resources}">
 
    <copy todir="$${testclassesdir}">
      
      <maven:makeRelativePath var="dir" basedir="${basedir}" path="${resource.directory}" separator="/"/>

      <j:if test="${empty dir}">
        <j:set var="dir" value="."/>
      </j:if>
      
      <fileset dir="${dir}">
      
      <j:forEach var="pattern" items="${resource.includes}">
        <include name="${pattern}"/>
      </j:forEach>
      <j:forEach var="pattern" items="${resource.excludes}">
        <exclude name="${pattern}"/>
      </j:forEach>
      </fileset>
    </copy>
 
    </j:forEach>

  </target>
    
  <target
    name="jar"
    depends="compile,test"
    description="o Create the jar">

    <jar
      jarfile="$${defaulttargetdir}/$${final.name}.jar"
      basedir="$${classesdir}"
      excludes="**/package.html"
    />

  </target>
  
  <target
    name="clean"
    description="o Clean up the generated directories">
    <delete dir="$${defaulttargetdir}"/>
    <delete dir="$${distdir}"/>
  </target>

  <target
    name="dist"
    depends="jar, javadoc"
    description="o Create a distribution">
    <mkdir dir="dist"/>
    <copy todir="dist">
      <fileset dir="$${defaulttargetdir}" includes="*.jar"/>        
      <fileset dir="$${basedir}" includes="LICENSE*, README*"/>        
    </copy>
  </target>
 
  <target
    name="test"
    depends="internal-test"
    if="test.failure"
    description="o Run the test cases">
    <fail message="There were test failures."/>
  </target>
  <target
    name="internal-test"
    depends="compile-tests">
    <j:if test="${unitTestSourcesPresent}">
      <mkdir dir="$${testreportdir}"/>
      <junit printSummary="yes"
             haltonerror="true"
             failureproperty="test.failure"
             fork="true"
             dir="./">

        <sysproperty key="basedir" value="."/>
        <formatter type="xml"/>
        <formatter type="plain" usefile="false"/>
        <classpath>
          <fileset dir="$${libdir}">
            <include name="*.jar"/>
          </fileset>
          <pathelement path="$${testclassesdir}"/>
          <pathelement path="$${classesdir}"/>
        </classpath>
        <batchtest todir="$${testreportdir}">
          <fileset dir="${testSrcDir}">
            <j:forEach var="pat" items="${pom.build.unitTest.includes}">
              <include name="${pat}"/>
            </j:forEach>
            <j:forEach var="pat" items="${pom.build.unitTest.excludes}">
              <exclude name="${pat}"/>
            </j:forEach>
            
            <!-- add extra excludes if the property is set -->
            <j:if test="${context.getVariable('maven.ant.excludeTests') != null}">
              <u:tokenize var="patterns" delim=",">${maven.ant.excludeTests}</u:tokenize>
              <j:forEach var="pattern" items="${patterns}">
                <exclude name="${pattern}" />
              </j:forEach>
            </j:if>
          </fileset>
        </batchtest>
      </junit>
    </j:if>
  </target>

  <target 
    name="compile-tests"
    depends="compile">
    <j:if test="${unitTestSourcesPresent}">
      <mkdir dir="$${testclassesdir}"/>
      <javac
        destdir="$${testclassesdir}"
        excludes="**/package.html"
        debug="true"
        deprecation="true"
        optimize="false">
        <src>
          <pathelement location="${testSrcDir}"/>
        </src>
        <classpath>
          <fileset dir="$${libdir}">
            <include name="*.jar"/>
          </fileset>
          <pathelement path="$${classesdir}"/>
        </classpath>
      </javac>

      <j:if test="${!pom.build.unitTest.resources.isEmpty()}">
        <j:forEach var="resource" items="${pom.build.unitTest.resources}">
          <j:choose trim="true">
            <j:when test="${!resource.includes.isEmpty()}">
               <j:set var="maven.has.test.resource.patterns" value="true"/>
             </j:when>
             <j:when test="${!resource.excludes.isEmpty()}">
               <j:set var="maven.has.test.resource.patterns" value="true"/>
             </j:when>
          </j:choose>      
        </j:forEach>
      </j:if>
   
      <j:if test="${maven.has.test.resource.patterns}">
      <copy todir="$${testclassesdir}">
        <fileset dir="${pom.build.unitTestSourceDirectory}">
          <j:forEach var="resource" items="${pom.build.unitTest.resources}">     
            <j:forEach var="res" items="${resource.includes}">
              <include name="${res}"/>
            </j:forEach>
            <j:forEach var="res" items="${resource.excludes}">
              <exclude name="${res}"/>
            </j:forEach>
          </j:forEach>
        </fileset>
      </copy>
      </j:if>
    </j:if>
  </target>

  <target
    name="javadoc"
    description="o Generate javadoc">
 
 
    <mkdir dir="$${javadocdir}"/>
 
    <!-- Get the year to display in the Javadocs -->
    <tstamp>
      <format property="year" pattern="${pom.inceptionYear}-yyyy"/>
    </tstamp>
 
    <property
      name="copyright"      
      value="Copyright &amp;copy; ${year} ${pom.organization.name}. All Rights Reserved."
    />
 
    <property
      name="title"
      value="${pom.name} ${pom.currentVersion} API"
    />
 
    <javadoc
      sourcepath="${srcDir}"
      packagenames="${pom.package}.*"
      destdir="$${javadocdir}"
      author="true"
      private="true"
      version="true"
      use="true"
      windowtitle="${title}"
      doctitle="${title}"
      bottom="${copyright}">
      <classpath>
        <fileset dir="$${libdir}">
          <include name="*.jar"/>
        </fileset>
        <pathelement location="$${defaulttargetdir}/$${final.name}.jar"/>
      </classpath>
    </javadoc>
 
  </target>

  <target
    name="get-deps"
    depends="init"
    unless="noget">
  
    <j:forEach var="dep" items="${pom.dependencies}">
    <!-- note: this is a valid use of artifactDirectory -->
    <get 
      src="${repo}/${dep.artifactDirectory}/${dep.type}s/${dep.artifact}"
      dest="$${libdir}/${dep.artifact}"
      usetimestamp="true"
      ignoreerrors="true"
    /></j:forEach>
    <!-- force junit for tests -->
    <get 
      src="${repo}/junit/jars/junit-3.8.1.jar"
      dest="$${libdir}/junit-3.8.1.jar"
      usetimestamp="true"
      ignoreerrors="true"/>
    <get 
      src="${repo}/ant/jars/ant-1.5.jar"
      dest="$${libdir}/ant-1.5.jar"
      usetimestamp="true"
      ignoreerrors="true"/>
    <get 
      src="${repo}/ant/jars/ant-optional-1.5.jar"
      dest="$${libdir}/ant-optional-1.5.jar"
      usetimestamp="true"
      ignoreerrors="true"/>
  </target>

  <!-- ================================================================== -->
  <!-- A N T  A U T O  I N S T A L L E R                                  -->
  <!-- ================================================================== -->
  <!-- For users that have Ant installed Maven can be automatically       -->
  <!-- installed by answering a few simple questions. We only need to     -->
  <!-- know what the user wants for ${maven.home} and ${maven.repo.local} -->
  <!-- and we're set.                                                     -->
  <!-- ================================================================== -->

  <target
    name="install-maven">

    <!--
    
    Ask the user what they would like to use for
    
    ${maven.home}
    ${maven.repo.local}
    
    -->

    <get 
      src="$${repo}/maven/maven-install-latest.jar"
      dest="$${user.home}/maven-install-latest.jar"
      usetimestamp="true"
    />
    
    <unjar
      src="$${user.home}/maven-install-latest.jar"
      dest="$${maven.home}"
    />
    
  </target>

</project>
</j:jelly>
