<project name="Castor core" default="compile.core" basedir="."
	xmlns:artifact="antlib:org.apache.maven.artifact.ant">

    <property environment="env" />
    <property name="source"               value="${basedir}/src"/>
    <property name="main.java"            value="${source}/main/java"/>
    <property name="main.resources"       value="${source}/main/resources"/>
    <property name="test.java"            value="${source}/test/java"/>
    <property name="test.resources"       value="${source}/test/resources"/>
    <property name="build"                value="${basedir}/build"/>
    <property name="classes"              value="${build}/classes"/>
    <property name="test.classes"         value="${build}/test"/>
    <property name="archive"              value="${project}-${version}${release}-core"/>

	<artifact:dependencies filesetId="dependency.fileset" useScope="test">
	    <artifact:pom id="core" file="${basedir}/pom.xml" />
    </artifact:dependencies>	

    <path id="build.classpath">
        <pathelement location="${basedir}/build/classes"/>
        <pathelement location="${basedir}/../build/classes"/>
    	<!--
        <pathelement location="${basedir}/../lib/commons-logging-1.1.jar"/>
        <pathelement location="${basedir}/../lib/junit_3.8.2.jar"/>
        -->
    </path>

    <path id="test.classpath">
        <path refid="build.classpath" />
        <pathelement location="${basedir}/build/test"/>
    </path>

	<target name="derive.core.libs">
		<mkdir dir="${basedir}/lib" />  	
	    <copy todir="${basedir}/lib" verbose="true">
	      <fileset refid="dependency.fileset"/>
	      <mapper type="flatten"/>
	    </copy>
	</target>	

    <target name="prepare.core" unless="prepare.executed">
        <mkdir dir="${build}"/>
        <mkdir dir="${classes}"/>
        <property name="prepare.executed" value="true"/>
    </target>

    <target name="clean.core" description="Removes build directory">
        <delete dir="${build}"/>
    </target>

    <target name="clean.tests.core" description="Removes test build directory">
        <delete dir="${test.classes}"/>
    </target>

    <target name="compile.core" depends="prepare.core, derive.core.libs" description="Compile sources">
        <javac srcdir="${main.java}" destdir="${classes}" 
               debug="on" encoding="ISO-8859-1" deprecation="false"
               source="1.5" target="1.5">
            <classpath refid="build.classpath"/>
        	<classpath>
        		<fileset dir="${basedir}/lib"/>
        	</classpath>
        </javac>
        <copy todir="${classes}">
            <fileset dir="${main.resources}">
                <include name="**"/>
            </fileset>
            <filterset begintoken="$${" endtoken="}">
                <filter token="VERSION" value="${version}"/>
                <filter token="RELEASE" value="${release}"/>
            </filterset>
        </copy>
    </target>

    <target name="jar.core" depends="compile.core" description="Build JAR file">
        <jar jarfile="${build}/${archive}.jar" basedir="${classes}" includes="**"
             manifest="${classes}/META-INF/MANIFEST.MF"/>
        <copy file="${build}/${archive}.jar" tofile="${basedir}/../dist/${archive}.jar"/>
    </target>

    <target name="tests.core" description="Runs JUnit tests" depends="compile.core">
        <junit printsummary="true">
            <formatter type="plain" />
            <batchtest>
                <fileset dir="${test.classes}">
                    <include name="**/Test*"/>
                    <include name="**/*Test"/>
                </fileset>
            </batchtest>
            <classpath refid="test.classpath" />
        </junit>
    </target>

</project>
