<?xml version="1.0" encoding="UTF-8"?>
<project name="common" basedir="." default="" xmlns:ivy="antlib:org.apache.ivy.ant" xmlns:antcontrib="antlib:net.sf.antcontrib">

	<tstamp>
		<format property="build.date" pattern="yyyy-MM-dd" />
	</tstamp>

	<dirname property="vaadin.basedir" file="${ant.file.common}" />
	<property file="${vaadin.basedir}/build.properties" />

	<property name="modules.to.publish.to.maven" value="shared,server,client,client-compiler,client-compiled,theme-compiler,themes" />
	<property name="modules.to.publish.to.download" value="${modules.to.publish.to.maven},all" />

	<ivy:settings file="${vaadin.basedir}/ivysettings.xml" />
	<ivy:settings file="${vaadin.basedir}/ivysettings.xml" id="ivysettings" />
	<ivy:resolve file="${vaadin.basedir}/ivy-taskdefs.xml" conf="taskdefs" />
	<ivy:cachepath pathid="taskdefs.classpath" conf="taskdefs" />
	<taskdef uri="antlib:net.sf.antcontrib" resource="net/sf/antcontrib/antlib.xml" classpathref="taskdefs.classpath" />
	<!-- ant contrib for Maven integration -->
	<taskdef resource="org/apache/maven/artifact/ant/antlib.xml" uri="antlib:org.apache.maven.artifact.ant" classpathref="taskdefs.classpath" />

	<!-- FIXME These are not available in other files -->
	<antcontrib:propertyregex property="vaadin.version.major" input="${vaadin.version}" regexp="([^\.]*)\.([^\.]*)\.([^\.]*)" select="\1" />
	<antcontrib:propertyregex property="vaadin.version.minor" input="${vaadin.version}" regexp="([^\.]*)\.([^\.]*)\.([^\.]*)" select="\2" />
	<antcontrib:propertyregex property="vaadin.version.revision" input="${vaadin.version}" regexp="([^\.]*)\.([^\.]*)\.([^\.]*)" select="\3" />

	<union id="empty.reference" />

	<property name="filtered.webcontent.dir" location="${vaadin.basedir}/result/filteredWebContent" />

	<target name="filter.webcontent" unless="webcontent.filtered">
		<property name="webcontent.filtered" value="true" />

		<delete dir="${filtered.webcontent.dir}" />
		<copy todir="${filtered.webcontent.dir}">
			<fileset dir="${vaadin.basedir}/WebContent">
				<include name="img/**" />
			</fileset>
		</copy>
		<copy todir="${filtered.webcontent.dir}">
			<fileset dir="${vaadin.basedir}/WebContent">
				<patternset>
					<include name="release-notes.html" />
					<include name="license.html" />
					<include name="css/**" />
				</patternset>
			</fileset>
			<filterchain>
				<expandproperties />
				<replacetokens begintoken="@" endtoken="@">
					<token key="version" value="${vaadin.version}" />
				</replacetokens>
				<replacetokens begintoken="@" endtoken="@">
					<token key="version-minor" value="${vaadin.version.major}.${vaadin.version.minor}" />
				</replacetokens>
				<replacetokens begintoken="@" endtoken="@">
					<token key="builddate" value="${build.date}" />
				</replacetokens>
			</filterchain>
		</copy>


	</target>
	<fileset dir="${filtered.webcontent.dir}" id="common.files.for.all.jars">
		<patternset>
			<include name="release-notes.html" />
			<include name="license.html" />
			<include name="css/**" />
			<include name="img/**" />
		</patternset>
	</fileset>


	<target name="pom.xml" description="Generates a pom.xml based on the Ivy configuration. Either for a snapshot or a release version" depends="pom.xml.release,pom.xml.snapshot">
	</target>

	<target name="pom.xml.release" if="build.release">
		<fail unless="result.dir" message="No result.dir parameter given" />
		<property name="ivy.xml" location="${result.dir}/../ivy.xml" />
		<property name="pom.xml" location="${result.dir}/lib/${module.name}-${vaadin.version}.pom" />
		<property name="conf" value="build, build-provided" />
		<property name="vaadin.maven.version" value="${vaadin.version}" />

		<ivy:makepom templatefile="${vaadin.basedir}/pom-template.xml" ivyfile="${ivy.xml}" pomfile="${pom.xml}" conf="${conf}">
			<mapping conf="build" scope="compile" />
			<mapping conf="build-provided" scope="provided" />
		</ivy:makepom>
	</target>

	<target name="pom.xml.snapshot" unless="build.release">
		<fail unless="result.dir" message="No result.dir parameter given" />
		<property name="ivy.xml" location="${result.dir}/../ivy.xml" />
		<property name="pom.xml" location="${result.dir}/lib/${module.name}-${vaadin.version}.pom" />
		<property name="temp.pom.xml" location="${pom.xml}.temp" />
		<property name="conf" value="build, build-provided" />
		<property name="vaadin.maven.version" value="${vaadin.version.major}.${vaadin.version.minor}-SNAPSHOT" />

		<ivy:makepom templatefile="${vaadin.basedir}/pom-template.xml" ivyfile="${ivy.xml}" pomfile="${temp.pom.xml}" conf="${conf}">
			<mapping conf="build" scope="compile" />
			<mapping conf="build-provided" scope="provided" />
		</ivy:makepom>
		<copy file="${temp.pom.xml}" tofile="${pom.xml}">
			<filterchain>
				<replacestring from="${vaadin.version}" to="${vaadin.maven.version}" />
			</filterchain>
		</copy>
		<delete file="${temp.pom.xml}" />
	</target>


	<target name="sources.jar" depends="compile">
		<fail unless="result.dir" message="No result.dir parameter given" />
		<fail unless="module.name" message="No module.name parameter given" />
		<fail unless="src" message="No src directory parameter given" />

		<property name="sources.jar" location="${result.dir}/lib/${module.name}-${vaadin.version}-sources.jar" />

		<jar file="${sources.jar}" compress="true">
			<fileset dir="${src}">
				<patternset>
					<include name="**/*.java" />
				</patternset>
			</fileset>
			<fileset refid="common.files.for.all.jars" />
		</jar>

	</target>

	<target name="javadoc.jar" depends="dependencies">
		<fail unless="result.dir" message="No result.dir parameter given" />
		<fail unless="module.name" message="No module.name parameter given" />
		<property name="src" location="{$result.dir}/../src" />
		<property name="javadoc.dir" value="${result.dir}/javadoc" />
		<property name="javadoc.jar" location="${result.dir}/lib/${module.name}-${vaadin.version}-javadoc.jar" />

		<javadoc destdir="${javadoc.dir}" author="true" version="true" use="true" windowtitle="${module.name}">
			<packageset dir="${src}" excludes="${classes.exclude}" />
			<doctitle>&lt;h1>${module.name}&lt;/h1></doctitle>
			<!-- <header><![CDATA[<script type="text/javascript" src=".html-style/style.js"></script>]]></header> -->
			<bottom>${javadoc.bottom}</bottom>
			<link offline="true" href="http://docs.oracle.com/javase/6/docs/api/" packagelistLoc="build/javadoc/j2se-1.6.0" />
			<link offline="true" href="http://java.sun.com/j2ee/1.4/docs/api/" packagelistLoc="build/javadoc/j2ee-1.4" />
			<classpath refid="classpath.compile.dependencies" />
		</javadoc>

		<!-- Create a javadoc jar -->
		<jar file="${javadoc.jar}" compress="true">
			<fileset dir="${javadoc.dir}" />
			<fileset refid="common.files.for.all.jars" />
		</jar>

	</target>

	<target name="jar" depends="compile, pom.xml">
		<fail unless="result.dir" message="No result.dir parameter given" />
		<fail unless="module.name" message="No module.name parameter given" />

		<property name="result.jar" location="${result.dir}/lib/${module.name}-${vaadin.version}.jar" />
		<property name="module.symbolic" value="com.vaadin.${module.name}" />
		<property name="classes" location="{$result.dir}/classes" />
		<property name="src" location="{$result.dir}/../src" />

		<union id="jar.files">
			<fileset dir="${classes}" excludes="${classes.exclude}" />
			<fileset dir="${src}" excludes="${jar.exclude}" />
			<fileset refid="common.files.for.all.jars" />
			<union refid="extra.jar.includes" />
		</union>
		<jar destfile="${result.jar}" duplicate="fail" index="true">
			<manifest>
				<attribute name="Implementation-Vendor" value="Vaadin Ltd" />
				<attribute name="Implementation-URL" value="http://vaadin.com" />
				<attribute name="Implementation-Version" value="${vaadin.version}" />
				<attribute name="Bundle-Version" value="${vaadin.version}" />
				<attribute name="Bundle-ManifestVersion" value="2" />
				<attribute name="Bundle-Name" value="${module.name}" />
				<attribute name="Bundle-SymbolicName" value="${module.symbolic}" />
				<attribute name="Bundle-Vendor" value="${vaadin.vendor}" />
				<attribute name="Bundle-RequiredExecutionEnvironment" value="JavaSE-1.6" />
			</manifest>
			<union refid="jar.files" />
		</jar>

		<antcall target="common.generate-osgi-exports">
			<param name="jar" value="${result.jar}" />
		</antcall>

	</target>

	<target name="generate-osgi-exports">
		<fail unless="jar" message="No jar parameter given" />

		<!-- Generate the Export-Package attribute in the manifest of the 
            JAR -->
		<java classname="com.vaadin.buildhelpers.GeneratePackageExports" failonerror="true" fork="yes">
			<arg value="${jar}" />
			<classpath refid="vaadin.buildhelpers.classpath" />
		</java>
	</target>

	<target name="compile" description="Compiles the module" depends="dependencies">
		<fail unless="module.name" message="No module name given" />
		<property name="result.dir" location="result" />
		<property name="src" location="${result.dir}/../src" />
		<property name="classes" location="${result.dir}/classes" />

		<mkdir dir="${classes}" />

		<javac srcdir="${src}" destdir="${classes}" source="${vaadin.java.version}" target="${vaadin.java.version}" debug="true" encoding="UTF-8" includeantruntime="false">
			<classpath refid="classpath.compile.dependencies" />
			<classpath refid="classpath.compile.custom" />
		</javac>
	</target>

	<target name="tests.run" depends="tests.compile">
		<fail unless="module.name" message="No module name given" />

		<property name="result.dir" location="result" />
		<property name="classes" location="${result.dir}/classes" />
		<property name="tests.src" location="${result.dir}/../tests/src" />
		<property name="tests.classes" location="${result.dir}/tests/classes" />


		<junit printsummary="withOutAndErr" fork="yes">
			<formatter usefile="false" type="plain" />
			<jvmarg value="-ea" />
			<classpath location="${tests.classes}" />
			<classpath location="${classes}" />
			<classpath refid="classpath.compile.custom" />
			<classpath refid="classpath.tests.dependencies" />

			<batchtest fork="yes">
				<fileset dir="${tests.src}">
					<exclude name="**/Abstract*" />
					<exclude name="com/vaadin/tests/data/bean/*" />
					<exclude name="com/vaadin/tests/util/*" />
					<exclude name="**/VaadinClasses.java" />
					<exclude name="**/SQLTestsConstants.java" />
				</fileset>
			</batchtest>
		</junit>
	</target>

	<target name="tests.compile" description="Compiles tests" depends="compile, dependencies.tests">
		<fail unless="module.name" message="No module name given" />
		<property name="result.dir" location="result" />
		<property name="base.dir" location="${result.dir}/.." />
		<property name="tests.src" location="${base.dir}/tests/src" />
		<property name="tests.resources" location="${base.dir}/tests/resources" />
		<property name="tests.classes" location="${result.dir}/tests/classes" />
		<property name="classes" location="${result.dir}/classes" />

		<mkdir dir="${tests.classes}" />

		<javac srcdir="${tests.src}" destdir="${tests.classes}" source="${vaadin.java.version}" target="${vaadin.java.version}" debug="true" encoding="UTF-8" includeantruntime="false">
			<classpath refid="classpath.tests.dependencies" />
			<classpath location="${classes}" />
			<classpath refid="classpath.tests.custom" />
		</javac>

		<!-- Copy resources -->
		<copy todir="${tests.classes}" failonerror="false">
			<fileset dir="${tests.resources}" />
		</copy>
	</target>

	<target name="dependencies" description="Resolves dependencies needed by this module" depends="filter.webcontent">
		<ivy:resolve resolveid="common" conf="build, build-provided" />
		<ivy:cachepath pathid="classpath.compile.dependencies" conf="build, build-provided" />
	</target>

	<target name="dependencies.tests" description="Resolves dependencies needed by tests">
		<ivy:resolve resolveid="common" conf="tests" />
		<ivy:cachepath pathid="classpath.tests.dependencies" conf="tests" />
	</target>

	<target name="clean">
		<fail unless="result.dir" message="No result.dir parameter given" />
		<delete dir="${result.dir}" />
	</target>

	<target name="publish-local" description="Publishes the given module to the local repository">
		<fail unless="result.dir" message="No result.dir parameter given" />
		<property name="conf" value="*(public)" />

		<ivy:resolve conf="${conf}" />
		<ivy:publish settingsref="ivysettings" conf="${conf}" resolver="build-temp" overwrite="true" forcedeliver="true">
			<!--			<artifacts pattern="${result.dir}/[artifact]-[revision].[ext]" />-->
			<artifacts pattern="${result.dir}/lib/[artifact]-[revision](-[classifier]).[ext]" />

		</ivy:publish>
		<ivy:publish settingsref="ivysettings" conf="${conf}" resolver="local-maven" overwrite="true">
			<artifacts pattern="${result.dir}/lib/[artifact]-[revision](-[classifier]).[ext]" />
		</ivy:publish>
	</target>

	<target name="publish.to.local.maven">
		<property name="conf" value="*(public)" />

		<ivy:resolve conf="${conf}" />
		<ivy:publish conf="${conf}" resolver="local-maven" overwrite="true">
		</ivy:publish>
	</target>
</project>