<?xml version="1.0"?>

<!--
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You 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.
 
   $Header:$
 -->

<project name="Beehive/WSM" 
         default="usage" 
         basedir="."
         xmlns:artifact="urn:maven-artifact-ant">

    <property environment="os"/>
    <import file="wsm-imports.xml"/>

    <property name="maven-artifact-ant.jar" location="lib/maven-artifact-ant-2.0.2-dep.jar"/>
    <typedef resource="org/apache/maven/artifact/ant/antlib.xml" uri="urn:maven-artifact-ant">
        <classpath>
            <pathelement location="${maven-artifact-ant.jar}"/>
        </classpath>
    </typedef>

    <artifact:dependencies pathId=".forcedownload.">
        <artifact:pom file="${basedir}/pom.xml"/>
    </artifact:dependencies>

    <property name="wsm.groupid" value="org.apache.beehive"/>
    <property name="wsm.version" value="1.0-SNAPSHOT"/>
    <property name="build.m2.dir" location="build/maven2/org/apache/beehive"/>

    <property name="debug" value="true"/>

    <property name="build.dir" location="build"/>
    <property name="classes.dir" location="${build.dir}/classes"/>
    <property name="jars.dir" location="${build.dir}/jars"/>
    <property name="docs.dir" location="${build.dir}/docs"/>

    <property name="api.dir" location="src/api"/>
    <property name="api.schema.dir" location="src/api/schemas"/>
    <property name="api.classes" location="${classes.dir}/api"/>

    <property name="core.dir" location="src/core"/>
    <property name="core.classes" location="${classes.dir}/core"/>

    <property name="axis.dir" location="src/axis1"/>
    <property name="axis.classes" location="${classes.dir}/axis1"/>

    <property name="webservice-utils-core.dir" location="src/webservice-utils/core"/>
    <property name="webservice-utils-core.classes" location="${classes.dir}/webservice-utils-core"/>

    <property name="webservice-utils-xmlbeans.dir" location="src/webservice-utils/xmlbeans"/>
    <property name="webservice-utils-xmlbeans.classes" location="${classes.dir}/webservice-utils-xmlbeans"/>

    <property name="wsdl.schema.dir" location="src/schema-wsdl"/>
    <property name="wsdl.classes.dir" location="${classes.dir}/wsdltypes"/>

    <property name="wsm.jar" location="${jars.dir}/beehive-wsm.jar"/>
    <property name="wsm-axis1.jar" location="${jars.dir}/beehive-wsm-axis1.jar"/>
    <property name="jsr181.jar" location="${build.dir}/jars/beehive-jsr181.jar"/>

    <path id="core.classpath">
        <fileset refid="axis-wsdl4j.fileset"/>
        <fileset refid="axis-jaxrpc.fileset"/>
        <fileset refid="annogen.fileset"/>

        <path refid="controls.dependency.path"/>
        <path refid="commons-logging.dependency.path"/>
        <path refid="servlet.dependency.path"/>
        <path refid="xbean.dependency.path"/>

        <pathelement location="${jars.dir}/beehive-wsdltypes.jar"/>
        <pathelement location="${api.classes}"/>
        <pathelement location="${core.classes}"/>
        <pathelement location="${webservice-utils-core.classes}"/>
	<pathelement location="${jars.dir}/beehive-jsr181.jar"/>
        <pathelement location="build/classes/glass"/>
    </path>
 
    <path id="axis.classpath">
        <path refid="core.classpath"/>
        <path refid="controls.dependency.path"/>
        <path refid="axis.dependency.path"/>

        <pathelement location="${core.classes}"/>
        <pathelement location="${webservice-utils-xmlbeans.classes}"/>
    </path>
        
    <target name="clean" description="Clean the WSM API and runtime">
        <delete dir="${build.dir}"/>

        <ant dir="test" target="clean" inheritAll="false"/>
    </target>

    <target name="build" description="Build the WSM API and runtime">
        <mkdir dir="${classes.dir}"/>
        <mkdir dir="${jars.dir}"/>

        <echo message="--------------------------------------------------"/>
        <echo message="|      WSM WSDL types build starting             |"/>
        <echo message="--------------------------------------------------"/>

        <mkdir dir="${wsdl.classes.dir}"/>
        <build-schemas srcdir="${wsdl.schema.dir}" destdir="${wsdl.classes.dir}"/>

        <jar jarfile="${jars.dir}/beehive-wsdltypes.jar">
            <fileset dir="${wsdl.classes.dir}"/>
            <manifest>
                <attribute name="Extension-Name" value="Beehive WSM WSDL Types"/>
                <attribute name="Specification-Title" value="Beehive WSM WSDL Types"/>
                <attribute name="Specification-Vendor" value="Apache Software Foundation"/>
                <attribute name="Specification-Version" value="${beehive.version}"/>
                <attribute name="Implementation-Title" value="Beehive WSM WSDL Types"/>
                <attribute name="Implementation-Vendor" value="Apache Software Foundation"/>
                <attribute name="Implementation-Version" value="${beehive.version}"/>
                <attribute name="Beehive-Version" value="${beehive.version}"/>
            </manifest>
        </jar>

        <echo message="--------------------------------------------------"/>
        <echo message="|      WSM WSDL types build ending               |"/>
        <echo message="--------------------------------------------------"/>

        <echo message="--------------------------------------------------"/>
        <echo message="|      JSR 181 API build starting                |"/>
        <echo message="--------------------------------------------------"/>
	
        <mkdir dir="${api.classes}"/>
        <javac destdir="${api.classes}" debug="${debug}" classpathref="core.classpath">
            <src path="${api.dir}"/>
        </javac>

        <jar jarfile="${jsr181.jar}">
            <fileset dir="${api.classes}"/>
            <manifest> 
                <attribute name="Extension-Name" value="JSR 181 API Library"/> 
                <attribute name="Specification-Title" value="Web Services Metadata for the Java Platform"/> 
                <attribute name="Specification-Vendor" value="BEA Systems Inc."/> 
                <attribute name="Specification-Version" value="pre 1.0"/> 
                <attribute name="Implementation-Title" value="Beehive WSM"/> 
                <attribute name="Implementation-Vendor" value="Apache Software Foundation"/> 
                <attribute name="Implementation-Version" value="${beehive.version}"/> 
                <attribute name="Beehive-Version" value="${beehive.version}"/> 
            </manifest> 
        </jar>

        <echo message="--------------------------------------------------"/>
        <echo message="|      JSR 181 API build ending                  |"/>
        <echo message="--------------------------------------------------"/>

        <echo message="--------------------------------------------------"/>
        <echo message="|      WSM Core build starting                   |"/>
        <echo message="--------------------------------------------------"/>

        <mkdir dir="build/classes/glass"/>
        <javac destdir="build/classes/glass" debug="${debug}">
            <src path="src/glass"/>
        </javac>

        <mkdir dir="${webservice-utils-core.classes}"/>
        <javac destdir="${webservice-utils-core.classes}" classpathref="core.classpath" debug="${debug}">
            <src path="${webservice-utils-core.dir}"/>
        </javac>

        <mkdir dir="${core.classes}"/>
        <javac destdir="${core.classes}" classpathref="core.classpath" debug="${debug}">
            <src path="${core.dir}"/>
        </javac>

        <copy todir="${core.classes}" overwrite="true">
            <fileset dir="${core.dir}" includes="**/*.template,**/*.vm,META-INF/**"/>
            <fileset dir="${core.dir}" includes="**/*.properties"/>
            <fileset dir="${core.dir}" includes="org/apache/beehive/wsm/schemas/**"/>
        </copy>
        
        <jar jarfile="${wsm.jar}">
            <fileset dir="${core.classes}"/>
            <fileset dir="${webservice-utils-core.classes}"/>
            <fileset dir="build/classes/glass"/>
            <manifest>
                <attribute name="Extension-Name" value="Beehive WSM Library"/> 
                <attribute name="Specification-Title" value="Beehive WSM Library"/> 
                <attribute name="Specification-Vendor" value="Apache Software Foundation"/> 
                <attribute name="Specification-Version" value="${beehive.version}"/> 
                <attribute name="Implementation-Title" value="Beehive WSM"/> 
                <attribute name="Implementation-Vendor" value="Apache Software Foundation"/> 
                <attribute name="Implementation-Version" value="${beehive.version}"/> 
                <attribute name="Beehive-Version" value="${beehive.version}"/> 
            </manifest> 
        </jar>
        <echo message="--------------------------------------------------"/>
        <echo message="|      WSM Core build ending                     |"/>
        <echo message="--------------------------------------------------"/>

        <echo message="--------------------------------------------------"/>
        <echo message="|      WSM-Axis1 build starting                   |"/>
        <echo message="--------------------------------------------------"/>

        <mkdir dir="${webservice-utils-xmlbeans.classes}"/>
        <javac destdir="${webservice-utils-xmlbeans.classes}" classpathref="axis.classpath" debug="on">
            <src path="${webservice-utils-xmlbeans.dir}"/>
        </javac>

        <mkdir dir="${axis.classes}"/>
        <javac destdir="${axis.classes}" classpathref="axis.classpath" debug="on">
            <src path="${axis.dir}"/>
        </javac>

        <copy todir="${axis.classes}" overwrite="true">
            <fileset dir="${axis.dir}" includes="META-INF/**"/>
        </copy>

        <jar jarfile="${wsm-axis1.jar}">
            <fileset dir="${axis.classes}"/>
            <fileset dir="${webservice-utils-xmlbeans.classes}"/>
            <manifest>
                <attribute name="Extension-Name" value="Beehive WSM support for Axis 1.x"/> 
                <attribute name="Specification-Title" value="Beehive WSM support for Axis 1.x"/> 
                <attribute name="Specification-Vendor" value="Apache Software Foundation"/> 
                <attribute name="Specification-Version" value="${beehive.version}"/> 
                <attribute name="Implementation-Title" value="Beehive WSM support for Axis 1.x"/> 
                <attribute name="Implementation-Vendor" value="Apache Software Foundation"/> 
                <attribute name="Implementation-Version" value="${beehive.version}"/> 
                <attribute name="Beehive-Version" value="${beehive.version}"/> 
            </manifest> 
        </jar>
        <echo message="--------------------------------------------------"/>
        <echo message="|      WSM-Axis1 build ending                     |"/>
        <echo message="--------------------------------------------------"/>
    </target>

    <target name="install" description="Install the JARs in a local Maven repository">
        <m2-install-jar resource="beehive-wsm"/>
        <m2-install-jar resource="beehive-wsdltypes"/>
        <m2-install-jar resource="beehive-jsr181"/>
        <m2-install-jar resource="beehive-wsm-axis1"/>
    </target>

    <target name="build.dist" description="Build a WSM distribution">
        <property name="dist.dir" location="build/apache-beehive-wsm-${wsm.version}"/>
        <mkdir dir="${dist.dir}"/>
  
        <mkdir dir="${dist.dir}/lib"/>

        <copy todir="${dist.dir}" file="NOTICE.txt"/>
        <copy todir="${dist.dir}" file="LICENSE.txt"/>
        <copy tofile="${dist.dir}/README.txt" file="README-dist.txt"/>

        <copy todir="${dist.dir}/lib">
            <fileset dir="build/jars" includes="*.jar"/>
            <fileset refid="controls.fileset"/>
        </copy>

        <ant dir="samples/wsm-samples" target="clean" inheritAll="false"/>     
        <ant dir="samples/wsm-blank" target="clean" inheritAll="false"/> 

        <copy todir="${dist.dir}/samples">
            <fileset dir="samples">
                <include name="wsm-samples/**"/>
                <include name="wsm-blank/**"/>
            </fileset>
        </copy>
    </target>

    <!-- ==================================================================== -->
    <!-- deploy.webservice.runtime -->
    <!-- ==================================================================== -->
    <target name="deploy.webservice.runtime"
            description="Deploy the web services runtime to a fully-quaflified webapp directory specified with the property 'webapp.dir'">

        <!-- todo: this shouldn't depend on having a webapp present -->
        <available property="webapp.dir.available" file="${webapp.dir}" type="dir"/>
        <fail unless="webapp.dir.available" message="Can't find the webapp directory ${webapp.dir}"/>

        <echo message="Deploy WSM to webapp rooted at: ${webapp.dir}"/>

        <!-- copy the required libraries -->
        <deploy-webservice-runtime appdir="${webapp.dir}/WEB-INF/lib"/>

        <copy todir="${webapp.dir}/WEB-INF/lib/" failOnError="true">
            <fileset dir="${jars.dir}" includes="**/*.jar"/>
            <fileset refid="log4j.fileset"/>
            <fileset refid="xbean.fileset"/>
            <fileset refid="jsr173.fileset"/>
            <fileset refid="controls.fileset"/>
            <fileset refid="velocity.fileset"/>
            <fileset refid="commons-discovery.fileset"/>
        </copy>
    </target>
                
    <!-- ==================================================================== -->
    <!-- docs -->
    <!-- ==================================================================== -->
    <target name="docs" description="Generate the WSM API documentation">
        <javadoc destdir="${docs.dir}/apidocs/classref_wsm" 
                 maxmemory="256M" 
                 windowtitle="Apache Beehive Web Service Metadata API Documentation (Version ${wsm.version})" 
                 doctitle="Apache Beehive Web Service Metadata API Documentation (Version ${wsm.version})" 
                 version="true" 
                 author="false" 
                 use="true" 
                 additionalparam="-breakiterator -noqualifier all -notimestamp " 
                 useexternalfile="true" 
                 classpathref="axis.classpath">
            <packageset dir="${core.dir}" defaultexcludes="yes"/>
            <packageset dir="${axis.dir}" defaultexcludes="yes"/>
        </javadoc>
        <fixcrlf srcDir="${docs.dir}/apidocs/classref_wsm" includes="**/*.html"/>
    </target>
    
    <!-- ==================================================================== -->
    <!-- Run the WSM tests                                                    -->
    <!-- ==================================================================== -->
    <target name="test" description="Run the WSM tests">
        <ant dir="test" target="test" inheritAll="false"/>
    </target>

    <macrodef name="m2-install-jar" description="Install a resource in a Maven 2 repository">
        <attribute name="resource"/>
        <sequential>
            <condition property="_cmd" value="mvn.bat">
                <os family="windows"/>
            </condition>
            <condition property="_cmd" value="mvn">
                <not><os family="windows"/></not>
            </condition>

            <exec executable="${_cmd}" failonerror="true">
                <arg line="install:install-file -Dfile=build/jars/@{resource}.jar -DgroupId=${wsm.groupid} -DartifactId=@{resource} -Dversion=${wsm.version} -Dpackaging=jar -DpomFile=src/@{resource}.pom"/>
            </exec>
        </sequential>
    </macrodef>

    <!-- Targets for building a wsm-schema.jar file that contains generated source files.  Useful for building WSM in an IDE environment -->
    <property name="xsd.schema.dir" location="src/schema-wsdl"/>
    <property name="api.schema.dir" location="src/api/schemas"/>
    <property name="schema.jar" location="wsm-schema.jar"/>
    <property name="schema.build.dir" location="build-schema"/>

    <target name="build-schema">
        <echo message="generate"/>
        <build-schemas srcdir="${xsd.schema.dir}" destdir="${schema.build.dir}"/>
        <build-schemas srcdir="${api.schema.dir}" destdir="${schema.build.dir}"/>
        <jar jarfile="${schema.jar}" basedir="${schema.build.dir}"/>
    </target>

    <target name="clean-schema">
        <delete dir="${schema.build.dir}"/>
        <delete file="${schema.jar}"/>
    </target>

    <target name="usage" description="Print the usage for this build.xml">
        <java fork="no" classname="org.apache.tools.ant.Main">
            <arg line="-f ${ant.file} -projecthelp"/>
        </java>
    </target>

</project>
