<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE project>
<project name="uws" basedir="." default="buildLib">
	
	<property name="version" value="4.4" />

	<property name="srcDir" value="src" />
	<property name="testDir" value="test" />
	<property name="libDir" value="lib" />
	<property name="compileDir" value="antBuild" />
	<property name="junitBuildDir" value="junitBuild" />
	<property name="classesDir" value="${compileDir}"/>
	<property name="javadocDir" value="javadoc/uws" />

	<property name="commonsIOJarName" value="commons-io-2.6.jar" />
	<property name="commonsIOJar" value="${libDir}/${commonsIOJarName}" />
	<property name="commonsFileUploadJarName" value="commons-fileupload-1.3.3.jar" />
	<property name="commonsFileUploadJar" value="${libDir}/${commonsFileUploadJarName}" />

	<property name="slf4jApiJarName" value="slf4j-api-1.7.25.jar" />
	<property name="slf4jApiJar" value="${libDir}/${slf4jApiJarName}" />
	
	<property name="jsonJarName" value="json-20180813.jar" />
	<property name="jsonJar" value="${libDir}/${jsonJarName}" />
	
	<property name="uwsPath" value="uws/**" />
	<property name="jsonPath" value="org/json/**" />
	<property name="licensePath" value="COPYING.LESSER" />
	<property name="extLibsPath" value="com/oreilly/servlet/**" />
	<property name="includesList" value="${uwsPath},${jsonPath},${extLibsPath}" />

	<property name="jarDest" value="." />
	<property name="libJarFile" value="${jarDest}/uws_${version}.jar" />
	<property name="srcJarFile" value="${jarDest}/uws_src_${version}.jar" />
	<property name="javadocJarFile" value="${jarDest}/uws_javadoc_${version}.jar" />
	
	<property name="testsDir" value="test" />
	<property name="junitReportsDir" value="reports/uws" />
	
	<fail message="The property SERVLET-API must be set! It provides the path toward a directory or a JAR which contains all classes inside javax.servlet.">
		<condition><not><isset property="SERVLET-API"/></not></condition>
	</fail>
	
	<!-- JAVA VERSION COMPATIBILITY -->
	<property name="ant.build.javac.source" value="1.7"/>
	<property name="ant.build.javac.target" value="1.7"/>

	<!-- CLASSPATHS -->
	<path id="uws.classpath">
		<pathelement location="${commonsIOJar}" />
		<pathelement location="${commonsFileUploadJar}" />
		<pathelement location="${slf4jApiJar}" />
		<pathelement location="${jsonJar}" />
		<pathelement location="${SERVLET-API}" />
	</path>
	
	<path id="junit.class.path">
		<path refid="uws.classpath" />
		<pathelement path="${JUNIT-API}" />
		<pathelement location="bin" />
	</path>
	
	<echo>UWS LIBRARY VERSION = ${version}</echo>
	
	<!-- BUILD ALL TASK -->
	<target name="buildAll" depends="buildLibAndSrc,buildJavadoc"
			description="Build three JARs: the library (classes), its sources and its Javadoc. Then, except the JAR, all the compiled javadoc files are deleted." >
		<antcall target="cleanJavadocBuild" />
	</target>
	
	<target name="cleanAll" depends="clean,cleanJavadoc,cleanJUnitReports" description="Delete all files generated by this ANT file for the set version." />
	
	<!-- JUNIT VALIDATION -->
	<target name="cleanJUnitReports" description="Clean the JUnit reports directory of the UWS library ONLY.">
			<delete dir="${junitReportsDir}" failonerror="false" />
	</target>
	
	<target name="junitValidation" depends="cleanJUnitReports,compileJUnit" description="Executes all JUnit tests before building the library and stop ANT at any error.">
		<mkdir dir="${junitReportsDir}"/>
		<junit errorproperty="uwsTestsFailure" failureproperty="uwsTestsFailure">
			<classpath refid="junit.class.path" />
			<classpath>
				<pathelement location="${compileDir}" />
				<pathelement location="${junitBuildDir}" />
			</classpath>
			<formatter type="brief" usefile="yes" />
			<batchtest todir="${junitReportsDir}">
				<fileset dir="${testsDir}" includes="uws/**/Test*.java" />
			</batchtest>
		</junit>
		<delete dir="${junitBuildDir}" failonerror="false" />
		<fail if="${uwsTestsFailure}" message="Failed JUnit validation for UWS Lib.!" />
	</target>
	
	<target name="compileJUnit" depends="compileLib" description="Build all the classes to test the UWS library.">
		<mkdir dir="${junitBuildDir}" />
		<javac destdir="${junitBuildDir}" includes="${includesList}" includeantruntime="false" encoding="utf8" bootclasspath="${uws.bootclasspath}" debug="true">
			<src path="${testDir}" />
			<classpath refid="junit.class.path" />
			<classpath refid="uws.classpath" />
			<classpath>
				<pathelement location="${compileDir}" />
			</classpath>
		</javac>
	</target>
			
	<!-- LIB & SOURCES -->
	<target name="clean" description="Delete the JARs for the library (classes) and for its sources for the set version.">
		<delete file="${libJarFile}" failonerror="false" />
		<delete file="${srcJarFile}" failonerror="false" />
		<delete dir="${compileDir}" failonerror="false" />
	</target>
	
	<target name="compileLib" depends="clean" description="Build all the classes of the UWS library. This target is particularly usefull because it lets highlighting missing dependencies.">
		<mkdir dir="${compileDir}" />
		<javac destdir="${compileDir}" srcdir="${srcDir}" includes="${includesList}" includeantruntime="false" encoding="utf8" debug="true">
			<classpath refid="uws.classpath" />
		</javac>
	</target>
	
	<target name="buildLib" depends="compileLib,junitValidation" description="After 'clean', build the library JAR (only classes).">
		<echo>Generate the library:</echo>
		<jar basedir="${classesDir}" destfile="${libJarFile}" includes="${includesList}">
			<zipfileset src="${jsonJar}" excludes="META-INF/*" />
			<zipfileset src="${commonsIOJar}" excludes="META-INF/**" />
			<zipfileset src="${commonsFileUploadJar}" excludes="META-INF/**" />
			<zipfileset dir="${srcDir}" includes="${licensePath}" />
		</jar>
		<delete dir="${compileDir}" failonerror="true" />
	</target>
	
	<target name="buildLibAndSrc" depends="buildLib" description="After 'clean' and 'buildLib', build the sources JAR (only .java).">
		<jar compress="false" destfile="${srcJarFile}">
			<zipfileset dir="${srcDir}" includes="${includesList},${licensePath}" prefix="${srcDir}" />
			<zipfileset dir="${libDir}" includes="${jsonJarName}" fullPath="${jsonJar}" />
			<zipfileset dir="${libDir}" includes="${commonsIOJarName}" fullPath="${commonsIOJar}" />
			<zipfileset dir="${libDir}" includes="${commonsFileUploadJarName}" fullPath="${commonsFileUploadJar}" />
		</jar>
	</target>
	
	<!-- JAVADOC -->
	<target name="cleanJavadocBuild" description="Delete the whole generated/compiled Javadoc directory.">
		<delete dir="${javadocDir}" failonerror="false" />
	</target>
		
	<target name="cleanJavadoc" depends="cleanJavadocBuild" description="Delete the Javadoc JAR AND the whole generated/compiled Javadoc directory.">
		<delete file="${jarDest}/${javadocJarFile}" failonerror="false" />
	</target>
	
	<target name="compileJavadoc" depends="cleanJavadoc" description="After 'cleanJavadoc', compile the whole Javadoc.">
		<javadoc encoding="utf-8" charset="utf-8" docencoding="utf-8" access="protected" author="true" destdir="${javadocDir}" nodeprecated="false" nodeprecatedlist="false" noindex="false" nonavbar="false" notree="false" source="1.7" splitindex="true" use="true" version="true">
			<packageset dir="${srcDir}" includes="${includesList}" />
			<classpath refid="uws.classpath" />
		</javadoc>
    </target>
	
	<target name="buildJavadoc" depends="compileJavadoc" description="After 'compileJavadoc', build the Javadoc JAR.">
		<jar destfile="${javadocJarFile}" basedir="${javadocDir}" />
	</target>
	
</project>
