<!--
 -
 - 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.
 -
 -->
<project xmlns:ivy="antlib:org.apache.ivy.ant" name="common">

  <dirname property="project.root" file="${ant.file.common}"/>

  <property name="project.name"          value="qpid"/>
  <!-- Version used for standard build output -->
  <property name="project.version"       value="0.23"/>
  <!-- The release version used for maven output. SNAPSHOT added via maven.version.suffix -->
  <property name="project.version.maven" value="0.24"/>
  <property name="project.url"           value="http://qpid.apache.org"/>
  <property name="project.groupid"       value="org.apache.qpid"/>
  <property name="project.namever"       value="${project.name}-${project.version}"/>

  <property name="resources"             location="${project.root}/resources"/>
  <property name="test.profiles"         location="${project.root}/test-profiles"/>
  <property name="build"                 location="${project.root}/build"/>
  <property name="build.scratch"         location="${build}/scratch"/>
  <property name="build.bin"             location="${build}/bin"/>
  <property name="build.etc"             location="${build}/etc"/>
  <property name="build.lib"             location="${build}/lib"/>
  <property name="build.api"             location="${build}/api"/>
  <property name="build.test.api"        location="${build}/test-api"/>
  <property name="build.results"         location="${build}/results"/>
  <property name="build.failed"          location="${build.results}/FAILED"/>
  <property name="build.report"          location="${build}/report"/>
  <property name="build.release"         location="${build}/release"/>
  <property name="build.release.prepare" location="${build.release}/prepare"/>
  <property name="build.scratch.broker.plugins.lib"  location="${build.scratch}/broker-plugins/lib"/>
  <property name="build.coverage.report" location="${build}/coverage/report"/>
  <property name="build.coverage.src"    location="${build}/coverage/src"/>
  <property name="build.findbugs"        location="${build}/findbugs"/>

  <property name="java.target"           value="1.5"/>
  <property name="java.source"           value="1.5"/>

  <property name="release"               location="${project.root}/release"/>

  <property name="tasks"                 location="${project.root}/tasks"/>
  <property name="tasks.classes"         location="${tasks}/classes"/>
  <property name="tasks.src"             location="${tasks}/src"/>

  <property name="qpid.home"             location="${project.root}/build"/>
  <property name="qpid.work"             location="${qpid.home}/work"/>

  <property name="javac.compiler.args"   value=""/>

  <property name="cobertura.dir" value="${project.root}/lib/cobertura" />
  <property name="cobertura.version" value="1.9.4.1" />

  <path id="cobertura.classpath">
    <fileset dir="${cobertura.dir}">
        <include name="**/*.jar" />
    </fileset>
  </path>

  <property name="mllib.dir" value="${project.root}/../python" />
  <property name="findbugs.dir" value="${project.root}/lib/findbugs" />

  <!-- properties used to control Ant Eclipse for Eclipse classpath/project files etc -->
  <property name="eclipse.updatealways" value="false"/>
  <property name="eclipse.compilercompliance" value="5.0"/>
  <property name="eclipse.container" value="JVM 1.5"/>

  <!-- properties for generating the maven artifacts -->
  <property name="maven.local.repo"      value="${build.scratch}/maven-local-repo"/>
  <property name="maven.settings.xml"    value="${project.root}/maven-settings.xml"/>
  <property name="maven.unique.version"  value="false"/>
  <property name="maven.snapshot"        value="true"/>
  <condition property="maven.version.suffix" value="" else="-SNAPSHOT">
    <isfalse value="${maven.snapshot}"/>
  </condition>

  <!-- properties for uploading our Maven artifacts to Nexus using Ivy -->
  <property name="ivy.organisation"      value="org/apache"/>
  <property name="nexus.organisation"    value="org.apache"/>
  <property name="nexus.host"            value="repository.apache.org"/>
  <property name="nexus.upload.url"      value="https://${nexus.host}/service/local/staging/deploy/maven2"/>

  <!-- properties for deplying snapshot artifacts -->
  <property name="maven.snapshots.repo.id"     value="apache.snapshots.https"/>
  <property name="maven.snapshots.repo.url"     value="https://${nexus.host}/content/repositories/snapshots"/>

  <!-- properties for downloading ivy, and then our dependencies -->
  <property name="ivy.jar.dir"         value="${project.root}/lib/ivy" />
  <property name="ivy.install.version" value="2.2.0" />
  <property name="ivy.jar.file"        value="${ivy.jar.dir}/ivy-${ivy.install.version}.jar" />
  <property name="ivy.m2repo.url"      value="http://repo1.maven.org/maven2"/>
  <property name="ivy.repo.url"        value="${ivy.m2repo.url}/org/apache/ivy/ivy"/>
  <property name="ivy.jar.url"         value="${ivy.repo.url}/${ivy.install.version}/ivy-${ivy.install.version}.jar"/>

  <available property="ivy.jar.file.exists" file="${ivy.jar.file}"/>
  <property name="retrieve.dependencies"    value="true" />
  <property name="optional.dependencies"    value="false" />

  <condition property="dont.download.ivy">
    <!-- Set prop to stop Ivy download if asked not to retrieve
    the dependencies, or the ivy jar is already present -->
    <or>
      <isfalse value="${retrieve.dependencies}"/>
      <and>
        <isset property="ivy.jar.file.exists"/>
        <istrue value="${ivy.jar.file.exists}"/>
      </and>
    </or>
  </condition>

  <condition property="ivy.dont.retrieve">
    <isfalse value="${retrieve.dependencies}"/>
  </condition>
  <condition property="ivy.configured">
    <isfalse value="${retrieve.dependencies}"/>
  </condition>
  <condition property="ivy.retrieve.optional">
    <and>
      <istrue value="${retrieve.dependencies}"/>
      <or>
        <istrue value="${optional.dependencies}"/>
        <istrue value="${optional}"/>
      </or>
    </and>
  </condition>


  <macrodef name="indirect">
    <attribute name="name"/>
    <attribute name="variable"/>
    <sequential>
      <property name="@{name}" value="${@{variable}}"/>
    </sequential>
  </macrodef>

  <macrodef name="echo-path">
    <attribute name="refid"/>
    <sequential>
      <property name="@{refid}" refid="@{refid}"/>
      <echo message="@{refid} = ${@{refid}}"/>
      <pathconvert property="@{refid}.pretty"
                   refid="@{refid}"
                   pathsep="${line.separator}    |-- "/>
      <echo message="@{refid}.pretty =${line.separator}    |-- ${@{refid}.pretty}"/>
    </sequential>
  </macrodef>

  <macrodef name="echo-prop">
    <attribute name="name"/>
    <sequential>
      <echo message="@{name} = ${@{name}}"/>
    </sequential>
  </macrodef>

  <macrodef name="findSubProjects">
	<attribute name="dir"/>
        <attribute name="name"/>
	<attribute name="excludes" default=""/>
	  
	<sequential>
	  <dirset id="@{dir}.refid" dir="@{dir}" excludes="@{excludes}">
	   <present targetdir="@{dir}">
        	 <mapper type="glob" from="*" to="*/build.xml" />
	   </present>
	  </dirset>

	  <pathconvert property="@{name}"
        	       refid="@{dir}.refid"
	               pathsep=" ">
		<map from="${project.root}/" to=""/>
          </pathconvert>
 	</sequential>
  </macrodef>

  <macrodef name="jython">
    <attribute name="path"/>
    <element name="args"/>
    <sequential>
      <java jar="${project.root}/${jython}" fork="true" failonerror="true">
        <arg value="-Dpython.path=@{path}"/>
        <args/>
      </java>
    </sequential>
  </macrodef>

  <mkdir dir="${tasks.classes}"/>
  <javac source="${java.source}" target="${java.target}" srcdir="${tasks.src}" destdir="${tasks.classes}"  classpath="${java.class.path}" includeantruntime="false">
    <compilerarg line="${javac.compiler.args}"/>
  </javac>


  <typedef name="propertymapper" classname="org.apache.qpid.tasks.PropertyMapper"
             classpath="${tasks.classes}"/>

  <taskdef name="map" classname="org.apache.qpid.tasks.Map"
           classpath="${tasks.classes}"/>
  <taskdef name="foreach" classname="org.apache.qpid.tasks.Foreach"
           classpath="${tasks.classes}"/>

  <macrodef name="copylist">
    <attribute name="todir"/>
    <attribute name="dir"/>
    <attribute name="files"/>
    <sequential>
      <foreach property="file" list="@{files}">
        <copy todir="@{todir}" flatten="true">
          <fileset dir="@{dir}" includes="${file}"/>
        </copy>
      </foreach>
    </sequential>
  </macrodef>

  <macrodef name="copytofile">
    <attribute name="tofile"/>
    <attribute name="dir"/>
    <attribute name="include"/>
    <attribute name="failOnError" default="false"/>
    <sequential>
      <copy tofile="@{tofile}" failOnError="@{failOnError}">
        <fileset dir="@{dir}">
          <include name="@{include}"/>
        </fileset>
      </copy>
    </sequential>
  </macrodef>


  <target name="clean-tasks">
    <delete dir="${tasks.classes}"/>
  </target>

  <target name="clean-results">
    <delete dir="${build.results}"/>
  </target>

  <target name="report" description="generate test report">
    <delete dir="${build.report}"/>
    <mkdir dir="${build.report}"/>
    <junitreport todir="${build.report}">
      <fileset dir="${build.results}">
        <include name="**/TEST-*.xml"/>
      </fileset>
      <report todir="${build.report}"/>
    </junitreport>
  </target>

  <target name="cobertura-init">
    <taskdef classpathref="cobertura.classpath" resource="tasks.properties" />
  </target>

  <target name="findbugs-init">

    <mkdir dir="${findbugs.dir}"/>
    <!-- Hack to make the FindBugs jars retrieved using Ivy work with the Ant Task,
    because it expects certain jar names without versions like in the .zip release -->
    <copytofile tofile="${findbugs.dir}/findbugs.jar" dir="${findbugs.dir}" include="findbugs-?.*.*.jar"/>
    <copytofile tofile="${findbugs.dir}/findbugs-ant.jar" dir="${findbugs.dir}" include="findbugs-ant-?.*.*.jar"/>
    <copytofile tofile="${findbugs.dir}/bcel.jar" dir="${findbugs.dir}" include="bcel-*.jar"/>
    <copytofile tofile="${findbugs.dir}/annotations.jar" dir="${findbugs.dir}" include="annotations-?.*.*.jar"/>
    <copytofile tofile="${findbugs.dir}/jsr305.jar" dir="${findbugs.dir}" include="jsr305-*.jar"/>
    <copytofile tofile="${findbugs.dir}/jFormatString.jar" dir="${findbugs.dir}" include="jFormatString-*.jar"/>

    <available file="${findbugs.dir}/findbugs-ant.jar" property="findbugs-ant.jar.present"/>
    <fail unless="findbugs-ant.jar.present" message="FindBugs was not found. Please add -Doptional.dependencies=true to your build command to retrieve FindBugs"/>

    <taskdef name="findbugs" classname="edu.umd.cs.findbugs.anttask.FindBugsTask" classpath="${findbugs.dir}/findbugs-ant.jar"/>
  </target>


  <!-- targets for downloading ivy and retrieving dependencies -->
  <target name="retrieve-dependencies" depends="load-ivy, configure-ivy, retrieve-optional-dependencies" unless="${ivy.dont.retrieve}">
    <echo message="Resolving and retrieving dependencies..."/>
    <ivy:resolve type="jar,bundle,war" file="${project.root}/ivy.retrieve.xml" conf="required"/>
    <ivy:retrieve type="jar,bundle,war" conf="required" sync="true"
        pattern="${project.root}/lib/required/[artifact]-[revision].[ext]" />
  </target>

  <target name="retrieve-optional-dependencies" if="${ivy.retrieve.optional}">
    <echo message="Resolving and retrieving optional dependencies..."/>
    <ivy:resolve type="jar" file="${project.root}/ivy.retrieve.xml" conf="findbugs"/>
    <ivy:retrieve type="jar" conf="findbugs" sync="true"
        pattern="${findbugs.dir}/[artifact]-[revision].[ext]" />

    <ivy:resolve type="jar" file="${project.root}/ivy.retrieve.xml" conf="cobertura"/>
    <ivy:retrieve type="jar" conf="cobertura" sync="true"
        pattern="${cobertura.dir}/[artifact]-[revision].[ext]" />
  </target>


  <target name="configure-ivy" unless="${ivy.configured}">
    <ivy:configure file="${project.root}/ivysettings.retrieve.xml" override="true"/>
    <property name="ivy.configured" value="true" />
  </target>

  <target name="load-ivy" depends="download-ivy" unless="${ivy.dont.retrieve}">
    <!-- Try to load Ivy from local ivy dir, in case the user has not already dropped it into
         Ant's lib dir (note that the latter copy will always take precedence). Won't
         fail so long as Ivy is in at least one of the locations. -->
    <mkdir dir="${ivy.jar.dir}"/>
    <path id="ivy.lib.path">
        <fileset dir="${ivy.jar.dir}" includes="*.jar"/>
    </path>
    <taskdef resource="org/apache/ivy/ant/antlib.xml"
              uri="antlib:org.apache.ivy.ant" classpathref="ivy.lib.path"/>
  </target>

  <target name="download-ivy" unless="${dont.download.ivy}">
    <mkdir dir="${ivy.jar.dir}"/>
    <echo message="Downloading ivy..."/>
    <get src="${ivy.jar.url}" dest="${ivy.jar.file}" usetimestamp="true"/>
  </target>


  <target name="help" description="display detailed build documentation">
    <echo>
  ant build

    This target compiles all sources, creates java archives, and
    copies scripts and configurations into the build directory:

      ${build}

    The build directory is treated as a live distro which this target
    will incrementally update. Developers can put the build/bin
    directory into their path in order to run any scripts or code
    directly out of the live build:

      ${build.bin}

  ant test [ -Dtest=&lt;pattern&gt; ] [-Dprofile=&lt;profile&gt;] [ report ]

    Execute unit tests and place the output in the build results
    directory:

      ${build.results}

    All test output will be redirected to a file of the form:

      TEST-&lt;class&gt;.txt

    The same output is available using an xml file format:

      TEST-&lt;class&gt;.xml

    The test system property may be used to restrict the number of
    tests run by a given invocation of the ant test target. The
    following command will run just the MongooseTest test case:

      ant test -Dtest=MongooseTest

    In addition, patterns may be used to specify more than one test.
    The following command will run both the MongooseTest and GooseTest
    test cases:

      ant test -Dtest=*ooseTest

    If no test property is specified, the "ant test" target will
    default to running all available tests for the project or module
    depending on the current working directory.

    Test Reports

    It can be useful to append the report target in order to
    generate an html summary of the tests that were just run. The
    following command will run both the MongooseTest and GooseTest
    test cases and generate an html summary of the results:

      ant test -Dtest=*ooseTest report

    See the documentation for the "ant report" target for more details
    on the generated report.

    Test Profiles

    There are a number of profiles defined for running the test suite.
    These test profiles define how the test should be run. If the test
    profile is not specified then 'default.testprofile' is utilised.
    This runs the system tests against the Java InVM broker. Additional
    test profiles exists as follows:

        cpp : Runs against the built cpp tree broker.

  ant report

    The report target will generate an html summary of the current
    test results into the report directory:

      ${build.report}

    The report target will operate on all results currently in the
    build results directory. Results are not cleared unless the clean
    target is used. This means that several consecutive test runs can
    be summarized into a single report. The following commands will
    produce a report summarizing both the MongooseTest and GooseTest
    test cases:

      ant test -Dtest=MongooseTest
      ...
      ant test -Dtest=GooseTest
      ...
      ant report

  ant release

    The release target generates binary distribution archives and
    places them into the release directory:

      ${release}

  ant release-bin
    
    The release-bin target generates binary distribution archives for 
    modules that have a specific binary package configured.
    To enable for other modules create a target "release-bin" that 
    depends on target "release-bin-tasks". The output is placed in: 

      ${module.release}

  ant clean

    The clean target removes build artifacts. When invoked from the
    project root this target will remove the entire project build and
    release directories:

      ${build}
        and
      ${release}
	and
      ${module.release}

    When invoked from a specific module, the clean target will delete
    that modules build root from underneath the project build root:

      ${build}/&lt;module&gt;

  ant clean-results

    The clean-results target removes all test output from the test
    results directory:

      ${build.results}

  ant eclipse

    Generates project and classpath files for the Eclispe IDE.  Requires that
    the Ant Eclipse task (http://ant-eclipse.sourceforge.net/) has been installed
    in $ANT_HOME/lib.

    The following system properties will be passed to the task. These can be usefully
    overridden from the command line.

    eclipse.updatealways - forces Eclipse files to be regenerated even if they are newer then the build.xml (default ${eclipse.updatealways}).
    eclipse.container - controls the Eclipse container (default ${eclipse.container}).
    eclipse.compilercompliance" - controls the Eclipse compiler compliance (default ${eclipse.compilercompliance}).

    </echo>
  </target>

</project>
