<?xml version="1.0"?>
<project name="maven"  default="ready-to-run-maven">

  <description>
    A file to import to set up Ant for running maven.
    
    it creates a presetdef extension of java that is set up for running maven,
    just add new new commands with nested arg elements, tweak the JVM with jvmarg.
    See the java command for all that can be used.
  </description>

  <target name="ready-to-run-maven"
    depends="init-maven"
    description="move the build into a state where a maven presetdef is defined"
    />
  
    <!-- override point -->
  <target name="ready-to-init-maven" />
  
  <target name="init-maven" depends="ready-to-init-maven">
    <property environment="env" />
  
    <!-- set up maven home -->
    <!-- if maven.home is predefined, it wins -->
    <condition property="maven.home" 
      value="${env.MAVEN_HOME}">
      <and>
        <isset property="env.MAVEN_HOME"/>
        <available file="${env.MAVEN_HOME}" type="dir"/>
        <available file="${env.MAVEN_HOME}" type="dir"/>
      </and>
    </condition>

    <condition property="maven.home" 
      value="/opt/maven">
      <and>
        <available file="/opt/maven" type="dir"/>
        <available file="/opt/maven/lib" type="dir"/>
      </and>
    </condition>
    <condition property="maven.home" 
      value="${user.home}/maven">
      <and>
        <available file="${user.home}/maven" type="dir"/>
        <available file="${user.home}/maven/lib" type="dir"/>
      </and>
    </condition>

    <!-- validate the value -->
    <fail>
      <condition><not><isset property="maven.home"/></not></condition>
      Unable to locate maven. It is not in the well-known locations, and
      there is no property maven.home defined. 
      
      Set maven.home to the base dir of your maven installation.
    </fail>
    <fail>
      <condition>
        <not>
          <available file="${maven.home}" type="dir"/>
        </not>
        </condition>
      There is no directory ${maven.home}
    </fail>
    <fail>
      <condition>
        <not>
          <available file="${maven.home}/lib" type="dir"/>
        </not>
        </condition>
      There is no directory ${maven.home}/lib
    </fail>

    
    <!-- Work out what the root of the JDK is. One problem here
        is that java.home points to the JRE you are using, not the base
        of the JDK. On windows this can be under Program Files somewhere-->
    <condition property="tools.jar" 
      value="${jdk.home}/../lib/tools.jar">
      <and>
        <isset property="jdk.home"/>
        <available file="${jdk.home}/lib/tools.jar" type="file"/>
      </and>
    </condition>

    <condition property="tools.jar" 
      value="${java.home}/../lib/tools.jar">
      <and><isset property="java.home"/>
        <available file="${java.home}/lib/tools.jar" type="file"/>
      </and>
    </condition>
    
    <condition property="tools.jar" 
      value="${env.JAVA_HOME}/lib/tools.jar">
      <and><isset property="env.JAVA_HOME"/>
        <available file="${env.JAVA_HOME}/lib/tools.jar" type="file"/>
      </and>
    </condition>
    <condition property="tools.jar" 
      value="${env.JDK_HOME}/lib/tools.jar">
      <and><isset property="env.JDK_HOME"/>
        <available file="${env.JDK_HOME}/lib/tools.jar" type="file"/>
      </and>
    </condition>
    <fail>
      <condition>
        <or>
          <not><isset property="tools.jar" /></not>
          <not><available file="${tools.jar}" type="file"/></not>
        </or>
      </condition>
      Unable to locate tools.jar, which contains the Java compiler and other needed
      tools.
      
      Please set the property jdk.home to point to the root directory of the JDK, 
      the environment variables JDK_HOME or JAVA_HOME to the same location, or tools.jar
      to the absolute location of tools.jar
      
      For reference
      tools.jar  = ${tools.jar}
      java.home  = ${java.home}
      JDK_HOME   = ${env.JDK_HOME}
      JAVA_HOME  = ${env.JAVA_HOME}
      
    </fail>
    <presetdef name="maven">
      <java
        classname="com.werken.forehead.Forehead"
        failonerror="true"
        fork="true"
        maxmemory="256m"
      >
        <classpath>
          <fileset dir="${maven.home}/lib" includes="forehead-*.jar" />
        </classpath>
        <sysproperty key="maven.home" value="${maven.home}" />
        <sysproperty key="tools.jar" value="${tools.jar}" />
        <sysproperty key="forehead.conf.file" 
          file="${maven.home}/bin/forehead.conf" />
      </java>
    </presetdef>
  </target>

  <target name="run-maven" depends="ready-to-run-maven">
    <maven/>
  </target>
  

</project>

