<project name="crawler-commons" default="compile">

	<property name="root.dir" value="${basedir}" />
	<property file="${root.dir}/build.properties" />

	<!-- define artifacts' name, which follows the convention of Maven -->
	<property name="maven-jar" value="${dist.dir}/lib/${artifactId}-${version}.jar" />
	<property name="maven-javadoc-jar"
		value="${dist.dir}/lib/${artifactId}-${version}-javadoc.jar" />
	<property name="maven-sources-jar"
		value="${dist.dir}/lib/${artifactId}-${version}-sources.jar" />

	<!-- defined maven snapshots and staging repository id and url -->
	<property name="maven-snapshots-repository-id" value="sonatype-nexus-snapshots" />
	<property name="maven-snapshots-repository-url"
		value="https://oss.sonatype.org/content/repositories/snapshots/" />
	<property name="maven-staging-repository-id" value="sonatype-nexus-staging" />
	<property name="maven-staging-repository-url"
		value="https://oss.sonatype.org/service/local/staging/deploy/maven2/" />


	<!-- ================================================================== -->
	<!-- General cleaning sources -->
	<!-- ================================================================== -->

	<target name="clean" description="--> clean the project">
		<echo>cleaning ${ant.project.name}</echo>
		<delete includeemptydirs="true" failonerror="false">
			<fileset dir="${build.dir}">
				<exclude name="classes-*-eclipse/" />
				<exclude name="crawlercommons.log" />
			</fileset>
		</delete>
	</target>


	<!-- ================================================================== -->
	<!-- Maven -->
	<!-- ================================================================== -->

	<target name="mvn-init" unless="compile.classpath"
		xmlns:artifact="urn:maven-artifact-ant" 
		description="--> initialize local maven-ant-tasks jar">
		<path id="maven.ant.tasks.classpath" path="${lib.dir}/maven-ant-tasks-2.0.10.jar" />
		<typedef resource="org/apache/maven/artifact/ant/antlib.xml"
			uri="urn:maven-artifact-ant" classpathref="maven.ant.tasks.classpath" />

		<condition property="maven.repo.local" value="${maven.repo.local}"
			else="${user.home}/.m2/repository">
			<isset property="maven.repo.local" />
		</condition>

		<echo>maven.repo.local=${maven.repo.local}</echo>
		<artifact:localRepository id="local.repository"
			path="${maven.repo.local}" />
		<artifact:pom file="pom.xml" id="maven.project" />

		<artifact:dependencies pathId="compile.classpath"
			filesetId="compile.fileset" useScope="compile">
			<pom refid="maven.project" />
			<localRepository refid="local.repository" />
		</artifact:dependencies>

		<artifact:dependencies pathId="test.classpath"
			filesetId="test.fileset" useScope="test">
			<pom refid="maven.project" />
			<localRepository refid="local.repository" />
		</artifact:dependencies>

		<artifact:dependencies pathId="runtime.classpath"
			filesetId="runtime.fileset" useScope="runtime">
			<pom refid="maven.project" />
			<localRepository refid="local.repository" />
		</artifact:dependencies>
	</target>

	<target name="mvn-install" xmlns:artifact="urn:maven-artifact-ant"
		depends="mvn-init" description="--> install local maven artifact">
		<artifact:install file="${build.dir}/${jar.name}">
			<pom refid="maven.project" />
		</artifact:install>
	</target>

	<!-- ================================================================== -->
	<!-- Build sources -->
	<!-- ================================================================== -->

	<target name="compile" depends="mvn-init" description="--> compile main classes">
		<mkdir dir="${build.dir.main-classes}" />
		<javac encoding="${build.encoding}" srcdir="${main.src.dir}"
			includes="**/*.java" destdir="${build.dir.main-classes}" debug="${javac.debug}"
			optimize="${javac.optimize}" target="${javac.version}" source="${javac.version}"
			deprecation="${javac.deprecation}">
			<compilerarg line="${javac.args} ${javac.args.warnings}" />
			<classpath refid="compile.classpath" />
		</javac>
	</target>


	<!-- ================================================================== -->
	<!-- Unit Tests -->
	<!-- ================================================================== -->

	<target name="compile-test" depends="compile" description="--> compile test classes">
		<echo>*** Building Unit Tests Sources ***</echo>
		<mkdir dir="${build.dir.test-classes}" />
		<path id="test.path">
			<pathelement location="${build.dir.main-classes}" />
		</path>
		<javac encoding="${build.encoding}" srcdir="${test.src.dir}"
			includes="**/*.java" destdir="${build.dir.test-classes}" debug="${javac.debug}"
			optimize="${javac.optimize}" target="${javac.version}" source="${javac.version}"
			deprecation="${javac.deprecation}">
			<compilerarg line="${javac.args} ${javac.args.warnings}" />
			<classpath refid="test.classpath" />
			<classpath refid="test.path" />
		</javac>
	</target>

	<target name="test" depends="compile-test" description="--> run unit tests">
		<delete dir="${build.dir.test-reports}" />
		<mkdir dir="${build.dir.test-reports}" />

		<junit showoutput="false" printsummary="yes" haltonfailure="no"
			fork="yes" maxmemory="256m" dir="${basedir}" errorProperty="tests.failed"
			failureProperty="tests.failed">
			<sysproperty key="net.sourceforge.cobertura.datafile"
				file="${reports.dir}/crawlercommons_coverage.ser" />
			<classpath>
				<pathelement location="${instrumented.dir}" />
				<pathelement location="${build.dir.main-classes}" />
				<pathelement location="${build.dir.test-classes}" />
				<pathelement location="${main.res.dir}" />
				<pathelement location="${test.res.dir}" />
				<path refid="test.classpath" />
			</classpath>
			<formatter usefile="no" type="plain" />
			<formatter type="xml" />
			<batchtest fork="yes" todir="${build.dir.test-reports}">
				<fileset dir="${test.src.dir}">
					<include name="**/*Test.java" />
					<exclude name="**/Abstract*.java" />
				</fileset>
			</batchtest>
		</junit>
		<fail if="tests.failed">Tests failed!</fail>
	</target>

	<!-- ================================================================== -->
	<!-- Build jar -->
	<!-- ================================================================== -->

	<target name="jar" depends="compile" description="--> build crawer commons jar">

		<tstamp>
			<format property="timestamp" pattern="MMM dd yyyy, HH:mm:ss" />
		</tstamp>

		<jar destfile="${build.dir}/${jar.name}" compress="true">
			<fileset dir="${build.dir.main-classes}" />
			<fileset dir="${main.res.dir}" />

			<manifest>
				<section name="crawler-commons">
					<attribute name="Implementation-Title" value="${ant.project.name}" />
					<attribute name="Implementation-Version" value="${version}" />
					<attribute name="Compile-Time" value="${timestamp}" />
					<attribute name="Compiled-By" value="${user.name}" />
				</section>
			</manifest>
		</jar>
	</target>


	<!-- ================================================================== -->
	<!-- Java Doc -->
	<!-- ================================================================== -->

	<target name="doc" depends="compile" description="--> create javadoc">
		<mkdir dir="${build.javadoc}" />
		<javadoc packagenames="${javadoc.package}" destdir="${build.javadoc}"
			author="true" version="true" use="true" windowtitle="${name} ${version} API"
			doctitle="${name} ${version} API">
			<packageset dir="${main.src.dir}" />
			<link href="${javadoc.link.java}" />
			<classpath>
				<path refid="compile.classpath" />
			</classpath>
			<group title="${javadoc.title}" packages="${javadoc.package}*" />
		</javadoc>
	</target>


	<!-- ================================================================== -->
	<!-- Install in local Maven repository									-->
	<!-- ================================================================== -->

	<target name="install"
			depends="jar, mvn-install"
			description="--> install SNAPSHOT jar to local repository">
	</target>
	
	<!-- ================================================================== -->
	<!-- Build JAR artifacts -->
	<!-- ================================================================== -->

	<target name="dist" depends="jar,doc" description="--> generate the distribution">

		<!-- copy the main artifact -->
		<copy tofile="${maven-jar}" file="${build.dir}/${jar.name}" />

		<!-- build the javadoc artifact -->
		<jar jarfile="${maven-javadoc-jar}">
			<fileset dir="${build.javadoc}" />
		</jar>

		<!-- build the sources artifact -->
		<jar jarfile="${maven-sources-jar}">
			<fileset dir="${main.src.dir}" />
		</jar>
	</target>

	<target name="deploy" depends="dist"
		description="--> deploy snapshot version to Maven snapshot repository"
		xmlns:artifact="urn:maven-artifact-ant">
		<artifact:mvn>
			<arg value="org.apache.maven.plugins:maven-deploy-plugin:2.6:deploy-file" />
			<arg value="-Durl=${maven-snapshots-repository-url}" />
			<arg value="-DrepositoryId=${maven-snapshots-repository-id}" />
			<arg value="-DpomFile=pom.xml" />
			<arg value="-Dfile=${maven-jar}" />
		</artifact:mvn>
	</target>

	<!-- before this, update project version (both build.xml and pom.xml) from 
		SNAPSHOT to RELEASE -->
	<target name="stage" depends="dist"
		description="--> deploy release version to Maven staging repository"
		xmlns:artifact="urn:maven-artifact-ant">
		<!-- sign and deploy the main artifact -->
		<artifact:mvn>
			<arg
				value="org.apache.maven.plugins:maven-gpg-plugin:1.3:sign-and-deploy-file" />
			<arg value="-Durl=${maven-staging-repository-url}" />
			<arg value="-DrepositoryId=${maven-staging-repository-id}" />
			<arg value="-DpomFile=pom.xml" />
			<arg value="-Dfile=${maven-jar}" />
			<arg value="-Pgpg" />
		</artifact:mvn>

		<!-- sign and deploy the sources artifact -->
		<artifact:mvn>
			<arg
				value="org.apache.maven.plugins:maven-gpg-plugin:1.3:sign-and-deploy-file" />
			<arg value="-Durl=${maven-staging-repository-url}" />
			<arg value="-DrepositoryId=${maven-staging-repository-id}" />
			<arg value="-DpomFile=pom.xml" />
			<arg value="-Dfile=${maven-sources-jar}" />
			<arg value="-Dclassifier=sources" />
			<arg value="-Pgpg" />
		</artifact:mvn>

		<!-- sign and deploy the javadoc artifact -->
		<artifact:mvn>
			<arg
				value="org.apache.maven.plugins:maven-gpg-plugin:1.3:sign-and-deploy-file" />
			<arg value="-Durl=${maven-staging-repository-url}" />
			<arg value="-DrepositoryId=${maven-staging-repository-id}" />
			<arg value="-DpomFile=pom.xml" />
			<arg value="-Dfile=${maven-javadoc-jar}" />
			<arg value="-Dclassifier=javadoc" />
			<arg value="-Pgpg" />
		</artifact:mvn>
	</target>


	<!-- ================================================================== -->
	<!-- Generate a package -->
	<!-- ================================================================== -->

	<target name="package" depends="test, jar, doc"
		description="--> create a tarball distribution">

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

		<!-- create target directory -->
		<mkdir dir="${build.dir.dist}" />
		<mkdir dir="${build.dir.dist}/docs" />
		<mkdir dir="${build.dir.dist}/docs/licenses" />
		<mkdir dir="${build.dir.dist}/docs/javadoc" />
		<mkdir dir="${build.dir.dist}/docs/reports" />
		<mkdir dir="${build.dir.dist}/lib" />
		<mkdir dir="${build.dir.dist}/src" />

		<!-- copy javadoc to target dir -->
		<copy todir="${build.dir.dist}/docs/javadoc">
			<fileset dir="${build.javadoc}" />
		</copy>

		<!-- copy any raw libs -->
		<copy todir="${build.dir.dist}/lib" flatten="true">
			<fileset dir="${lib.dir}" />
			<path refId="compile.classpath" />
			<path refId="runtime.classpath" />
		</copy>

		<!-- copy src -->
		<copy todir="${build.dir.dist}/src">
			<fileset dir="${basedir}/src" />
		</copy>

		<!-- copy project jar to dist dir -->
		<copy todir="${build.dir.dist}">
			<fileset file="${build.dir}/${jar.name}" />
		</copy>

		<!-- copy build files to dist dir -->
		<copy todir="${build.dir.dist}">
			<fileset file="${basedir}/build.xml" />
			<fileset file="${basedir}/build.properties" />
			<fileset file="${basedir}/pom.xml" />
		</copy>

		<!-- copy documents -->
		<copy todir="${build.dir.dist}">
			<fileset file="${basedir}/README" />
		</copy>
		<copy todir="${build.dir.dist}/docs">
			<fileset file="${basedir}/doc/eclipse-formatter.properties" />
		</copy>
		<copy todir="${build.dir.dist}/docs/licenses">
			<fileset dir="${basedir}/doc/licenses" />
		</copy>
		<!-- copy license to the root -->
		<copy todir="${build.dir.dist}" file="${basedir}/doc/LICENSE.txt" />

		<tar longfile="gnu" compression="gzip" destfile="${build.release.file}">
			<tarfileset dir="${build.dir.dist}" />
		</tar>
	</target>


	<!-- ================================================================== -->
	<!-- Generating eclipse file -->
	<!-- ================================================================== -->

	<target name="eclipse" depends="mvn-init, clean-eclipse"
		description="--> create the Eclipse project files">

		<taskdef name="eclipse" classname="prantl.ant.eclipse.EclipseTask"
			classpathref="compile.classpath" />
		<mkdir dir="${build.dir.main-classes-eclipse}" />
		<mkdir dir="${build.dir.test-classes-eclipse}" />
		<eclipse>
			<settings>
				<jdtcore compilercompliance="6.0" />
				<resources encoding="UTF-8" />
			</settings>
			<project name="${ant.project.name}" />
			<classpath>
				<container
					path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6" />

				<source path="${basedir}/src/main/java" output="${build.dir.main-classes-eclipse}" />
				<source path="${basedir}/src/main/resources" output="${build.dir.main-classes-eclipse}" />
				<source path="${basedir}/src/test/java" output="${build.dir.test-classes-eclipse}" />
				<source path="${basedir}/src/test/resources" output="${build.dir.test-classes-eclipse}" />
				<output path="${build.dir.main-classes-eclipse}" />
				<library pathref="test.classpath" exported="false" />
			</classpath>
		</eclipse>
		<concat destfile="${root.dir}/.settings/org.eclipse.jdt.core.prefs"
			append="true">
			<filelist dir="${root.dir}/doc/" files="eclipse-formatter.properties" />
		</concat>
		<replace file="${root.dir}/.settings/org.eclipse.jdt.core.prefs"
			token="org.eclipse.jdt.core.compiler.compliance=6.0" value="org.eclipse.jdt.core.compiler.compliance=1.6" />
	</target>

	<target name="clean-eclipse" description="--> clean the Eclipse project files">
		<delete file=".classpath" />
		<delete file=".eclipse" />
		<delete file=".project" />
		<delete dir=".settings" />
	</target>


</project>
