<project name="MessagePack for Java" default="jar"
    xmlns:ivy="antlib:org.apache.ivy.ant"
    xmlns:mvn="urn:maven-artifact-ant">

  <property name="Name" value="MessagePack"/>
  <property name="name" value="msgpack"/>
  <property name="version" value="0.0.1"/>
  <property name="fullname" value="${name}-${version}"/>
  <property name="year" value="2010"/>

  <!-- Load user's default properties. -->
  <property file="${user.home}/build.properties" />

  <property name="src.dir" value="${basedir}/src/main/java"/>  	
  <property name="java.src.dir" value="${src.dir}/"/>
  <property name="build.dir" value="${basedir}/build"/>
  <property name="lib.dir" value="${basedir}/lib"/>
  <property name="dist.dir" value="${basedir}/dist"/>

  <property name="build.classes" value="${build.dir}/classes"/>
  <property name="build.doc" value="${build.dir}/doc"/>
  <property name="build.javadoc" value="${build.doc}/api/"/>
  <property name="build.javadoc.log" value="${build.dir}/javadoc.log"/>

  <property name="test.count" value="100"/>
  <property name="test.junit.output.format" value="plain"/>
  <property name="test.java.src.dir" value="${basedir}/src/test/java"/>
  <property name="test.java.build.dir" value="${build.dir}/test"/>
  <property name="test.java.classes" value="${test.java.build.dir}/classes"/>
  <property name="test.java.include" value="Test*"/>
 
  <property name="javac.encoding" value="ISO-8859-1"/>
  <property name="javac.debug" value="on"/>
  <property name="javac.optimize" value="on"/>
  <property name="javac.deprecation" value="off"/>
  <property name="javac.version" value="1.6"/>
  <property name="javac.args" value=""/>
  <property name="javac.args.warnings" value="-Xlint:unchecked"/>

  <property name="javadoc.link.java"
	    value="http://java.sun.com/javase/6/docs/api/"/>
  <property name="javadoc.packages" value="org.${name}.*"/>

  <!-- ivy settings -->
  <property name="ivy.version" value="2.1.0"/>
  <property name="ivy.url"
	    value="http://repo2.maven.org/maven2/org/apache/ivy/ivy" />
  <property name="ivy.home" value="${user.home}/.ant" />
  <property name="ivy.lib" value="${build.dir}/lib"/>
  <property name="ivy.test.lib" value="${build.dir}/test/lib"/>
  <property name="mvn.repo"
	    value="https://repository.apache.org/content/repositories/snapshots"/>

  <!-- the normal classpath -->
  <path id="libs">
    <fileset dir="${ivy.lib}">
      <include name="**/*.jar" />
    </fileset>
  </path>
  <path id="java.classpath">
    <pathelement location="${build.classes}"/>
    <fileset dir="${lib.dir}">
      <include name="**/*.jar" />
      <exclude name="**/excluded/" />
    </fileset>
    <fileset dir="${ant.home}/lib">
      <include name="ant.jar" />
    </fileset>
    <path refid="libs" />
  </path>
  <path id="test.libs">
    <fileset dir="${ivy.test.lib}">
      <include name="**/*.jar" />
    </fileset>
  </path>
  <path id="test.java.classpath">
    <pathelement location="${test.java.classes}" />
    <path refid="java.classpath"/>
    <path refid="test.libs"/>
  </path>

  <!-- init & clean -->
  <target name="init">
    <mkdir dir="${build.dir}" />
    <mkdir dir="${lib.dir}" />
    <mkdir dir="${build.classes}" />

    <mkdir dir="${test.java.build.dir}"/>
    <mkdir dir="${test.java.classes}"/>

    <mkdir dir="${ivy.lib}"/>
    <mkdir dir="${ivy.test.lib}"/>
    <condition property="ivy.jar.exists">
      <available file="${lib.dir}/ivy-${ivy.version}.jar"/>
    </condition>
  </target>
  <target name="clean">
    <delete dir="${build.dir}" />
  </target>

  <!-- ivy targets -->
  <target name="ivy-download" unless="ivy.jar.exists" depends="init">
    <delete dir="${lib.dir}"
	    includes="ivy-*.jar" excludes="ivy-${ivy.version}.jar"/>
    <get src="${ivy.url}/${ivy.version}/ivy-${ivy.version}.jar"
         dest="${lib.dir}/ivy-${ivy.version}.jar" usetimestamp="true"/>
  </target>
  <target name="ivy-init" depends="ivy-download" unless="ivy.initialized">
    <taskdef resource="org/apache/ivy/ant/antlib.xml"
             uri="antlib:org.apache.ivy.ant" classpathref="java.classpath"/>
    <!-- ensure that ivy taskdef is only run once, otw ant will error -->
    <property name="ivy.initialized" value="true"/>
  </target>
   
 <target name="ivy-retrieve-build" depends="init,ivy-init">
    <ivy:retrieve type="jar" conf="build"
		  pattern="${ivy.lib}/[artifact]-[revision].[ext]"/>
 </target>
 <target name="ivy-retrieve-test" depends="init,ivy-init">
    <ivy:retrieve type="jar" conf="test"
		  pattern="${ivy.test.lib}/[artifact]-[revision].[ext]"/>
 </target>

  <!-- compiler -->
  <macrodef name="java-compiler">
    <attribute name="dest" default="${build.classes}"/>
    <attribute name="includes" default="**/*.java"/>
    <attribute name="excludes" default=""/>
    <attribute name="classpath" default="java.classpath"/>
    <element name="src" implicit="yes"/>
    <sequential>
      <javac 
	 destdir="@{dest}"
         includes="@{includes}"
         excludes="@{excludes}" 
	 encoding="${javac.encoding}" 
	 debug="${javac.debug}"
	 optimize="${javac.optimize}"
	 target="${javac.version}"
	 source="${javac.version}"
	 deprecation="${javac.deprecation}">
	<compilerarg line="${javac.args} ${javac.args.warnings}" />
	<classpath refid="@{classpath}"/>
        <src />
      </javac>
    </sequential>
  </macrodef>

  <!-- compile -->
  <target name="compile" depends="init,ivy-retrieve-build">
    <java-compiler>
      <src path="${java.src.dir}"/>
    </java-compiler>
  </target>

  <!-- test -->
  <macrodef name="test-runner">
    <attribute name="files.location" />
    <attribute name="tests.pattern" />
    <attribute name="test.dir" default="${test.java.build.dir}" />
    <sequential>
      <junit showoutput="yes"
             printsummary="withOutAndErr"
             haltonfailure="no"
             fork="yes" forkMode="once"
             errorProperty="tests.failed" failureProperty="tests.failed">
        <sysproperty key="test.count" value="${test.count}"/>
        <sysproperty key="test.dir" value="@{test.dir}"/>
        <classpath refid="test.java.classpath"/>
        <formatter type="${test.junit.output.format}"/>
        <batchtest todir="${test.java.build.dir}" unless="testcase">
          <fileset dir="@{files.location}"
		   includes="@{tests.pattern}"
		   excludes="**/${test.java.exclude}.java" />
        </batchtest>
        <batchtest todir="${test.java.build.dir}" if="testcase">
          <fileset dir="@{files.location}" includes="**/${testcase}.java"/>
        </batchtest>
      </junit>
      <fail if="tests.failed">Tests Failed!</fail>
    </sequential>
  </macrodef>
  <target name="compile-test" depends="ivy-retrieve-test,compile">
    <java-compiler dest="${test.java.classes}"
		   classpath="test.java.classpath">
      <src path="${test.java.src.dir}/org" />
    </java-compiler>
  </target>
  <target name="test" depends="init,compile-test">
    <test-runner files.location="${test.java.src.dir}"
                 tests.pattern="**/${test.java.include}.java"/>
  </target>

  <!-- jar -->
  <target name="jar" depends="compile">
    <jar jarfile="${build.dir}/${name}-${version}.jar" basedir="${build.classes}" >
      <manifest>
        <section name="org/${name}">
          <attribute name="Implementation-Title" value="${Name}"/>
          <attribute name="Implementation-Version" value="${version}"/>
        </section>
      </manifest>
    </jar>
  </target>

  <!-- javadoc -->
  <target name="javadoc" depends="compile" description="Generate javadoc">
    <mkdir dir="${build.javadoc}"/>
    <record name="${build.javadoc.log}" action="start"/>
    <javadoc
      Locale="en_US"
      packagenames="org.${org}.${name}.*"
      destdir="${build.javadoc}"
      encoding="UTF-8"
      docencoding="UTF-8"
      author="true"
      version="true"
      use="true"
      windowtitle="${Name} ${version} API"
      doctitle="${Name} ${version} API"
      bottom="Copyright &amp;copy; ${year} The ${Name} Project"
      >
        <packageset dir="${java.src.dir}"/>
        <link href="${javadoc.link.java}"/>
        <classpath >
          <path refid="java.classpath" />
        </classpath>
    </javadoc>
    <record name="${build.javadoc.log}" action="stop"/>
    <condition property="javadoc.warnings">
      <isfileselected file="${build.javadoc.log}">
	<contains text=": warning - "/>
      </isfileselected>
    </condition>
    <fail if="javadoc.warnings">Javadoc warnings!</fail>
  </target>
  <target name="javadoc-jar" depends="javadoc">
    <jar jarfile="${build.dir}/${fullname}-javadoc.jar">
      <fileset dir="${build.javadoc}" includes="**/*"/>
    </jar>
  </target>

  <!-- sources -->
  <target name="source">
    <jar jarfile="${build.dir}/${fullname}-sources.jar">
      <fileset dir="${java.src.dir}" includes="**/*.java"/>
    </jar>
  </target>

  <!-- pom -->
  <target name="pom" depends="ivy-init">
    <ivy:makepom ivyfile="${basedir}/ivy.xml"
		 pomfile="${dist.dir}/${fullname}.pom">
      <mapping conf="default" scope="compile"/>
      <mapping conf="test" scope="test"/>
    </ivy:makepom>
  </target>

  <!-- dist -->
  <target name="dist" depends="jar, pom, source, javadoc-jar"
	  description="Build distribution">
    <mkdir dir="${dist.dir}"/>
    <copy todir="${dist.dir}"> 
      <fileset file="${build.dir}/${fullname}.jar"/>
      <fileset file="${build.dir}/${fullname}-sources.jar"/>
      <fileset file="${build.dir}/${fullname}-javadoc.jar"/>
    </copy>
  </target>

  <!-- maven: install msgpack into local m2 cache -->
  <target name="mvn-install" depends="jar, pom, source, javadoc-jar"
	  description="Installs msgpack to local m2 cache">
    <typedef resource="org/apache/maven/artifact/ant/antlib.xml"
	     uri="urn:maven-artifact-ant"
	     classpathref="java.classpath"/>
    <mvn:pom file="${dist.dir}/${fullname}.pom" id="msgpack"/>
    <mvn:install file="${build.dir}/${fullname}.jar">
      <attach file="${build.dir}/${fullname}-sources.jar"
           classifier="sources" />
      <attach file="${build.dir}/${fullname}-javadoc.jar"
           classifier="javadoc" />
      <pom refid="msgpack"/>
    </mvn:install>
  </target>

  <!-- maven: create local repository into ${basedir}/maven2 -->
  <target name="mvn-deploy" depends="jar, pom, source, javadoc-jar"
     description="Deploys MessagePack to Maven repo.">
    <typedef resource="org/apache/maven/artifact/ant/antlib.xml"
	     uri="urn:maven-artifact-ant"
	     classpathref="java.classpath"/>
    <mvn:pom file="${dist.dir}/${fullname}.pom" id="msgpack"/>
    <mvn:deploy file="${build.dir}/${fullname}.jar">
      <remoteRepository url="file://localhost/${basedir}/maven2/"/>
      <attach file="${build.dir}/${fullname}-sources.jar"
           classifier="sources" />
      <attach file="${build.dir}/${fullname}-javadoc.jar"
           classifier="javadoc" />
      <pom refid="msgpack"/>
    </mvn:deploy>
  </target>

</project>
