<?xml version="1.0" encoding="UTF-8"?>

<project name="cobertura" default="coverage" basedir="." xmlns:cobertura="antlib:net.sourceforge.cobertura.ant">

	<description>
    Cobertura - http://cobertura.sourceforge.net/
    The copyright for this program is retained by its contributors
    See the included COPYRIGHT file for a complete list of contributors
    Cobertura is licensed under the GNU General Public License
    Cobertura comes with ABSOLUTELY NO WARRANTY
	</description>

	<property file="build.properties" />
	
	<!-- Import the Cobertura Ant Library -->
	<property name="cobertura.local.library" location="antLibrary" />
	<import file="${cobertura.local.library}/library.xml" />


	<path id="base.path">
		<fileset dir="${lib.dir}">
			<include name="**/*.jar" />
		</fileset>
		<fileset dir="${jetty.dir}">
			<include name="lib/servlet-api*.jar" />
		</fileset>
	</path>

	<path id="cobertura.classpath">
		<pathelement location="cobertura.jar" />
		<fileset dir="${lib.dir}">
			<include name="**/*.jar" />
		</fileset>
	</path>

	<macrodef name="cobertura-taskdef">
		<sequential>
			<taskdef classpathref="cobertura.classpath" resource="tasks.properties" />

			<!--
				This is probably a bug in Eclipse, but the Eclipse ant
				editor seems to lock the classpath of the last taskdef,
				so we do a taskdef of one of the core ant tasks so that
				cobertura.jar does not become locked.
			-->
			<taskdef name="not-used" classname="org.apache.tools.ant.taskdefs.Delete">
				<classpath>
					<pathelement path="${java.class.path}" />
				</classpath>
			</taskdef>
		</sequential>
	</macrodef>

	<target name="init">
		<!-- Insert our version number in a file. -->
		<filter token="version" value="${version}" />
		<copy file="${etc.dir}/plugin.xml" todir="${build.dir}" filtering="true" overwrite="true" />
	</target>

	<target name="compile" depends="init">
		<mkdir dir="${build.classes.dir}" />
		<javac fork="true" srcdir="${src.dir}" destdir="${build.classes.dir}" debug="true" encoding="UTF-8"
			target="${javac.target}">
			<compilerarg value="-Xlint:unchecked" compiler="javac1.5"/>
			<classpath location="${build.classes.dir}" />
			<classpath refid="base.path" />
		</javac>

		<mkdir dir="${build.otherclasses.dir}" />
		<javac fork="true" srcdir="${othersrc.dir}" destdir="${build.otherclasses.dir}" debug="true" encoding="UTF-8"
			target="${javac.target}">
			<compilerarg value="-Xlint:unchecked" compiler="javac1.5"/>
			<classpath location="${build.classes.dir}" />
			<classpath refid="base.path" />
		</javac>

		<mkdir dir="${build.functionalconditiontest.dir}" />
		<javac fork="true" srcdir="${examples.functionalconditiontest.dir}" destdir="${build.functionalconditiontest.dir}" debug="true" encoding="UTF-8"
			target="${javac.target}">
			<compilerarg value="-Xlint:unchecked" compiler="javac1.5"/>
			<classpath location="${build.classes.dir}" />
			<classpath refid="base.path" />
		</javac>

		<!-- Copy jar resources (css, images, log4j property file, etc.) -->
		<copy todir="${build.classes.dir}">
			<fileset dir="${src.dir}">
				<exclude name="**/*.java" />
			</fileset>
		</copy>
	</target>

	<target name="compile-tests" depends="init,compile,cobertura-groovy-init">
		<mkdir dir="${build.test.classes.dir}" />
		<cobertura:groovyc srcdir="${test.src.dir}" destdir="${build.test.classes.dir}" encoding="UTF-8">
			<classpath location="${build.classes.dir}" />
			<classpath location="${build.test.classes.dir}" />
			<classpath refid="base.path" />
			<classpath location="${build.otherclasses.dir}" />
			<classpath location="${build.functionalconditiontest.dir}" />
			<classpath path="${java.class.path}"/>
			<javac debug="true" encoding="UTF-8" >
				<compilerarg value="-Xlint:unchecked" compiler="javac1.5"/>
			</javac>
		</cobertura:groovyc>

		<copy todir="${build.test.classes.dir}">
			<fileset dir="${test.src.dir}">
				<exclude name="**/*.java" />
			</fileset>
		</copy>
	</target>

	<target name="jar" depends="init,compile">
		<mkdir dir="${build.dist.dir}" />

		<!-- the standard jar for Cobertura -->
		<jar jarfile="${basedir}/${ant.project.name}.jar" compress="true">
			<fileset dir="${build.classes.dir}">
				<include name="**/*.class" />
			</fileset>
			<fileset dir="${etc.dir}">
				<include name="log4j.properties" />
				<include name="tasks.properties" />
			</fileset>
			<fileset dir="${src.dir}">
				<exclude name="**/*.java" />
				<exclude name="**/package.html" />
			</fileset>
			<fileset dir="${basedir}">
				<include name="COPYING" />
				<include name="COPYRIGHT" />
				<include name="README" />
			</fileset>
			<manifest>
				<attribute name="Specification-Title" value="${description}"/>
				<attribute name="Specification-Vendor" value="Cobertura"/>
				<attribute name="Specification-Version" value="${version}"/>
				<attribute name="Implementation-Title" value="${description}"/>
				<attribute name="Implementation-Vendor" value="Cobertura"/>
				<attribute name="Implementation-Vendor-Id" value="net.sourceforge.cobertura"/>
				<attribute name="Implementation-Version" value="${version}"/>
			</manifest>
		</jar>

		<!--
			Eclipse needs the ANT tasks and the runtime of Cobertura to be split into
			separate jars, otherwise lots of bad things happen in eclipse to do with
			class loading.
		-->
		<mkdir dir="${build.dist.eclipse.dir}" />
		<jar jarfile="${build.dist.eclipse.dir}/${ant.project.name}.jar">
			<fileset dir="${build.classes.dir}">
				<include name="net/sourceforge/cobertura/ant/**/*.class" />
			</fileset>
			<fileset dir="${etc.dir}">
				<include name="tasks.properties" />
			</fileset>
			<manifest>
				<attribute name="Specification-Title" value="${description}"/>
				<attribute name="Specification-Vendor" value="Cobertura"/>
				<attribute name="Specification-Version" value="${version}"/>
				<attribute name="Implementation-Title" value="${description}"/>
				<attribute name="Implementation-Vendor" value="Cobertura"/>
				<attribute name="Implementation-Vendor-Id" value="net.sourceforge.cobertura"/>
				<attribute name="Implementation-Version" value="${version}"/>
			</manifest>
		</jar>
		<jar jarfile="${build.dist.eclipse.dir}/${ant.project.name}-main.jar">
			<fileset dir="${build.classes.dir}">
				<exclude name="net/sourceforge/cobertura/ant/**/*.class" />
			</fileset>
			<fileset dir="${etc.dir}">
				<include name="log4j.properties" />
				<include name="tasks.properties" />
			</fileset>
			<manifest>
				<attribute name="Specification-Title" value="${description}"/>
				<attribute name="Specification-Vendor" value="Cobertura"/>
				<attribute name="Specification-Version" value="${version}"/>
				<attribute name="Implementation-Title" value="${description}"/>
				<attribute name="Implementation-Vendor" value="Cobertura"/>
				<attribute name="Implementation-Vendor-Id" value="net.sourceforge.cobertura"/>
				<attribute name="Implementation-Version" value="${version}"/>
			</manifest>
		</jar>
	</target>
	
	<target name="war" depends="cobertura-groovy-init">
		<cobertura:groovy>
			//load buildUtil definitions
			evaluate(new File("buildUtil.groovy"))
			
			buildWar(new File("coberturaFlush.war"))
		</cobertura:groovy>
	</target>

	<target name="instrument" depends="jar">
		<delete file="${basedir}/cobertura.ser" />
		<delete file="${build.dir}/cobertura.ser" /> <!-- TODO: Is this needed? -->

		<cobertura-taskdef />

		<cobertura-instrument datafile="${build.dir}/cobertura.ser" todir="${build.instrumented.dir}">
		    <fileset dir="${build.classes.dir}">
		        <include name="**/*.class" />
		        <exclude name="**/*Test.class" />
				<exclude name="net/sourceforge/cobertura/javancss/**/*.class" />
		    </fileset>
		    <fileset dir="${build.otherclasses.dir}">
		        <include name="**/*.class" />
		        <exclude name="**/*Test.class" />
		    </fileset>
		</cobertura-instrument>
	</target>

	<!--
		This target is used when testing both instrumented and
		non-instrumented code.  The net.sourceforge.cobertura.datafile
		sysproperty is only used if we're testing instrumented code.
	-->
	<target name="test" depends="compile-tests">
		<delete dir="${build.reports.dir}/junit-xml" failonerror="false" />
		<delete dir="${test.work.dir}" failonerror="false" />
		<mkdir dir="${build.reports.dir}/junit-xml" />

		<junit fork="true" forkmode="once" haltonfailure="false" failureProperty="test.failed" printsummary="true" timeout="900000">
			<sysproperty key="net.sourceforge.cobertura.datafile" file="${build.dir}/cobertura.ser" />
			<sysproperty key="basedir" value="${basedir}"/>
			<jvmarg value="-Xmx512M" />
			<!--
			<jvmarg value="-Xdebug" />
			<jvmarg value="-Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=y" />
			-->
			<classpath location="${build.test.classes.dir}" />
			<classpath location="${build.instrumented.dir}" />
			<classpath location="${build.classes.dir}" />
			<classpath location="${build.otherclasses.dir}" />
			<classpath location="${etc.dir}" />
			<classpath refid="base.path" />
			<classpath refid="cobertura.common.local.library" />
			<classpath>
				<fileset dir="${java.home}/../">
					<include name="lib/tools.jar"/>
				</fileset>
			</classpath>
			<formatter type="xml" />
			<formatter type="plain" usefile="false" />
			<test name="${testcase}" todir="${build.reports.dir}/junit-xml" if="testcase" />
			<batchtest todir="${build.reports.dir}/junit-xml" unless="testcase">
				<fileset dir="${build.test.classes.dir}">
					<include name="**/*Test.class" />
					<exclude name="test/first/Test.class" />
					<exclude name="test/condition/Test.class" />
				</fileset>
			</batchtest>
		</junit>

		<delete dir="${build.reports.dir}/junit-html" failonerror="false" />
		<mkdir dir="${build.reports.dir}/junit-html" />

		<junitreport todir="${build.reports.dir}/junit-html">
			<fileset dir="${build.reports.dir}/junit-xml">
				<include name="TEST-*.xml" />
			</fileset>
			<report format="frames" todir="${build.reports.dir}/junit-html" />
		</junitreport>

	</target>

	<target name="coverage-report">
		<cobertura-taskdef />

		<delete dir="${build.reports.dir}/coverage-html" failonerror="false" />
		<mkdir dir="${build.reports.dir}/coverage-html" />
		<cobertura-report datafile="${build.dir}/cobertura.ser"
		                  destdir="${build.reports.dir}/coverage-html"
		                  format="html"
		                  >
			<fileset dir="${src.dir}">
				<include name="**/*.java" />
			</fileset>				  
			<fileset dir="${othersrc.dir}">
				<include name="**/*.java" />
			</fileset>
	    </cobertura-report>

		<delete dir="${build.reports.dir}/coverage-xml" />
		<mkdir dir="${build.reports.dir}/coverage-xml" />
		<cobertura-report datafile="${build.dir}/cobertura.ser"
		                  destdir="${build.reports.dir}/coverage-xml"
		                  format="xml"
					      >
			<fileset dir="${src.dir}">
				<include name="**/*.java" />
			</fileset>				  
			<fileset dir="${othersrc.dir}">
				<include name="**/*.java" />
			</fileset>
		</cobertura-report>

	</target>

	<!--
	<target name="format">
		<property name="dir.jalopy" value="${lib.dir}/jalopy" />
		<taskdef name="jalopy" classname="de.hunsicker.jalopy.plugin.ant.AntPlugin">
			<classpath>
				<fileset dir="${dir.jalopy}">
					<include name="*.jar" />
				</fileset>
			</classpath>
		</taskdef>

		<jalopy fileformat="unix"
			convention="etc/jalopy.xml"
			loglevel="info"
			classpathref="base.path">
			<fileset dir="${src.dir}">
				<include name="**/*.java" />
			</fileset>
			<fileset dir="${test.src.dir}">
				<include name="**/*.java" />
			</fileset>
		</jalopy>
	</target>
	-->

	<target name="javadoc">
		<delete dir="${build.api.dir}" />
		<mkdir dir="${build.api.dir}" />

		<javadoc access="private" author="true" breakiterator="true" destdir="${build.api.dir}" failonerror="true" encoding="UTF-8" linksource="true" serialwarn="false" version="true" windowtitle="Cobertura API">
			<fileset dir="${src.dir}">
				<include name="**/*.java" />
			</fileset>
			<classpath location="${ant.home}/lib/ant.jar" />
			<classpath refid="base.path" />
		</javadoc>
	</target>

	<target name="dist" depends="jar,javadoc">
		<!--
			Copy the files needed for the binary jar to a clean directory
			tree.  The "binary" jar contains the minimum files needed to
			use Cobertura.
		-->
		<copy-unix-scripts todir="${build.dist.dir}/package-bin/cobertura-${version}" />
				
		<copy todir="${build.dist.dir}/package-bin/cobertura-${version}">
			<fileset dir="${basedir}">
				<include name="ChangeLog" />
				<include name="cobertura-*.bat" />
				<include name="COPYING" />
				<include name="COPYRIGHT" />
				<include name="README" />
				<include name="lib/**" />

				<!-- Exclude jars that are only used by the JUnit tests -->
				<exclude name="lib/jaxen*" />
				<exclude name="lib/jdom*" />
				<exclude name="lib/junit*" />
				<exclude name="lib/xerces/**" />
				<exclude name="lib/xalan*" />

				<include name="cobertura.jar" />
				<include name="coberturaFlush.war" />
				<include name="examples/basic/README" />
				<include name="examples/basic/build.properties" />
				<include name="examples/basic/*.xml" />
				<include name="examples/basic/src/**" />
			</fileset>
		</copy>

		<!--
			Copy the files needed for the source jar to a clean directory
			tree.  The "source" jar contains everything needed to build
			Cobertura.
		-->
		<copy-unix-scripts todir="${build.dist.dir}/package-src/cobertura-${version}" />

		<copy todir="${build.dist.dir}/package-src/cobertura-${version}">
			<fileset dir="${basedir}">
				<include name="antLibrary/**" />
				<include name="ChangeLog" />
				<include name="cobertura-*.bat" />
				<include name="COPYING" />
				<include name="COPYRIGHT" />
				<include name="README" />
				<include name="build.properties" />
				<include name="build.xml" />
				<include name="etc/**" />
				<include name="lib/**" />
				<include name="resources/**" />
				<include name="src/**" />
				<include name="src-2/**" />
				<include name="test/**" />

				<include name="examples/*/README" />
				<include name="examples/*/build.properties" />
				<include name="examples/*/*.xml" />
				<include name="examples/*/src/**" />
			</fileset>
		</copy>

		<!-- Create bz2 files -->
		<create-tar basedir="${build.dist.dir}/package-bin" destfile="${ant.project.name}-${version}-bin.tar.bz2" compression="bzip2"/>
		<create-tar basedir="${build.dist.dir}/package-src" destfile="${ant.project.name}-${version}-src.tar.bz2" compression="bzip2"/>

		<!-- Create gz files -->
		<create-tar basedir="${build.dist.dir}/package-bin" destfile="${ant.project.name}-${version}-bin.tar.gz" compression="gzip"/>
		<create-tar basedir="${build.dist.dir}/package-src" destfile="${ant.project.name}-${version}-src.tar.gz" compression="gzip"/>

		<!-- Create zip files -->
		<create-zip basedir="${build.dist.dir}/package-bin" destfile="${ant.project.name}-${version}-bin.zip"/>
		<create-zip basedir="${build.dist.dir}/package-src" destfile="${ant.project.name}-${version}-src.zip"/>

		<!-- Make an Eclipse plugin zip -->
		<!--<zip destfile="${ant.project.name}-${version}-eclipse-plugin.zip" encoding="UTF-8">
			<zipfileset prefix="com.${ant.project.name}_${version}" dir="${basedir}">
				<include name="ChangeLog" />
				<include name="COPYING" />
				<include name="COPYRIGHT" />
				<include name="README" />
				<include name="lib/**" />
			</zipfileset>

			<zipfileset prefix="com.${ant.project.name}_${version}" dir="${build.dist.eclipse.dir}">
				<include name="${ant.project.name}.jar" />
				<include name="${ant.project.name}-main.jar" />
			</zipfileset>

			<zipfileset prefix="com.${ant.project.name}_${version}" dir="${etc.dir}">
				<include name="plugin.properties" />
			</zipfileset>

			<zipfileset prefix="com.${ant.project.name}_${version}" dir="${build.dir}">
				<include name="plugin.xml" />
			</zipfileset>
		</zip>
		-->
	</target>
	
	<macrodef name="copy-unix-scripts">
		<attribute name="todir" />
		<sequential>
			<copy todir="@{todir}">
				<fileset dir="${basedir}">
					<include name="cobertura-*.sh" />
				</fileset>
				<!-- remove windows carriage returns from unix scripts -->
				<filterchain>
					<fixcrlf eof="remove" eol="unix"/>
				</filterchain>
			</copy>
		</sequential>
	</macrodef>
	
	<macrodef name="create-zip">
		<attribute name="basedir" />
		<attribute name="destfile" />
		<sequential>
			<zip destfile="@{destfile}" encoding="UTF-8">
				<zipfileset dir="@{basedir}" filemode="755">
					<include name="*.sh"/>
				</zipfileset>
				<zipfileset dir="@{basedir}">
					<exclude name="*.sh"/>
				</zipfileset>
			</zip>
		</sequential>
	</macrodef>

	<macrodef name="create-tar">
		<attribute name="basedir" />
		<attribute name="destfile" />
		<attribute name="compression" />
		<sequential>
			<tar destfile="@{destfile}" compression="@{compression}">
				<tarfileset dir="@{basedir}" mode="755">
					<include name="*.sh"/>
				</tarfileset>
				<tarfileset dir="@{basedir}">
					<exclude name="*.sh"/>
				</tarfileset>
			</tar>
		</sequential>
	</macrodef>

	<target name="create-bundles" depends="jar,javadoc">
	    <mkdir dir="${build.bundle.dir}" />

		<delete>
			<fileset dir="${build.bundle.dir}">
				<include name="*" />
			</fileset>
		</delete>

	    <mkdir dir="${build.bundle.source.dir}" />
	    <mkdir dir="${build.bundle.runtime.dir}" />

		<!-- the javadoc jar for Cobertura.
		     used by IDE, maven, and global artifact repository. -->
		<jar jarfile="${build.bundle.source.dir}/${ant.project.name}-${version}-javadoc.jar" compress="true">
			<fileset dir="${build.api.dir}">
				<include name="**/*" />
			</fileset>
			<fileset dir="${basedir}">
				<include name="COPYING" />
				<include name="COPYRIGHT" />
				<include name="README" />
			</fileset>
			<manifest>
				<attribute name="Cobertura" value="Javadoc for ${version}"/>
			</manifest>
		</jar>

		<!-- the sources jar for Cobertura.
		     this is not the same as the -src.tar.gz, and is not expected to be
			 a fully compilable source jar.
			 This jar should contain the source for the classes that exist in
			 the cobertura.jar file, and is used from within an IDE to aide
			 in debugging of an application that utilizes that jar file.
		     This is an optional jar file for the global repository. -->
		<jar jarfile="${build.bundle.source.dir}/${ant.project.name}-${version}-sources.jar" compress="true">
			<fileset dir="${src.dir}">
				<include name="**/*" />
			</fileset>
			<fileset dir="${basedir}">
				<include name="COPYING" />
				<include name="COPYRIGHT" />
				<include name="README" />
			</fileset>
			<manifest>
				<attribute name="Cobertura" value="Source for ${version}"/>
			</manifest>
		</jar>

		<!-- copy existing files into expected and versioned names -->
	    <copy file="COPYING" tofile="${build.bundle.source.dir}/LICENSE.txt"
		      overwrite="true" />
		<copy file="${ant.project.name}.jar" 
			  tofile="${build.bundle.source.dir}/${ant.project.name}-${version}.jar"
			  overwrite="true" />

		<!-- Create the bundle for the cobertura lib -->

	    <copy file="${basedir}/maven/ibiblio/cobertura.pom"
		      tofile="${build.bundle.source.dir}/pom.xml"
			  overwrite="true">
			<filterset>
				<filter token="VERSION" value="${version}" />
			</filterset>
		</copy>
		
		<!--
		This bundle jar is only used if the new release is manually uploaded to the
		maven repository.
		-->
		<jar jarfile="${basedir}/${ant.project.name}-${version}-bundle.jar" compress="false">
		    <fileset dir="${build.bundle.source.dir}">
				<include name="LICENSE.txt" />
				<include name="${ant.project.name}-${version}.jar" />
				<include name="${ant.project.name}-${version}-javadoc.jar" />
				<include name="${ant.project.name}-${version}-sources.jar" />
				<include name="pom.xml" />
			</fileset>
		</jar>

		<!-- Create the bundle for the cobertura-runtime -->

	    <copy file="${basedir}/maven/ibiblio/cobertura-runtime.pom"
		      tofile="${build.bundle.runtime.dir}/pom.xml"
			  overwrite="true">
			<filterset>
				<filter token="VERSION" value="${version}" />
			</filterset>
		</copy>

	    <copy file="COPYING" tofile="${build.bundle.runtime.dir}/LICENSE.txt"
		      overwrite="true" />

		<jar jarfile="${basedir}/${ant.project.name}-runtime-${version}-bundle.jar" compress="false">
		    <fileset dir="${build.bundle.runtime.dir}">
				<include name="LICENSE.txt" />
				<include name="pom.xml" />
			</fileset>
		</jar>
		
	</target>
	
	<target name="deploy-to-local-maven-repo-only" depends="cobertura-groovy-init">
		<cobertura:groovy>
			//load buildUtil definitions
			evaluate(new File("buildUtil.groovy"))
			
			deployToLocalMavenRepo()
		</cobertura:groovy>
		
	</target>

	<target name="install-eclipse-plugin">
		<apply dir="${eclipse.dir}/plugins" executable="unzip">
			<arg value="-o" />
			<fileset dir="dist">
				<include name="${ant.project.name}-${version}-eclipse-plugin.zip" />
			</fileset>
		</apply>
	</target>

	<target name="clean" description="Remove all files created by the build process.">
		<delete dir="${build.dir}" />
		<delete>
			<fileset dir="${basedir}">
				<include name="cobertura.jar" />
				<include name="cobertura-*-bundle.jar" />
				<include name="cobertura.ser" />
				<include name="cobertura.ser.lock" />
				<include name="cobertura-*-bin.*" />
				<include name="cobertura-*-src.*" />
				<include name="cobertura-*-eclipse-plugin.zip" />
				<include name="coberturaFlush.war" />
			</fileset>
		</delete>
	</target>
	
	<target name="failure-check">
		<fail message="JUnit tests failed" if="test.failed" />
		
	</target>
	
	<target name="cobertura-check">
		<cobertura-check datafile="${build.dir}/cobertura.ser"
		                 totalbranchrate="70"
		                 totallinerate="80" />

	</target>
	
	<!--
	Used in merging new versions of javancss into Cobertura.   See
	javancss/coberturaREADME.txt for details.
	-->
	<target name="javancss-merge" depends="cobertura-groovy-init">
		<cobertura:groovy>
			//load buildUtil definitions
			evaluate(new File("buildUtil.groovy"))
			
			mergeJavancss()
		</cobertura:groovy>
	</target>

	<target name="coverage" depends="compile,compile-tests,instrument,test,coverage-report,failure-check,cobertura-check" description="Compile, instrument ourself, run the tests and generate JUnit and coverage reports." />
	<target name="release" depends="clean,compile,compile-tests,test,failure-check,jar,war,javadoc,dist,create-bundles,deploy-to-local-maven-repo-only" description="Full build cycle, producing a distribution." />

</project>
<!--
  vim: et sts=4 sw=4 noexpandtab
  -->
