<project name="pdftk-java" default="main"
         xmlns:ivy="antlib:org.apache.ivy.ant"
         xmlns:jacoco="antlib:org.jacoco.ant">

  <presetdef name="javac">
    <javac encoding="UTF-8" includeantruntime="false" debug="true"/>
  </presetdef>
  
  <target name="clean">
    <delete dir="build"/>
  </target>

  <target name="compile">
    <mkdir dir="lib"/>
    <mkdir dir="build/classes"/>
    <javac srcdir="java" destdir="build/classes" source="1.8" target="1.8">
      <classpath>
        <fileset dir="lib" includes="*.jar"/>
      </classpath>
    </javac>
    <copy todir="build/classes">
      <fileset dir="java" includes="**/*.afm"/>
      <fileset dir="java" includes="**/*.txt"/>
      <fileset dir="java" includes="**/*.lng"/>
    </copy>
  </target>

  <target name="jar" depends="compile">
    <mkdir dir="build/jar"/>
    <jar destfile="build/jar/pdftk.jar" basedir="build/classes">
      <manifest>
        <attribute name="Main-Class" value="com.gitlab.pdftk_java.pdftk"/>
      </manifest>
    </jar>
  </target>

  <target name="resolve">
    <ivy:retrieve conf="build"/>
  </target>
  
  <target name="main" depends="clean,resolve,jar" />
  
  <target name="test-compile" depends="compile">
    <mkdir dir="build/test"/>
    <javac srcdir="test" destdir="build/test">
      <classpath>
        <fileset dir="lib" includes="*.jar"/>
        <pathelement path="build/classes"/>
      </classpath>
    </javac>
  </target>

  <target name="test-run" depends="test-compile">
    <jacoco:coverage>
      <junit fork="true" forkmode="once" failureproperty="tests.failed">
        <classpath>
          <fileset dir="lib" includes="*.jar"/>
          <pathelement path="build/classes"/>
          <pathelement path="build/test"/>
        </classpath>
        <formatter type="plain" usefile="false" />
        <batchtest>
          <fileset dir="test" includes="**/*Test.java" />
        </batchtest>
      </junit>
    </jacoco:coverage>
  </target>

  <target name="test-resolve">
    <ivy:retrieve conf="test"/>
  </target>  

  <target name="coverage-report">
    <jacoco:report>
      <executiondata>
        <file file="jacoco.exec"/>
      </executiondata>
      <structure name="pdftk-java">
        <classfiles>
          <fileset dir="build/classes/com"/>
        </classfiles>
        <sourcefiles>
          <fileset dir="java"/>
        </sourcefiles>
      </structure>
      <html destdir="coverage"/>
    </jacoco:report>
  </target>

  <target name="test" depends="test-resolve,test-run,coverage-report">
    <fail if="tests.failed" />
  </target>
  
</project>
