<project name="Castor XML - Core module" default="compile.xml" 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="main.generated"       value="${basedir}/target/generated-sources/castor"/>
    <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}-xml"/>
    <property name="gen.src"              value="${build}/generated-sources"/>

    <property name="castor.xml.files"
          value="**/org/exolab/castor/*,
                 org/exolab/castor/mapping/**/*.java,
                 org/exolab/castor/net/**/*.java,
                 org/exolab/castor/tools/**/*.java,
                 org/exolab/castor/types/**/*.java,
                 org/exolab/castor/util/**/*.java,
                 org/exolab/castor/xml/**/*.java,
                 org/castor/xml/**/*.java"/>

    <property name="excludes"             
    	      value="**/package.html,**/dax/**,**/dax/engine/**, org/exolab/castor/xml/schema/**,org/exolab/castor/xml/dtd/**"/>

	<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}/../core/build/classes"/>
    </path>

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

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

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

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

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

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

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

    <target name="compile.bugs.xml" depends="compile.xml" 
    	    description="Build the tests packages">
        <mkdir dir="${test.classes}"/>
        <javac srcdir="${test.java}" destdir="${test.classes}"
               debug="on" encoding="ISO-8859-1" deprecation="${deprecation}"
        	   source="1.5" target="1.5">
            <classpath refid="build.classpath"/>
        </javac>

        <copy todir="${test.classes}">
            <fileset dir="${test.resources}"/>
        </copy>
    </target>
	
	<target name="tests.xml" description="Runs JUnit tests" depends="compile.bugs.xml">
		<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>
