<?xml version="1.0"?>
<!-- 
/*
 * Copyright 2001-2004 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 -->


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

  <!--==================================================================-->
  <!-- Default goal: builds sites for all subprojects                   -->
  <!--==================================================================-->    
  <goal name="multiproject"
    description="Run the site goal of all subprojects"
    prereqs="multiproject:site">
  </goal>


  <!--======================================================================-->
  <!-- Runs reactor and puts variable "reactorProjects" into jelly contex   -->
  <!-- This allows to share the work done by reactor in goals               -->
  <!-- which just need the list of project but don't need to run any goal   -->
  <!--======================================================================-->    
  <goal name="multiproject:site-init">
  
    <!-- FIXME: This can't use multiproject:goal as it throws away the projects -->
    <!-- generate site docs -->
    <maven:reactor
      basedir="${maven.multiproject.basedir}"
      banner="Gathering project list"
      includes="${maven.multiproject.includes}"
      excludes="${maven.multiproject.excludes}"
      postProcessing="true"
      ignoreFailures="${maven.multiproject.ignoreFailures}"
    />
    
    <mkdir dir="${maven.gen.docs}"/>
    
    <j:set var="aggregateDir"          value="${maven.multiproject.aggregateDir}"/>
    <j:set var="navType"               value="${maven.multiproject.navigation}"/>
    <j:set var="overviewPageCreate"    value="${maven.multiproject.overviewPage.create}"/>
    <j:set var="overviewPageTitle"     value="${maven.multiproject.overviewPage.title}"/>
    <j:set var="overviewPageLink"      value="${maven.multiproject.overviewPage.link}"/>    
    <j:set var="overviewPageLinkTitle" value="${maven.multiproject.overviewPage.linkTitle}"/> 
    <j:set var="overviewPageText"      value="${maven.multiproject.overviewPage.text}"/> 
  </goal>
  
  
  <!--==================================================================-->
  <!-- Builds sites for all subprojects                                 -->
  <!--==================================================================-->    
  <goal name="multiproject:site"        
        description="Run the site goal of all subprojects">

    <attainGoal name="multiproject:create-nav"/>
    <attainGoal name="multiproject:create-overview-page"/>
    
    <attainGoal name="site" />
    
    <!-- copy or not? -->
    <j:set var="navType" value="${maven.multiproject.navigation}"/>
    <j:if test="${navType == 'aggregate'}">
      <!-- FIXME: This can't use multiproject:goal as it throws away the projects -->
      <!-- generate site docs -->
      <maven:reactor
        basedir="${maven.multiproject.basedir}"
        banner="Generating site for "
        includes="${maven.multiproject.includes}"
        excludes="${maven.multiproject.excludes}"
        postProcessing="true"
        goals="site"
        ignoreFailures="${maven.multiproject.ignoreFailures}"
      />
    
      <!-- copy each project over into ${maven.docs.dest} -->
      <j:forEach var="reactorProject" items="${reactorProjects}">
        <!-- note this is the same name as is generated in the navigation-aggregate.xml -->
        <j:set var="directory" value="${maven.docs.dest}/${maven.multiproject.aggregateDir}${reactorProject.artifactId}"/>
        <mkdir dir="${directory}"/>
        <!--
        ${reactorProject.name} has docs directory
        ${reactorProject.context.getVariable('maven.docs.dest')}
        needing to be copied
        -->
        <j:set var="fromDir" 
          value="${reactorProject.context.getVariable('maven.docs.dest')}"/>
        <copy toDir="${directory}">
          <fileset dir="${fromDir}"/>
        </copy>
      </j:forEach>
    </j:if>

  </goal>

  <!--==================================================================-->
  <!-- Generate HTML files for navigation bar across projects.          -->
  <!--==================================================================-->    
  <goal name="multiproject:create-nav"
        prereqs="multiproject:site-init">
        
        
    <j:forEach var="reactorProject" items="${reactorProjects}">
      <j:if test="${reactorProject.id == pom.id}">
        <fail message="You must exclude ${pom.id} (the top level project) from the subproject set"/>
      </j:if>
    </j:forEach>
    
    <!-- generate navbar for project based on aggregate or independent -->    
    <j:set var="encoding" value="${maven.docs.outputencoding}"/>
    <j:if test="${navType != ''}">
      <echo>Producing ${navType} navigation...</echo> 
      <velocity:merge 
        name="${maven.gen.docs}/navigation.xml"
        basedir="${maven.multiproject.templates}"
        template="navigation-${navType}.xml"
        inputEncoding="${encoding}"
        outputEncoding="${encoding}" 
        />
    </j:if>

    <j:set var="userNav" value="${maven.docs.src}/navigation.xml" />

    <!-- FIXME: Will the xdoc stuff screw this up?? -->
    <util:file var="userNavTemplate" name="${userNav}"/>
    <j:if test="${userNavTemplate.exists()}">
      <echo>Producing user supplied navigation...</echo> 

      <velocity:merge 
        name="${maven.gen.docs}/navigation.xml"
        basedir="${maven.docs.src}"
        template="navigation.xml"
        inputEncoding="${encoding}"
        outputEncoding="${encoding}" 
        />
    </j:if>

  </goal>
  
  <!--==================================================================-->
  <!-- Generate Overview of all projects                                -->
  <!--==================================================================-->    
  <goal name="multiproject:create-overview-page"
        prereqs="multiproject:site-init">

    <j:new var="file" className="java.io.File">
      <j:arg value="${maven.gen.docs}/${overviewPageLink}.xml"/>
    </j:new>
    
    <mkdir dir="${file.parent}"/>
    
    <j:if test="${overviewPageCreate=='true'}">      
      <ant:echo>Creating overview page</ant:echo>
      <j:set var="encoding" value="${maven.docs.outputencoding}"/>
      <velocity:merge 
        name="${maven.gen.docs}/${overviewPageLink}.xml"
        basedir="${maven.multiproject.templates}"
        template="projects-overview.xml"
        inputEncoding="${encoding}"
        outputEncoding="${encoding}" 
        />
    </j:if>
 
  </goal>

  <!--==================================================================-->
  <!-- Run specific sets of goals for all subprojects                   -->
  <!--==================================================================-->
  <goal name="multiproject:goal" 
        description="Run a given goal on all projects">
        
      <maven:param-check value="${goal}" fail="true">
A goal to run must be specified, e.g.
maven -Dgoal=clean multiproject:goal</maven:param-check>

      <maven:reactor
        basedir="${maven.multiproject.basedir}"
        banner="Executing ${goal}"
        includes="${maven.multiproject.includes}"
        excludes="${maven.multiproject.excludes}"
        postProcessing="false"
        goals="${goal}"
        ignoreFailures="${maven.multiproject.ignoreFailures}"
      />
  </goal> 
  
  <!--==================================================================-->
  <!-- Builds all artifacts for all subprojects                         -->
  <!--==================================================================-->
  <goal name="multiproject:artifact"
        description=" Builds all artifacts for all subproject">

    <j:set var="goal" value="multiproject:artifact-callback"/>
    <attainGoal name="multiproject:goal"/>

  </goal>
  
  <goal name="multiproject:artifact-callback">
  
    <ant:echo>Running ${maven.multiproject.type}:${maven.multiproject.type} for ${pom.name}</ant:echo>
    <attainGoal name="${maven.multiproject.type}:${maven.multiproject.type}"/>
    
  </goal>
  
  
  <!--==================================================================-->
  <!-- Run 'artifact':install for all subprojects                       -->
  <!--==================================================================-->  
  <goal name="multiproject:install"
        description="Run 'artifact':install for all subprojects">

    <j:set var="goal" value="multiproject:install-callback"/>
    <attainGoal name="multiproject:goal"/>
    
  </goal>
  
  <goal name="multiproject:install-callback">
  
     <ant:echo>Running ${maven.multiproject.type}:install for ${pom.name}</ant:echo>
     <attainGoal name="${maven.multiproject.type}:install"/>
     
  </goal>
  
  <!--==================================================================-->
  <!-- Run 'artifact':install-snapshot for all subprojects              --> 
  <!--==================================================================-->    
  <goal name="multiproject:install-snapshot"
        description="Run 'artifact':install-snapshot for all subprojects">

    <j:set var="goal" value="multiproject:install-snapshot-callback"/>
    <attainGoal name="multiproject:goal"/>
    
  </goal>
  
  <goal name="multiproject:install-snapshot-callback">
     <ant:echo>Running ${maven.multiproject.type}:install-snapshot for ${pom.name}</ant:echo>
     <attainGoal name="${maven.multiproject.type}:install-snapshot"/>
  </goal>
  
  <!--==================================================================-->
  <!-- Run 'artifact':deploy for all subprojects                        -->
  <!--==================================================================--> 
   <goal name="multiproject:deploy"
         description="Run 'artifact':deploy for all subprojects">

    <j:set var="goal" value="multiproject:deploy-callback"/>
    <attainGoal name="multiproject:goal"/>
    
  </goal>
  
  <goal name="multiproject:deploy-callback">
  
    <ant:echo>Running ${maven.multiproject.type}:deploy for ${pom.name}</ant:echo>
    <attainGoal name="${maven.multiproject.type}:deploy"/>
    
  </goal> 
  
  <!--==================================================================-->
  <!-- Run 'artifact':deploy-snapshot for all subprojects               -->
  <!--==================================================================--> 
  <goal name="multiproject:deploy-snapshot"
        description="Run 'artifact':deploy-snapshot for all subprojects">

    <j:set var="goal" value="multiproject:deploy-snapshot-callback"/>
    <attainGoal name="multiproject:goal"/>
  </goal>
  
  <goal name="multiproject:deploy-snapshot-callback">
  
    <ant:echo>Running ${maven.multiproject.type}:deploy-snapshot for ${pom.name}</ant:echo>
    <attainGoal name="${maven.multiproject.type}:deploy-snapshot"/>
    
  </goal>

  <!--==================================================================-->
  <!-- Maven Report API - Register                                      -->
  <!--==================================================================-->    
  <goal name="maven-multiproject-plugin:register">
  
    <doc:registerReport 
      name="Dependency Convergence" 
      link="dependency-convergence-report"
      pluginName="maven-multiproject-plugin"
      description="Generate report which shows how consistent are the versions of artitfacts between projects."/>
      
  </goal>
  
  
  <!--==================================================================-->
  <!-- Maven Report API - Deregister                                    -->
  <!--==================================================================-->    
  <goal name="maven-multiproject-plugin:deregister">
  
    <doc:deregisterReport name="Multiproject Dependency Convergence Report"/>
    
  </goal>
  
  <!--==================================================================-->
  <!-- Maven Report API - Report                                        -->
  <!--==================================================================-->    
  <goal name="maven-multiproject-plugin:report">
  
    <attainGoal name="multiproject:dependency-convergence-report"/>
    
  </goal>    
    
  <!--==================================================================-->
  <!-- Generate report which shows the harmony of                       -->
  <!--  versions of dependencies which are used in sub-projects         -->
  <!--==================================================================-->    
  <goal name="multiproject:dependency-convergence-report"
        description="Generate report which shows how consistent are the versions of artitfacts between projects" >
    
    <mkdir dir="${maven.gen.docs}"/>

    <!-- FIXME: 2) Needs to be a tag
     | get all projects for template processing
     | -->
    <maven:reactor
      basedir="${maven.multiproject.basedir}"
      banner="Adding dependencies from:"
      includes="${maven.multiproject.includes}"
      excludes="${maven.multiproject.excludes}"
      postProcessing="true"
      ignoreFailures="true"
    />

      <j:useBean var="harmonizer" class="org.apache.maven.multiproject.harmonizer.DependencyHarmonizer"/>
      ${harmonizer.build(reactorProjects)}
      
      <j:set var="encoding" value="${maven.docs.outputencoding}"/>
      <velocity:merge 
        name="${maven.gen.docs}/dependency-convergence-report.xml"
        basedir="${plugin.resources}/templates"
        template="dependency-convergence-report.xml"
        inputEncoding="${encoding}"
        outputEncoding="${encoding}" 
        />
  </goal>
  
  
  <!--==================================================================-->
  <!-- Cleans all subprojects                                           -->
  <!--==================================================================-->  
  <goal name="multiproject:clean"
    description="Clean all subprojects">

    <j:set var="goal" value="clean:clean"/>
    <attainGoal name="multiproject:goal"/>
    
  </goal>
  
  
</project>
