<?xml version="1.0"?>
<!--
   $Id: build.xml,v 1.24 2010/01/11 23:17:24 karsten Exp $

   This is a build file for use with the Jakarta Ant build tool, see
   http://jakarta.apache.org/ant/index.html

   To build, go to the directory where this file is located and run
     ant <target>
   with one of the following targets:
   
     clean               removes temporary files and directories
     compile             compiles the framework, extras, tutorial and tests
     javadoc             creates the API documentation
     test                compiles and runs the unit tests
     jar                 creates a library jar
     create-dist-dir     creates a distribution tree: docs, sources, jar
     create-dist-zip     creates a zipped distribution
     create-maven-bundle creates a Maven bundle
     create-all          creates the distribution zip and Maven bundle
     
   To compile, you MUST set the property "build.boot.classpath",
   for example in your user build.properties file a line like:
   build.boot.classpath=C:/Java/Jdk1.5.0_11/jre/lib/rt.jar
   
   To run the unit tests you MUST set the junit.jar property,
   for example in your user build.properties file. 

-->

<project default="create-all" basedir="." name="JGoodies Binding" >

    <!-- ***************************************************************** -->
    <!-- Give users a chance to override build properties.                 -->
    <!-- ***************************************************************** -->
    <property file="${user.home}/binding.build.properties" />
    <property file="${user.home}/build.properties" />
    <property file="${basedir}/build.properties" />
    <property file="${basedir}/default.properties" />
 
 
    <!-- ***************************************************************** -->
    <!-- P A T H S                                                         -->
    <!-- ***************************************************************** -->
    <path id="classpath.core">
        <pathelement location="${build.core.dir}" 	/> 
        <pathelement location="${lib.common.jar}"   /> 
    </path>
    
    <path id="classpath.extras">
        <pathelement location="${build.core.dir}" 	/> 
        <pathelement location="${build.extras.dir}" /> 
        <pathelement location="${lib.common.jar}"   /> 
    </path>
    
    <path id="classpath.tests">
        <pathelement location="${build.core.dir}" 	/> 
        <pathelement location="${build.extras.dir}" /> 
        <pathelement location="${build.test.dir}" 	/> 
        <pathelement location="${lib.common.jar}"   /> 
        <pathelement location="${junit4.jar}" 		/> 
        <pathelement location="${hamcrest.jar}"     /> 
    </path>

    <path id="classpath.javadoc">
        <pathelement location="${lib.common.jar}"   /> 
    </path>

  
    <!-- ***************************************************************** -->
    <!-- C L E A N                                                         -->
    <!-- ***************************************************************** -->
	<target name="clean"  
		    description="Removes all temporary files and directories." >

		<delete dir="${build.dir}" />
		<delete dir="${dist.dir}"  />
	</target>
	

    <!-- ***************************************************************** -->
    <!-- P R E P A R A T I O N                                             -->
    <!-- ***************************************************************** -->
	<target name="prepare" 
	        description="Prepares the build and distribution targets." >

        <tstamp>
           <format property="DATE" pattern="yyyy-MM-dd hh:mm:ss" />
        </tstamp>
        
		<mkdir dir="${build.core.dir}"		/>
		<mkdir dir="${build.extras.dir}"	/>

        <available
            property="junit.task.present"
            classname="org.apache.tools.ant.taskdefs.optional.junit.JUnitTask"
        />
        <available
            property="junit.jar.present"
            file="${junit4.jar}"
        />
	</target>
	
	
    <!-- ***************************************************************** -->
    <!-- C O M P I L A T I O N                                             -->
    <!-- ***************************************************************** -->
	<target name="compile" 
	        depends="compile-core, compile-extras, compile-tests" 
	        description="Compiles the framework, extras, tests and tutorial." />
	        
    <!-- ***************************************************************** -->
	<target name="compile-core" depends="prepare"
	        description="Compiles the  sources." >
		<javac 
			srcdir       ="${src.core.dir}" 
			destdir      ="${build.core.dir}"
        	encoding     ="${build.encoding}"
        	executable   ="${build.compile.executable}"
        	fork         ="${build.compile.fork}"
			debug        ="${build.compile.debug}"
			depend       ="${build.compile.depend}"
			deprecation  ="${build.compile.deprecation}"
			nowarn       ="${build.compile.nowarn}" 
			optimize     ="${build.compile.optimize}"
            source       ="${build.compile.source}"
			target       ="${build.compile.target}"
			verbose      ="${build.compile.verbose}"
			bootclasspath="${build.boot.classpath}" 
			classpathref ="classpath.core" />
	</target>
	
    <!-- ***************************************************************** -->
	<target name="compile-extras" depends="compile-core"
	        description="Compiles the extra sources." >
		<javac 
			srcdir       ="${src.extras.dir}" 
			destdir      ="${build.extras.dir}"
        	encoding     ="${build.encoding}"
        	executable   ="${build.compile.executable}"
        	fork         ="${build.compile.fork}"
			debug        ="${build.compile.debug}"
			depend       ="${build.compile.depend}"
			deprecation  ="${build.compile.deprecation}"
			nowarn       ="${build.compile.nowarn}" 
			optimize     ="${build.compile.optimize}"
            source       ="${build.compile.source}"
			target       ="${build.compile.target}"
			verbose      ="${build.compile.verbose}"
			bootclasspath="${build.boot.classpath}"
			classpathref ="classpath.extras" />
	</target>
	
    <!-- ***************************************************************** -->
	<target name="compile-tests" depends="compile-extras" if="junit.jar.present"
	        description="Compiles the unit tests." >
        <mkdir dir="${build.test.dir}"/>
		<javac 
			srcdir       ="${src.test.dir}" 
			destdir      ="${build.test.dir}"
        	encoding     ="${build.encoding}"
        	executable   ="${build.compile.executable}"
        	fork         ="${build.compile.fork}"
			debug        ="${build.compile.debug}"
			depend       ="${build.compile.depend}"
			deprecation  ="${build.compile.deprecation}"
			nowarn       ="${build.compile.nowarn}" 
			optimize     ="${build.compile.optimize}"
            source       ="${build.compile.source}"
			target       ="${build.compile.target}"
			verbose      ="${build.compile.verbose}"
			bootclasspath="${build.boot.classpath}"
			classpathref ="classpath.tests" />
	</target>	
	
	
    <!-- ***************************************************************** -->
    <!-- J A V A D O C                                                     -->
    <!-- ***************************************************************** -->
    <target name="javadoc" depends="prepare" 
            description="Creates the API documentation using JavaDoc." >
		<mkdir dir="${build.javadocs.dir}"	/>
        <javadoc 
        	sourcepath="${src.core.dir}:${src.extras.dir}"
            packagenames="${javadoc.packages}"
        	destdir="${build.javadocs.dir}"
        	encoding="${build.encoding}"
            access="protected" 
            author="true" 
            version="true" 
            use="true" 
            link="${javadoc.link}"
        	overview="${javadoc.overview}"
            windowtitle="${Name} ${spec.version} API" 
            doctitle="${Name} ${spec.version} API"
            bottom="${copyright.message}" 
            classpathref ="classpath.javadoc" >
         </javadoc>
    </target>
    
    
    <!-- ***************************************************************** -->
    <!-- U N I T   T E S T S                                               -->
    <!-- ***************************************************************** -->
	<target name="test" depends="compile-tests" if="junit.task.present"
	        description="Compiles and runs the unit tests." >
        <mkdir dir="${build.reports.dir}"/>
        <junit printsummary="yes" haltonfailure="no" >
            <classpath refid="classpath.tests"/>
            <formatter type="plain" />
            <batchtest fork="yes" todir="${build.reports.dir}">
                <fileset dir="${src.test.dir}" includes="**/*Test.java" />
            </batchtest>
        </junit>
	</target>
		
    
    <!-- ***************************************************************** -->
    <!-- J A R                                       				       -->
    <!-- ***************************************************************** -->
	<target name="jar" depends="compile-core"
	        description="Creates the library jar." >
		<jar 
			destfile="${build.core.jar}" >
			<fileset dir="${build.core.dir}" />
            <manifest>
                <attribute name ="Built-By" 
                           value="${user.name}"/>
                <attribute name ="Specification-Title"
                           value="${spec.title}" />
                <attribute name ="Specification-Vendor"
                           value="${spec.vendor}" />
                <attribute name ="Specification-Version"
                           value="${spec.version}" />
         		<attribute name ="Implementation-Title"
                           value="${impl.title}" />
                <attribute name ="Implementation-Vendor"
                           value="${impl.vendor}" />
         		<attribute name ="Implementation-Version"
                           value="${impl.version} ${DATE}" />
           </manifest>
		</jar>
	</target>
	
	
    <!-- ***************************************************************** -->
    <!-- C R E A T E                                                       -->
    <!-- ***************************************************************** -->
    <target name="create-all" depends="create-dist-zip, create-maven-bundle"
            description="Creates the distribution zip and Maven bundle." >
    </target>   

  
    <target name="create-dist-dir" depends="clean, compile, javadoc, test, jar"
	        description="Creates the distribution directory tree." >
	
		<mkdir dir="${dist.dir}" />
		
		<!-- Compile the documentation. -->
		<copy todir="${dist.docs.dir}" >
			<fileset dir="${docs.dir}"  
			         excludes="**/*.psd, **/*.sxw, whitepaper.pdf"     />
		</copy>
        <move todir="${dist.docs.dir}" file="${build.javadocs.dir}" />

		<!-- Copy the source directories. -->
		<copy todir="${dist.src.dir}" >
		    <fileset dir="${src.dir}" 
                     excludes="unused,
                               unused/**/*"      />
		</copy>
		
		<!-- Copy the required libraries. -->
		<copy todir="${dist.lib.dir}" file="${lib.common.jar}" />

		<!-- Copy the distribution files. -->
		<copy todir="${dist.dir}" filtering="true" >
			<fileset dir="${top.dir}" 
				     includes="*.txt, *.html, *.xml, *.properties"
				     excludes="build.properties, todo.txt" />
			<filterset>
				<filter token="impl.version"      value="${impl.version}"      />
				<filter token="version.name"      value="${version.name}"      />
				<filter token="copyright.message" value="${copyright.message}" />
				<filter token="copyright.short"   value="${copyright.short}"   />
			</filterset>
		</copy>

		<!-- Copy the library jar file. -->
		<copy tofile="${dist.core.jar}" file="${build.core.jar}" />
		
	</target>

    <!-- ***************************************************************** -->
	<target name="create-dist-zip" depends="create-dist-dir"
	        description="Packages the distribution as a zip file." >
	        
        <zip 
        	destfile="${dist.zip}" 
        	basedir="${dist.root.dir}"
        	includes="${dist.subdir}/**/*" />
	</target>
	
    <!-- ***************************************************************** -->
    <target name="create-maven-bundle" depends="create-dist-dir"
            description="Creates a Maven bundle for the Ibiblio upload." >
            
        <!-- Copy the Maven pom template. -->
        <copy 
        	tofile="${dist.maven.pom}" 
        	file="${build.maven.pom.template}" >
			<filterset>
				<filter token="impl.version"      value="${impl.version}"      />
				<filter token="shortname"         value="${shortname}"      />
			</filterset>
    	</copy>
        
        <copy tofile="${dist.maven.bin.jar}" file="${build.core.jar}" />
        <jar 
            destfile="${dist.maven.src.jar}"
            basedir="${dist.src.dir}" />   
        <jar 
            destfile="${dist.maven.bundle}" 
            basedir="${build.maven.dir}" />
    </target>
    
        
</project>