<?xml version="1.0"?>
<!--

  Build file to allow ant (http://jakarta.apache.org/ant/) to be used
  to build the PostgreSQL JDBC Driver.

  This file now requires Ant 1.4.1.  2002-04-18

  $Header: /cvsroot/pgsql-server/src/interfaces/jdbc/build.xml,v 1.30 2002/10/20 00:10:55 barry Exp $

-->

<!DOCTYPE project [
  <!ENTITY jarfiles "postgresql.jar,postgresql-examples.jar">
]>

<project name="postgresqlJDBC" default="all" basedir=".">

  <!-- set global properties for this build -->
  <property name="srcdir"  value="." />
  <property name="jardir"  value="jars" />
  <property name="builddir" value="build" />
  <property name="package" value="org/postgresql" />
  <property name="debug" value="on" />

  <!--
    This is a simpler method than utils.CheckVersion
    It defaults to jdbc1, but builds jdbc2 if the java.lang.Byte class is
    in the CLASSPATH (ie JDK1.2 or later), and then enterprise if the
    javax.sql.DataSource class is present.

    Important: This must have the following order: jdbc1, jdbc2, jdbc3
  -->
  <target name="check_versions">
    <condition property="jdbc1">
      <equals arg1="${ant.java.version}" arg2="1.1"/>
    </condition>
    <condition property="jdbc2">
      <or>
        <equals arg1="${ant.java.version}" arg2="1.2"/>
        <equals arg1="${ant.java.version}" arg2="1.3"/>
      </or>
    </condition>
    <condition property="jdbc3">
        <equals arg1="${ant.java.version}" arg2="1.4"/>
    </condition>
    <available property="datasource" classname="javax.sql.DataSource"/>
    <available property="junit" classname="junit.framework.Test" />
    <condition property="jdbc2tests">
      <and>
        <isset property="jdbc2"/>
        <isset property="junit"/>
      </and>
    </condition>
    <condition property="jdbc2optionaltests">
      <and>
        <isset property="jdbc2"/>
        <isset property="datasource"/>
        <isset property="junit"/>
      </and>
    </condition>
    <condition property="jdbc3tests">
      <and>
        <isset property="jdbc3"/>
        <isset property="junit"/>
      </and>
    </condition>
  </target>


  <!-- default target -->
  <target name="all">
    <antcall target="jar" />
  </target>


  <!-- create the jar file -->
  <target name="jar" depends="compile,examples">
    <jar jarfile="${jardir}/postgresql.jar" whenempty="fail">
      <fileset dir="${builddir}">
        <include name="${package}/**/*.class" />
        <exclude name="${package}/test/**" />
      </fileset>

      <fileset dir="${srcdir}">
        <include name="${package}/*.properties" />
      </fileset>
    </jar>

    <jar jarfile="${jardir}/postgresql-examples.jar" whenempty="fail">
      <fileset dir="${builddir}">
        <include name="example/**/*.class" />
      </fileset>

      <fileset dir="${srcdir}">
        <include name="example/*.properties" />
      </fileset>
    </jar>
  </target>


  <!-- This is the core of the driver.  It is common for all three versions. -->
  <target name="compile" depends="prepare,check_versions,driver">
    <javac srcdir="${srcdir}" destdir="${builddir}" debug="${debug}">
      <include name="${package}/**" />

      <exclude name="${package}/jdbc1/**" unless="jdbc1"/>
      <exclude name="${package}/jdbc2/**" unless="jdbc2"/>
      <exclude name="${package}/jdbc3/**" unless="jdbc3"/>

      <exclude name="${package}/jdbc2/optional/**" unless="jdbc2" />
      <exclude name="${package}/jdbc2/optional/**" unless="datasource" />

      <exclude name="${package}/test/**"/>
    </javac>
  </target>


  <!--
    This generates Driver.java from Driver.java.in
    It's required for importing the driver version properties
  -->
  <target name="driver" depends="prepare,check_versions">
    <!-- determine the edition text -->
    <condition property="edition" value="JDBC1">
        <equals arg1="${jdbc1}" arg2="true"/>
    </condition>
    <condition property="edition" value="JDBC2">
        <equals arg1="${jdbc2}" arg2="true"/>
    </condition>
    <condition property="edition" value="JDBC3">
        <equals arg1="${jdbc3}" arg2="true"/>
    </condition>
    <condition property="edition" value="JDBC2 Enterprise">
      <and>
        <available classname="javax.sql.DataSource" />
        <equals arg1="${jdbc2}" arg2="true"/>
      </and>
    </condition>

    <!-- determine the connection class -->
    <condition property="connectclass" value="org.postgresql.jdbc1.Jdbc1Connection">
        <equals arg1="${jdbc1}" arg2="true"/>
    </condition>
    <condition property="connectclass" value="org.postgresql.jdbc2.Jdbc2Connection">
        <equals arg1="${jdbc2}" arg2="true"/>
    </condition>
    <condition property="connectclass" value="org.postgresql.jdbc3.Jdbc3Connection">
        <equals arg1="${jdbc3}" arg2="true"/>
    </condition>

    <!-- Some defaults -->
    <filter token="MAJORVERSION" value="${major}" />
    <filter token="MINORVERSION" value="${minor}" />
    <filter token="VERSION" value="PostgreSQL ${fullversion} ${edition}" />
    <filter token="JDBCCONNECTCLASS" value="${connectclass}" />
    <filter token="DEF_PGPORT" value="${def_pgport}" />

     <fail unless="major" message="'major' undefined. Please follow the directions in README."/>
     <fail unless="minor" message="'minor' undefined. Please follow the directions in README."/>
 
    <!-- Put a check for the current version here -->

    <!-- now copy and filter the file -->
    <copy file="${package}/Driver.java.in"
          overwrite="true"
          tofile="${package}/Driver.java"
          filtering="yes" />

    <echo message="Configured build for the ${edition} edition driver" />
  </target>


  <!-- Prepares the build directory -->
  <target name="prepare">
    <!-- use the enable_debug option from configure -->
    <condition property="debug" value="on">
      <and>
        <equals arg1="${enable_debug}" arg2="yes" />
      </and>
    </condition>
    <mkdir dir="${builddir}" />
    <mkdir dir="${jardir}" />
  </target>


  <!-- This builds the examples -->
  <target name="examples" depends="compile">
    <javac srcdir="${srcdir}" destdir="${builddir}" debug="${debug}">
      <include name="example/**" />
      <exclude name="example/corba/**"/>
      <exclude name="example/blobtest.java" if="jdbc1"/>
    </javac>
  </target>


  <!-- Builds the corba example -->
  <target name="corba" if="jdbc2">
    <exec dir="${srcdir}/example/corba" executable="idl2java">
      <arg value="stock.idl" />
    </exec>

    <javac srcdir="${srcdir}" destdir="${builddir}" debug="${debug}">
      <include name="example/corba/**" />
    </javac>
  </target>



  <!-- Install the jar files -->
  <target name="install" depends="all" if="install.directory">
    <copy todir="${install.directory}" overwrite="true">
      <fileset dir="${jardir}" includes="&jarfiles;" />
    </copy>
  </target>


  <!-- Uninstall the jar file -->
  <target name="uninstall" if="install.directory">
    <delete>
      <fileset dir="${install.directory}" includes="&jarfiles;" />
    </delete>
  </target>



  <!-- This target removes any class files from the build directory -->
  <target name="clean">
    <delete quiet="true" dir="${builddir}" />
    <delete quiet="true" dir="${jardir}" />
    <delete quiet="true" file="${package}/Driver.java" />
  </target>



  <!-- This compiles and executes the JUnit tests -->

  <!-- defaults for the tests - override these if required -->
  <property name="database" value="jdbc:postgresql:test" />
  <property name="username" value="test" />
  <!-- Password must be something.  Doesn't matter if trust is used! -->
  <property name="password" value="password" />
  <!-- junit.ui is one of textui, awtui, or swingui -->
  <property name="junit.ui" value="textui" />


  <target name="test" depends="testjdbc2,testjdbc2optional,testjdbc3">
   </target>

  <target name="testjdbc2" depends="jar" if="jdbc2tests">
    <javac srcdir="${srcdir}" destdir="${builddir}" debug="${debug}">
      <include name="${package}/test/jdbc2/*" />
    </javac>
   <java fork="yes" classname="junit.${junit.ui}.TestRunner" taskname="junit" failonerror="true">
      <arg value="org.postgresql.test.jdbc2.Jdbc2TestSuite" />
      <sysproperty key="database" value="${database}" />
      <sysproperty key="username" value="${username}" />
      <sysproperty key="password" value="${password}" />
      <classpath>
        <pathelement location="${builddir}" />
        <pathelement path="${java.class.path}" />
      </classpath>
    </java>
  </target>

  <target name="testjdbc2optional" depends="jar" if="jdbc2optionaltests">
    <javac srcdir="${srcdir}" destdir="${builddir}" debug="${debug}">
      <include name="${package}/test/jdbc2/optional/**" />
      <include name="${package}/test/util/**" />
    </javac>
    <java fork="yes" classname="junit.${junit.ui}.TestRunner" taskname="junit" failonerror="true">
      <arg value="org.postgresql.test.jdbc2.optional.OptionalTestSuite" />
      <sysproperty key="database" value="${database}" />
      <sysproperty key="username" value="${username}" />
      <sysproperty key="password" value="${password}" />
      <classpath>
        <pathelement location="${builddir}" />
        <pathelement path="${java.class.path}" />
      </classpath>
    </java>
  </target>

  <target name="testjdbc3" depends="jar" if="jdbc3tests">
    <javac srcdir="${srcdir}" destdir="${builddir}" debug="${debug}">
      <include name="${package}/test/jdbc3/*" />
      <include name="${package}/test/util/*" />
    </javac>
    <java fork="yes" classname="junit.${junit.ui}.TestRunner" taskname="junit" failonerror="true">
      <arg value="org.postgresql.test.jdbc3.Jdbc3TestSuite" />
      <sysproperty key="database" value="${database}" />
      <sysproperty key="username" value="${username}" />
      <sysproperty key="password" value="${password}" />
      <classpath>
        <pathelement location="${builddir}" />
        <pathelement path="${java.class.path}" />
      </classpath>
    </java>
  </target>


</project>
