<?xml version="1.0" encoding="UTF-8"?>
<!--
   ** Copyright (c) 2010-2013, KNOPFLERFISH project
   ** All rights reserved.
   **
   ** Redistribution and use in source and binary forms, with or without
   ** modification, are permitted provided that the following conditions
   ** are met:
   **
   ** - Redistributions of source code must retain the above copyright notice,
   **   this list of conditions and the following disclaimer.
   **
   ** - Redistributions in binary form must reproduce the above copyright
   **   notice, this list of conditions and the following disclaimer in
   **   the documentation and/or other materials provided with the
   **   distribution.
   **
   ** - Neither the name of the KNOPFLERFISH project nor the names of its
   **   contributors may be used to endorse or promote products derived
   **   from this software without specific prior written permission.
   **
   ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   ** FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   ** COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   ** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
   ** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
   ** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
   ** STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   ** ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
   ** OF THE POSSIBILITY OF SUCH DAMAGE.
  -->

<project name="mvn_repo" default="all"
         xmlns:artifact="antlib:org.apache.maven.artifact.ant">

  <property name="ant.dir"       location="-- PLEASE FILL IN --"/>
  <property name="out.dir"       location="-- PLEASE FILL IN --"/>
  <property name="mvn2.repo.dir" location="-- PLEASE FILL IN --"/>

  <property name="dependency.management.file" location="-- PLEASE FILL IN --"/>
  <property name="dependency.management.repo.file" location="-- PLEASE FILL IN --"/>

  <!-- The default group id of the artefacts. -->
  <property name="group.id"      value="-- PLEASE FILL IN --"/>

  <!-- Property for the maven deploy plugin. -->
  <property name="updateReleaseInfo" value="true"/>

  <!-- Download and define maven ant tasks -->
  <property name="maven-ant-tasks.version" value="2.1.3"/>
  <property name="maven-ant-tasks.jar"
            value="maven-ant-tasks-${maven-ant-tasks.version}.jar"/>
  <property name="maven-ant-tasks.path"
            location="${ant.dir}/lib/${maven-ant-tasks.jar}"/>
  <path id="maven-ant-tasks.classpath"
        path="${maven-ant-tasks.path}" />
  <available classname="org.apache.maven.artifact.ant.WritePomTask"
             classpathref="maven-ant-tasks.classpath"
             property="maven-ant-tasks.present"/>

  <target name="init" depends="install_maven-ant-tasks">
    <typedef resource="org/apache/maven/artifact/ant/antlib.xml"
             uri="antlib:org.apache.maven.artifact.ant"
             classpathref="maven-ant-tasks.classpath" />
  </target>

  <target name="install_maven-ant-tasks" unless="maven-ant-tasks.present">
    <get src="http://repo1.maven.org/maven2/org/apache/maven/maven-ant-tasks/2.1.3/maven-ant-tasks-${maven-ant-tasks.version}.jar"
         dest="${maven-ant-tasks.path}"/>
  </target>


  <macrodef name="mvn_deploy_bundle"
            description="Deploys a bundle archive to a Maven 2 repository">
    <attribute name="projDirName"
               description="Name of directory to place output in."/>
    <attribute name="groupId"
               description="The id of the group that the artifact belongs to."
               default="${group.id}"/>
    <attribute name="artifactId"
               description="Id of the artifact to deploy"/>
    <attribute name="version"
               description="Version of the artifact to deploy"/>
    <attribute name="artifactName"
               description="Name of the artifact to deploy"/>
    <attribute name="description"
               description="Description of the artifact to deploy"
               default=""/>
    <attribute name="orgName"
               description="Name part of the organiztion element in the pom"
               default="Knopflerfish"/>
    <attribute name="orgUrl"
               description="URL part of the organiztion element in the pom"
               default="http://www.knopflerfish.org/"/>
    <attribute name="artifactBundle"
               description="Bundle jar file for the artifact to deploy"/>
    <attribute name="packing"
               description="Artifact packing type"
               default="jar"/>
    <attribute name="settingsFile"
               description="Settings file to load"
               default=""/>
    <element name="dependencies"
             optional="true"
             description="Place holder element for pom dependency."/>
    <element name="licenses"
             optional="true"
             description="Place holder element for pom license."/>
    <element name="source-attachment"
             optional="true"
             description="Place holder element for source artifact attachment."/>
    <element name="javadoc-attachment"
             optional="true"
             description="Place holder element for javadoc artifact attachment."/>
    <sequential>
      <mkdir dir="${mvn2.repo.dir}"/>
      <mkdir dir="${out.dir}/@{projDirName}"/>

      <artifact:pom id="@{artifactId}-pom"
                    groupId="@{groupId}"
                    artifactId="@{artifactId}"
                    version="@{version}"
                    packaging="@{packing}"
                    name="@{artifactName}"
                    description="@{description}">
        <organization name="@{orgName}" url="@{orgUrl}"/>
        <licenses/>
        <dependencies/>
      </artifact:pom>

      <artifact:writepom pomRefId="@{artifactId}-pom"
                         file="${out.dir}/@{projDirName}/@{artifactId}-pom.xml"/>

      <artifact:deploy file="@{artifactBundle}">
        <remoteRepository url="file://localhost${mvn2.repo.dir}"/>
        <pom file="${out.dir}/@{projDirName}/@{artifactId}-pom.xml"
             settingsFile="@{settingsFile}">
          <profile id="release-profile" active="true"/>
        </pom>
	<source-attachment/>
	<javadoc-attachment/>
      </artifact:deploy>
    </sequential>
  </macrodef>

  <!-- Targets for bundles will be added after this line -->
  <target name="all"
          description="Deploy all bundles to the Maven 2 repository."
          depends="init">
    <tstamp>
      <format property="tstamp.year" pattern="yyyy" locale="en"/>
    </tstamp>

    <copy file="${dependency.management.file}"
          toFile="${dependency.management.repo.file}"/>
    <copy file="${ant.dir}/html_template/mvn_dep_mgmt.xsl"
          toFile="${dependency.management.repo.file}/../mvn_dep_mgmt.xsl">
      <filterset>
	<filter token="YEAR" value="${tstamp.year}"/>
      </filterset>
    </copy>
  </target>

</project>
