<?xml version="1.0"?>

<project xmlns:j="jelly:core"
         xmlns:ant="jelly:ant"
         xmlns:util="jelly:util">

  <!--==================================================================-->
  <!-- Compile all source code, weaving the Aspects                     -->
  <!--==================================================================-->    
  <goal name="aspectj"
    description="Compile code with AspectJ"
    prereqs="aspectj:compile"/>

  <goal name="aspectj:compile"
    description="Compile code with AspectJ">
  
    <j:if test="${sourcesPresent == 'true'}">

      <ant:taskdef 
        name="ajc" 
        classname="org.aspectj.tools.ant.taskdefs.AjcTask"/>

      <ant:available property="aspectSourcesPresent"
        file="${pom.build.aspectSourceDirectory}"/>

      <ant:uptodate property="aspectj.compile.notRequired" 
        targetfile="${maven.build.dir}/${maven.final.name}.jar">
        <ant:srcfiles dir="${pom.build.sourceDirectory}"/>
        <j:if test="${aspectSourcesPresent == 'true'}">
          <ant:srcfiles dir="${pom.build.aspectSourceDirectory}"/>
        </j:if>
      </ant:uptodate>

      <j:set var="aspectjCompileNotRequired" value="${aspectj.compile.notRequired}"/>
      <j:if test="${aspectjCompileNotRequired == null}">

        <j:set 
          var="ajcEmacssymFlag" 
          value="${maven.aspectj.ajc.option.emacssym}"/>

        <ant:ajc
          destdir="${maven.build.dest}"
          excludes="**/package.html"
          debug="${maven.compile.debug}"
          deprecation="${maven.compile.deprecation}"
          optimize="${maven.compile.optimize}"
          emacssym="${ajcEmacssymFlag}">

          <ant:src>
            <ant:path refid="maven.compile.src.set"/>
              <j:if test="${aspectSourcesPresent == 'true'}">
                <ant:pathelement location="${pom.build.aspectSourceDirectory}"/>
              </j:if>
          </ant:src>
      
          <j:forEach var="sm" items="${pom.build.sourceModifications}">
            <ant:available property="classPresent" classname="${sm.className}"/>
            <j:if test="${classPresent != 'true'}">
              <j:forEach var="exclude" items="${sm.excludes}">
                <ant:exclude name="${exclude}"/>
              </j:forEach>
            </j:if>
          </j:forEach>
      
          <ant:classpath>
            <ant:path refid="maven.dependency.classpath"/>
            <ant:pathelement path="${maven.build.dest}"/>
            <ant:pathelement path="${plugin.getDependencyPath('aspectj:aspectjrt')}"/>
          </ant:classpath>
        </ant:ajc>

      </j:if>

  </j:if>
      
  </goal>

  <goal name="aspectj:ajdoc" description="Javadoc source using ajdoc">

    <j:if test="${sourcesPresent == 'true'}">

      <ant:taskdef name="ajdoc" classname="org.aspectj.tools.ant.taskdefs.Ajdoc" />

      <ant:available property="aspectSourcesPresent"
        file="${pom.build.aspectSourceDirectory}"/>
        
        <!-- retrieve various javadoc plugin vars -->
        <util:tokenize var="javadocVarList" delim=",">
          maven.javadoc.author,maven.javadoc.destdir,
          maven.javadoc.links,maven.javadoc.maxmemory,
          maven.javadoc.private,maven.javadoc.stylesheet,
          maven.javadoc.use,maven.javadoc.version
        </util:tokenize>
    
        <j:forEach var="javadocVar" items="${javadocVarList}">
          <j:set var="javadocVar" value="${javadocVar.trim()}" />
          <j:set var="${javadocVar}"
            value="${pom.getPluginContext('maven-javadoc-plugin').getVariable(javadocVar)}"/>
        </j:forEach>

        <ant:ajdoc packagenames="${pom.package}.*"
          destdir="${maven.javadoc.destdir}"
          author="${maven.javadoc.author}"
          private="${maven.javadoc.private}"
          version="${maven.javadoc.version}"
          use="${maven.javadoc.use}"
          windowtitle="${title}"
          doctitle="${title}"
          bottom="${copyright}"
          stylesheetfile="${maven.javadoc.stylesheet}">

          <j:forEach var="link" items="${links}">
            <ant:link href="${link.trim()}"/>
          </j:forEach>
          <ant:classpath>
            <ant:path refid="maven.dependency.classpath"/>
            <ant:path location="${maven.build.dest}"/>
            <ant:pathelement path="${plugin.getDependencyPath('aspectj:aspectjrt')}"/>
          </ant:classpath>

          <ant:sourcepath>
            <ant:path refid="maven.compile.src.set"/>
            <j:if test="${aspectSourcesPresent == 'true'}">
              <pathelement location="${pom.build.aspectSourceDirectory}"/>
            </j:if>
          </ant:sourcepath>            

        </ant:ajdoc>

    </j:if>

  </goal>

</project>
