<?xml version='1.0'?>

<project name='Submission Manager' default='jar' basedir='.' xmlns:ivy="antlib:org.apache.ivy.ant">

  <property environment='env' />
  <property file='ant.properties' />

  <target name='clean'
          description='Clears all generated files, including build directories, distributables, and documentation.'>
    <delete dir='${dir.build}' />
    <delete dir='${dir.deploy}' />
  </target>

  <path id='compile.classpath'>
    <fileset dir='${dir.lib}'>
      <include name='*.jar'/>
    </fileset>
    <fileset dir='${dir.groovy}/embeddable/'>
      <include name='*.jar'/>
    </fileset>
  </path>

  <taskdef name='groovyc' classname='org.codehaus.groovy.ant.Groovyc'
                  classpathref='compile.classpath'/>
  <target name='compile'
          depends='init-ivy, resolve'
          description='Compiles the application and arranges resources into a web archive hierarchy.'>
    <mkdir dir='${dir.build}' />

    <groovyc destdir='${dir.build}'>
      <src>
        <path path="${dir.src.app}" />
      </src>
      <classpath>
        <path refid='compile.classpath'/>
      </classpath>
      <javac source='1.6' debug='on' />
      <include name='**'/>
    </groovyc>

  </target>

  <target name='jar' depends='compile'
    description='Builds jar archives with all compiled classes for application.'>

    <mkdir dir='${dir.deploy}' />
    <delete>
      <fileset dir='${dir.deploy}' includes='*.jar'/>
    </delete>

    <mkdir dir='${dir.build}/META-INF/lib' />
    <copy todir='${dir.build}/META-INF/lib'>
      <fileset dir='${dir.lib}'>
        <include name='cxf*' />
        <include name='groovyws*' />
        <include name='jaxb*' />
        <include name='jaxrpc*' />
        <include name='jdom*' />
        <include name='jtds*' />
        <include name='XmlSchema*' />
        <include name='wsdl4j*' />
      </fileset>
      <fileset dir='${dir.groovy}/embeddable/'>
        <include name='*.jar'/>
      </fileset>
    </copy>

    <jar destfile='${dir.deploy}/${app.jar}' basedir='${dir.build}'
         includes='ucp/**' excludes='**/*Test*'>
      <metainf dir='${dir.build}/META-INF' />
      <file file='${dir.config}/atlassian-plugin.xml' />
      <fileset dir='${dir.src}'>
        <include name='templates/**' />
      </fileset>
    </jar>

    <delete dir='${dir.build}/META-INF' />

  </target>

  <path id='test.classpath'>
    <pathelement path='${dir.build}' />
    <fileset dir='${dir.lib}'>
      <include name='*.jar' />
    </fileset>
    <fileset dir='${dir.groovy}/embeddable/'>
      <include name='*.jar'/>
    </fileset>
  </path>

  <target name='test' depends='compile'
    description='Executes Unit Test cases in the test package tree.'>

    <javac srcdir='${dir.src.test}' destdir='${dir.build}' includeantruntime='true'>
      <classpath>
        <path refid='test.classpath' />
      </classpath>
      <include name='ucp/**'/>
    </javac>

    <junit printsummary='off' fork='true' dir='.' forkmode='once' maxmemory='512M'>
      <formatter type='plain' usefile='false' />
      <sysproperty key="ucp.books.submgr.config.dir" value="${dir.config}" />
      <classpath refid='test.classpath' />
      <batchtest>
        <fileset dir='${dir.build}' includes='**/*Test*' />
      </batchtest>
    </junit>

  </target>

  <!-- automatically configure download and install ivy if required. -->
  <condition property='ivy.home' value='${env.IVY_HOME}'>
    <isset property='env.IVY_HOME' />
  </condition>

  <target name='download-ivy' unless='offline'>
    <mkdir dir='${ivy.jar.dir}'/>
    <get src='http://repo2.maven.org/maven2/org/apache/ivy/ivy/${ivy.install.version}/ivy-${ivy.install.version}.jar'
         dest='${ivy.jar.file}' usetimestamp='true'/>
  </target>

  <target name='init-ivy' depends='download-ivy'>
    <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="resolve" description="--> retrieve dependencies with ivy">
    <ivy:settings file="${dir.config}/ivysettings.xml" />
    <ivy:retrieve />
  </target>

</project>
