<?xml version="1.0" encoding="UTF-8"?>
<!--
 Licensed to the Apache Software Foundation (ASF) under one
 or more contributor license agreements.  See the NOTICE file
 distributed with this work for additional information
 regarding copyright ownership.  The ASF licenses this file
 to you under the Apache License, Version 2.0 (the
 "License"); you may not use this file except in compliance
 with the License.  You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing,
 software distributed under the License is distributed on an
 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 KIND, either express or implied.  See the License for the
 specific language governing permissions and limitations
 under the License.
-->

<!-- ======================================================================== -->
<!--                                                                          -->
<!--  maven Buildfile                                                         -->
<!--                                                                          -->
<!-- $Id$ -->
<!--                                                                          -->
<!-- ======================================================================== -->

<project
  xmlns:j="jelly:core"
  xmlns:deploy="deploy"
  default="jar:jar">

  <!-- Allow any user specific values to override the defaults -->
  <property file="${user.home}/build.properties" />
  <!-- Allow user defaults for this project -->
  <property file="build.properties" />
  <!-- Set default values for the build -->
  <property file="default.properties" />

  <!-- ================================================================== -->
  <!-- D I S T : B U I L D _ B I N   P R E   G O A L                      -->
  <!-- ================================================================== -->
  <!-- copies jars, sample configuration ... to the                       -->
  <!-- ${maven.dist.bin.assembly.dir} to include them in the binary dists -->
  <!-- ================================================================== -->
  <preGoal name="dist:build-bin">
    <!-- include listed dependencies -->
    <j:forEach var="dep" items="${pom.dependencies}">
      <j:if test="${dep.getProperty('dist.bundle') != null &amp;&amp; dep.getProperty('dist.bundle').equalsIgnoreCase('true')}">
        <copy todir="${maven.dist.bin.assembly.dir}/lib"
              file="${maven.repo.local}/${dep.getArtifactDirectory()}/jars/${dep.getArtifact()}"/>
      </j:if>
    </j:forEach>

    <!-- copy the contents of the conf directory -->
    <!-- to the right place -->
    <copy todir="${maven.dist.bin.assembly.dir}" failonerror="false">
      <fileset dir="${maven.src.dir}/conf">
        <include name="componentConfiguration.xml" />
        <include name="roleConfiguration.xml" />
        <include name="Torque.properties" />
        <include name="master/*" />
      </fileset>
      <fileset file='${basedir}/common/NOTICE'/>
      <fileset file='${basedir}/common/LICENSE.txt'/>
    </copy>
  </preGoal>

  <!-- ================================================================== -->
  <!-- D I S T : B U I L D _ S R C   P R E   G O A L                      -->
  <!-- ================================================================== -->
  <!-- includes common files                                              -->
  <!-- This is necessary as long as maven does not resolves the extends   -->
  <!-- property of the pom upon generation of the source distribution     -->
  <!-- ================================================================== -->
  <preGoal name="dist:build-src">
      <copy todir="${maven.dist.src.assembly.dir}/common">
      <fileset dir="${basedir}/common">
        <include name="project.xml"/>
        <include name="project.properties"/>
        <include name="NOTICE"/>
        <include name="LICENSE.txt"/>
      </fileset>
    </copy>
  </preGoal>

  <!-- ================================================================== -->
  <!-- D I S T : L I T E                                                  -->
  <!-- ================================================================== -->
  <!-- binary distribution without jars used by scarab                    -->
  <!-- ================================================================== -->
  <goal name="dist:lite"
        prereqs="jar:jar"
        description="distribution without dependency jars">

    <!-- copy schema files to the right place -->
    <copy todir="${maven.dist.bin.assembly.dir}/schema">
      <fileset dir="${maven.src.dir}/schema" />
    </copy>

    <!-- copy master files to the right place -->
    <copy todir="${maven.dist.bin.assembly.dir}/master">
      <fileset dir="${maven.src.dir}/conf/master" />
    </copy>

    <!-- copy config files to the right place -->
    <copy todir="${maven.dist.bin.assembly.dir}">
      <fileset dir="${maven.src.dir}/conf" />
    </copy>

    <!-- Copy Jars -->
    <copy todir="${maven.dist.bin.assembly.dir}/lib">
      <fileset dir="${maven.build.dir}">
        <include name="${maven.final.name}.jar"/>
      </fileset>
    </copy>

    <!-- Create a zip file -->
    <zip zipfile="${maven.build.dir}/${maven.final.name}.zip">
      <zipfileset dir="${maven.dist.bin.assembly.dir}"/>
    </zip>
  </goal>

  <!-- ================================================================== -->
  <!-- R U N T I M E T E S T                                              -->
  <!-- ================================================================== -->
  <goal
    name="runtime:test"
    prereqs="runtime:prepare"
    description="runtime tests (set torque.testProfile in your build.properties)">

    <ant antfile="build-test.xml"
         target="test"
         inheritAll="false">
      <property name="maven.repo.local" value="${maven.repo.local}" />
    </ant>
  </goal>

  <goal
    name="runtime:test-classpath"
    prereqs="runtime:prepare"
    description="runtime tests using useClasspath property (set torque.testProfile in your build.properties)">

    <ant antfile="build-test.xml"
         target="test-classpath"
         inheritAll="false"/>
  </goal>

  <goal
    name="runtime:prepare"
    prereqs="jar:jar">

    <j:if test="${context.getVariable('torque.testProfile') == null || context.getVariable('torque.testProfile') == ''}">
        <fail>
