<project name="glassfish-gmbal" default="dist" basedir=".">
    <description>
GlassFish MBean Annotation Library.
    </description>
  <!-- set global properties for this build -->
  <property name="version" value="3.2.0-b003"/>
  <property name="jar.name" value="gmbal"/>
  <property name="src" location="src"/>
  <property name="src.test" location="test"/>
  <property name="build" location="build/classes"/>
  <property name="build.test" location="build/test-classes"/>
  <property name="test.reports" location="build/test-reports"/>
  <property name="dist"  location="dist"/>
  <property name="javadoc" location="dist/javadoc"/>
  <property name="javac.debug" value="true"/>
  <path id="project.cp">
    <pathelement location="${build}"/>
  </path>
  <path id="test.cp">
    <pathelement location="${build.test}"/>
  </path>
  <target name="init">
    <!-- Create the build directory structure used by compile -->
    <mkdir dir="${build}"/>
  </target>
      
   <path id="build.classpath">
    <path refid="project.cp"/>
    <fileset dir="/usr/share/java">
        <include name="glassfish-management-api.jar"/>
        <include name="glassfish-pfl/pfl-basic.jar"/>
        <include name="glassfish-pfl/pfl-basic-tools.jar"/>
        <include name="glassfish-pfl/pfl-tf.jar"/>
        <include name="glassfish-pfl/pfl-tf-tools.jar"/>
        <include name="ant/ant-junit.jar"/>
        <include name="junit.jar"/>
    </fileset>
   </path> 
  
  <target name="compile" depends="init"
        description="compile the source " >
    <!-- Compile the java code from ${src} into ${build} -->
    <javac srcdir="${src}" 
      classpathref="build.classpath"
      destdir="${build}"
      debug="${javac.debug}"
      source="1.6"
      target="1.6">
    </javac>
  </target>
      
  <macrodef name="jar-and-manifest">
    <attribute name="destfile"/>
    <attribute name="basedir"/>
    <attribute name="excludes" default=""/>
    <attribute name="includes" default=""/>
    <sequential>
      <jar destfile="${dist}/@{destfile}.jar"
        basedir="@{basedir}"
        excludes="@{excludes}"
        includes="@{includes}">
        <manifest>
          <attribute name="Built-By"
            value="${user.name}"/>
          <attribute name="Bundle-Name"
            value="@{destfile}"/>
          <attribute name="Bundle-SymbolicName"
            value="@{destfile}"/>
          <attribute name="Bundle-Version"
            value="${version}"/>
        </manifest>
      </jar>
    </sequential>
  </macrodef>
      
  <target name="dist" depends="compile"
        description="generate the distribution" >
      
    <antcall target="make-bnd-properties"/>
    <antcall target="gen-resource-bundle"/>
    <!-- generate LogStrings.properties ResourceBundle -->
    <copy tofile="${build}/org/glassfish/gmbal/typelib/LogStrings.properties">
      <filelist dir="${build}/org/glassfish/gmbal/typelib">
        <file name="Exceptions.properties"/>
      </filelist>
    </copy>
      
    <!-- Create the distribution directory -->
    <mkdir dir="${dist}"/>
      
    <make-pom
      module="gmbal"
      description="gmbal implementation"/>
      
    <make-pom
      module="gmbal-api-only"
      description="gmbal API"/>
      
  </target>
      
  <target name="jars" depends="dist"
        description="generate osgi manifest" >
      
    <jar-and-manifest
      basedir="${build}"
      destfile="gmbal"
      includes="org/glassfish/gmbal/**/*"/>
      
    <jar-and-manifest
      basedir="${build}"  
      destfile="gmbal-api-only"
      includes="org/glassfish/gmbal/*.class,org/glassfish/gmbal/util/**/"/>
      
  </target>
  
  <target name="osgi" depends="dist"
        description="generate osgi manifest" >
      
    <mkdir dir="${dist}/bundles"/>
      
    <osgi-manifest
      inputfile="gmbal"
      bndfile="gmbal"/>
      
    <osgi-manifest
      inputfile="gmbal-api-only"
      bndfile="gmbal-api-only"/>
      
  </target>
      
  <target name="gen-resource-bundle">
    <!-- generate Exceptions.properties ResourceBundle -->
    <java classname="org.glassfish.pfl.basic.tools.logex.ExceptionResourceGenerator" classpathref="build.classpath" fork="true" dir=".">
      <arg value="-verbose"/>
      <arg value="2"/>
      <arg value="-source"/>
      <arg value="${build}"/>
      <arg value="-destination"/>
      <arg value="${build}"/>
    </java>
  </target>
      
  <target name="make-bnd-properties">
    <delete file="make/bnd.properties"/>
    <echo message="Bundle-Version: ${version}${line.separator}" file="make/bnd.properties"/>
  </target>
      
  <macrodef name="osgi-manifest">
    <attribute name="inputfile"/>
    <attribute name="bndfile"/>
    <sequential>
        <java classpath="/usr/share/java/aqute-bnd/biz.aQute.bnd.jar:/usr/share/java/aqute-bnd/biz.aQute.bndlib.jar" classname="bnd" fork="true" failonerror="true">
          <arg value="build"/>
          <arg value="-classpath"/>
          <arg value="${dist}/@{inputfile}.jar"/>
          <arg value="-output"/>
          <arg value="${dist}/bundles/@{inputfile}.jar"/>
	  <arg value="make/@{bndfile}.bnd"/>
        </java>
    </sequential>
  </macrodef>
      
  <macrodef name="make-pom">
    <attribute name="module"/>
    <attribute name="description"/>
    <sequential>
        <loadfile property="HEADER-@{module}" srcFile="make/header.xml">
            <filterchain>
                <replacetokens>
                    <token key="artifactId" value="@{module}"/>
                    <token key="version" value="${version}"/>
                    <token key="jarDescription" value="@{description}"/>
                    <token key="groupId" value="org.glassfish.gmbal"/>
                </replacetokens>
            </filterchain>
        </loadfile>
        <copy file="make/@{module}.template.xml" tofile="make/@{module}.pom" overwrite="true">
            <filterset>
              <filter token="HEADER" value="${HEADER-@{module}}"/>
              <filter token="version" value="${version}"/>
              <filter token="groupId" value="org.glassfish.gmbal"/>
            </filterset>
        </copy>
    </sequential>
  </macrodef>
  
  <target name="clean"
        description="clean up" >
    <!-- Delete the ${build} and ${dist} directory trees -->
    <delete dir="${build}"/>
    <delete dir="${dist}"/>
    <delete dir="${javadoc}"/>
  </target>
      
  <target name="javadoc">
    <javadoc sourcepath="${src}"
      destdir="${javadoc}"
      additionalparam="-Xdoclint:none"
      source="1.6" charset="ISO-8859-1"
      >
      <packageset dir="${src}"/>
      <classpath>
        <pathelement location="${dist}/${jar.name}.jar"/>
        <pathelement location="${dist}/${jar.name}-api-only.jar"/>
        <path refid="project.cp"/>
        <path refid="build.classpath"/>
      </classpath>
    </javadoc>
  </target>
      
  <target name="compile-tests" 
          depends="compile" 
          description="Compile the test code" 
          unless="test.skip">
    <mkdir dir="${build.test}"/>
    <javac destdir="${build.test}" 
           encoding="UTF-8" 
           nowarn="false" 
           debug="true" 
           optimize="false" 
           deprecation="true" 
           target="1.6" 
           verbose="false" 
           fork="false" 
           source="1.6">
      <src>
        <pathelement location="${src.test}"/>
      </src>
      <classpath>
        <pathelement location="${dist}/${jar.name}.jar"/>
        <path refid="project.cp"/>
        <path refid="build.classpath"/>
      </classpath>
    </javac>
  </target>
      
  <target name="test" 
          depends="compile-tests, junit-missing" 
          unless="junit.skipped" 
          description="Run the test cases">
    <mkdir dir="${test.reports}"/>
    <junit printSummary="yes" haltonerror="true" haltonfailure="true" fork="true" dir=".">
      <sysproperty key="basedir" value="."/>
      <formatter type="xml"/>
      <formatter type="plain" usefile="false"/>
      <classpath>
        <path refid="test.cp"/>
        <pathelement location="${build}"/>
        <pathelement location="${build.test}"/>
        <path refid="project.cp"/>
        <path refid="build.classpath"/>
        <pathelement location="${java.home}/../lib/tools.jar"/>
      </classpath>
      <batchtest todir="${test.reports}" unless="test">
        <fileset dir="${src.test}">
          <include name="**/*Test.java"/>
          <!-- this test fail -->
          <exclude name="org/glassfish/gmbal/typelib/*.java"/>
        </fileset>
      </batchtest>
      <batchtest todir="${test.reports}" if="test">
        <fileset dir="${src.test}">
          <include name="**/${test}.java"/>
          <!-- this test fail -->
          <exclude name="org/glassfish/gmbal/typelib/*.java"/>
        </fileset>
      </batchtest>
    </junit>
  </target>
      
  <target name="test-junit-present">
    <available classname="junit.framework.Test" property="junit.present"/>
  </target>
      
  <target name="test-junit-status" 
          depends="test-junit-present">
    <condition property="junit.missing">
      <and>
        <isfalse value="${junit.present}"/>
        <isfalse value="${test.skip}"/>
      </and>
    </condition>
    <condition property="junit.skipped">
      <or>
        <isfalse value="${junit.present}"/>
        <istrue value="${test.skip}"/>
      </or>
    </condition>
  </target>
      
  <target name="junit-missing" 
          depends="test-junit-status" 
          if="junit.missing">
    <echo>=================================== WARNING ===================================</echo>
    <echo>          JUnit is not present in your CLASSPATH. Tests not executed           </echo>
    <echo>===============================================================================</echo>
  </target>
        
</project>