--------------------------------------------------------------------------
| Runtime Test Configuration Error                                       |
--------------------------------------------------------------------------

 For executing the runtime tests, you must set the property
 'torque.testProfile' by defining it on the command line of your
 maven command or putting it into the build.properties file.

--------------------------------------------------------------------------
        </fail>
    </j:if>

    <!-- Set values in test profile -->
    <property file="${torque.testProfile}"/>

    <!-- Use the profile as the build.properties file
         in the dist directory so that it is customizable
         by each tester. -->
    <echo message="Copying profile into distribution directory: ${torque.testProfile}"/>

    <filter token="APPLICATION_ROOT" value="."/>
    <filter token="DATABASE" value="${torque.database}"/>
    <filter token="DATABASE_DEFAULT" value="${torque.defaultDatabase}"/>
    <filter token="DATABASE_URL" value="${torque.database.url}"/>
    <filter token="DATABASE_USER" value="${torque.database.user}"/>
    <filter token="DATABASE_DRIVER" value="${torque.database.driver}"/>
    <filter token="DATABASE_PASSWORD" value="${torque.database.password}"/>
    <filter token="DATABASE_ID_METHOD" value="${torque.idMethod}"/>
    <filter token="DATABASE_ADAPTER" value="${torque.database}"/>
    <filter token="VALIDATION_QUERY" value="${torque.database.validationQuery}"/>

    <copy
      tofile="${torque.distDir}/build.properties"
      file="${torque.testProfile}"
      overwrite="yes"
    />

    <!-- copy test schemas -->
    <copy
      todir="${torque.distDir}/src/schema"
      filtering="yes"
      overwrite="yes">
      <fileset dir="${rttest.dir}">
        <include name="**/*.xml"/>
      </fileset>
    </copy>

    <!-- copy test sql -->
    <copy
      todir="${torque.distDir}/sql"
      filtering="yes"
      overwrite="yes">
      <fileset dir="${rttest.dir}">
        <include name="*.sql"/>
        <include name="*.ref.xml"/>
      </fileset>
    </copy>

    <!-- copy test sources -->
    <copy todir="${torque.distDir}/src/java">
      <fileset dir="${rttest.dir}"/>
    </copy>

    <copy
      tofile="${build.test}/rttest/build-torque.xml"
      file="${torque.generator.home}/src/conf/build-torque.xml"
      overwrite="yes"
    />
    <copy
      tofile="${build.test}/rttest/default.properties"
      file="${torque.generator.home}/src/conf/default.properties"
      overwrite="yes"
    />

    <!-- copy libs -->
    <deploy:copy-deps todir="${build.test}/rttest/lib"/>
    <copy file="${maven.build.dir}/${maven.final.name}.jar"
          todir="${build.test}/rttest/lib"/>
    <copy todir="${build.test}/rttest/lib">
      <fileset dir="${torque.generator.home}/target/">
        <include name="*.jar"/>
      </fileset>
    </copy>

<!-- FIXME: Do we need this for the rttests?
    <copy todir="${build.test}/rttest/lib">
      <fileset dir="${torque.template.home}/target/">
        <include name="*.jar"/>
      </fileset>
    </copy>
-->

    <copy file="${torque.testDatabaseJar}"
          todir="${build.test}/rttest/lib"/>

    <!-- copy templates -->
    <copy todir="${build.test}/rttest/templates">
      <fileset dir="${torque.template.home}/src/templates"/>
    </copy>

    <!-- copy Torque.properties -->
    <copy
      tofile="${build.test}/rttest/Torque.properties"
      file="src/conf/master/Torque.master"
      filtering="yes"
      overwrite="yes"
    />
  </goal>

  <!-- ================================================================== -->
  <!-- O J B   R U N T I M E T E S T                                      -->
  <!-- ================================================================== -->

  <goal
    name="runtime:test-ojb"
    prereqs="runtime:prepare-ojb"
    description="runtime tests for ojb tasks (set torque.testProfile in your build.properties)">

    <ant antfile="build-test.xml"
         target="test-ojb"
         inheritAll="false"/>
  </goal>

  <goal
    name="runtime:prepare-ojb"
    prereqs="jar:jar">

    <!-- Set values in test profile -->
    <property file="${torque.testProfile}"/>

    <!-- Use the profile as the build.properties file
         in the dist directory so that it is customizable
         by each tester. -->
    <echo message="Copying profile into distribution directory: ${torque.testProfile}"/>

    <filter token="APPLICATION_ROOT" value="."/>
    <filter token="DATABASE" value="${torque.database}"/>
    <filter token="DATABASE_DEFAULT" value="${torque.defaultDatabase}"/>
    <filter token="DATABASE_URL" value="${torque.database.url}"/>
    <filter token="DATABASE_USER" value="${torque.database.user}"/>
    <filter token="DATABASE_DRIVER" value="${torque.database.driver}"/>
    <filter token="DATABASE_PASSWORD" value="${torque.database.password}"/>
    <filter token="DATABASE_ID_METHOD" value="${torque.idMethod}"/>
    <filter token="DATABASE_ADAPTER" value="${torque.database}"/>

    <copy
      tofile="${test.ojb.distDir}/build.properties"
      file="${torque.testProfile}"
      overwrite="yes"
    />

    <!-- copy test schemas -->
    <copy
      todir="${test.ojb.distDir}/src/schema"
      filtering="yes"
      overwrite="yes">
      <fileset dir="${test.ojb.srcDir}">
        <include name="**/*.xml"/>
      </fileset>
    </copy>

    <!-- copy test sources -->
    <copy todir="${test.ojb.distDir}/src/java">
      <fileset dir="${test.ojb.srcDir}"/>
    </copy>

    <copy
      tofile="${test.ojb.distDir}/build-torque.xml"
      file="${torque.generator.home}/src/conf/build-torque.xml"
      overwrite="yes"
    />
    <copy
      tofile="${test.ojb.distDir}/default.properties"
      file="${torque.generator.home}/src/conf/default.properties"
      overwrite="yes"
    />

    <!-- copy libs -->
    <copy todir="${test.ojb.distDir}/lib">
      <fileset dir="${torque.generator.home}/target/">
        <include name="*.jar"/>
      </fileset>
    </copy>
    <copy file="${torque.testDatabaseJar}" todir="${test.ojb.distDir}/lib"/>
    <copy file="${ojb.jar}" todir="${test.ojb.distDir}/lib"/>
    <copy file="${commons-collections}" todir="${test.ojb.distDir}/lib"/>
  </goal>

</project>
