<!--

                            ***********************************
                               Share Extensions build script  
                            ***********************************
    
    Author: Will Abson
    
    Provides a set of targets for building extensions to Alfresco Share in ZIP, ACP and JAR archive 
    formats. Since Alfresco 3.3, JAR is the recommended package structure for all simple extensions.
    For more complex extensions that add additional Java libraries or Spring configuration, ACP
    should be used.
    
    The following file structure is required in your project directory
    
        /build.xml - This file
        /config - All web-tier configuration files, e.g. web scripts and Surf configuration
        /web - All static resource files, e.g. CSS, JS
        
    For building AMP files, the files module.proerties and file-mapping.properties are also required
    in the project root directory.
    
    During the build process, temporary 'build' and 'dist' directories will be created in the base 
    project directory. These may be removed at any time using the 'clean' target.

-->
<project basedir="." default="dist-jar" name="Share Example Dashlets">

	<!-- Allow override properties -->
    <property file="build.properties" />
    <property file="${user.home}/build.properties" />
	
	<!-- Property default values. May be overridden using above files or via command-line args -->
    <property name="jar.name" value="share-extension.jar" />
    <property name="zip.name" value="share-extension.zip" />
    <property name="amp.name" value="share-extension.amp" />
    <property name="config.includes" value="**/*.*" />
    <property name="config.excludes" value="" />
    <property name="build.res.includes" value="**/*.*" />
    <property name="build.res.excludes" value="" />
	
	<!-- Additional property values. Generally should not be overridden -->
    <property name="config.dir" value="${basedir}/config" />
    <property name="res.dir" value="${basedir}/web" />
    <property name="build.dir" value="${basedir}/build" />
    <property name="build.jar.dir" value="${build.dir}/jar" />
    <property name="build.zip.dir" value="${build.dir}/war" />
    <property name="build.amp.dir" value="${build.dir}/amp" />
    <property name="dist.dir" value="${basedir}/dist" />
    <property name="amp.file.properties" value="module.properties" />
    <property name="amp.file.mappings" value="file-mapping.properties" />

	<!-- Clean out the build and distribution directories -->
	<target name="clean">
		<delete dir="${build.dir}" />
		<delete dir="${dist.dir}" />
	</target>
	
	<!-- Create required prerequisite directory structure -->
	<target name="prepare">
	    <mkdir dir="${build.dir}" />
		<mkdir dir="${dist.dir}" />
	</target>
	
	<!--
        Assemble the configuration and resource files in a JAR file structure. This mechanism 
        was introduced in Alfresco 3.3 and allows Share extensions containing web scripts, Surf 
        configuration and static assets to be added to the servlet container as shared libraries.
        
        For more complex extensions that add additional Java libraries or Spring configuration, ACP
        should be used.
	-->
	<target name="build-jar">
        <mkdir dir="${build.jar.dir}" />
		<copy todir="${build.jar.dir}">
			<fileset dir="${config.dir}" includes="${config.includes}" excludes="${config.excludes}" />
		</copy>
		<mkdir dir="${build.jar.dir}/META-INF" />
		<copy todir="${build.jar.dir}/META-INF">
			<fileset dir="${res.dir}" includes="${build.res.includes}" excludes="${build.res.excludes}" />
		</copy>
	</target>
	
	<!-- Assemble the JAR file -->
    <target name="dist-jar" depends="clean, prepare, build-jar">
    	<jar destfile="${dist.dir}/${jar.name}">
    		<fileset dir="${build.jar.dir}" />
    	</jar>
	</target>
    
    <!--
        Assemble the configuration and resource files in a ZIP file structure
        
        This creates a structure which can be deployed in the root directory of an Alfresco Tomcat
        distribution, with the following directories.
        
            /shared/classes/alfresco/web-extension - for all config files
            /webapps/ROOT/share-extension - for web resources
            
        The share-extension directory is not an official location for storing web resources 
        required by extensions but works as an interim measure for versions of Alfresco prior to
        version 3.3, provided that the ROOT webapp is enabled in your Tomcat instance.
        
        In version 3.3 and above the JAR file mechanism is recommended as an alternative.
    -->
    <target name="build-zip">
        <mkdir dir="${build.zip.dir}/shared/classes" />
        <copy todir="${build.zip.dir}/shared/classes">
        	<globmapper from="alfresco/*" to="alfresco/web-extension/*" handledirsep="true" />
            <fileset dir="${config.dir}" includes="${config.includes}" excludes="${config.excludes}" />
        </copy>
        <mkdir dir="${build.zip.dir}/webapps/ROOT/share-extension" />
        <copy todir="${build.zip.dir}/webapps/ROOT/share-extension">
            <fileset dir="${res.dir}" includes="${build.res.includes}" excludes="${build.res.excludes}" />
        </copy>
    </target>
    
    <!-- Assemble the ZIP file -->
    <target name="dist-zip" depends="clean, prepare, build-zip">
        <zip destfile="${dist.dir}/${zip.name}">
            <fileset dir="${build.zip.dir}" />
        </zip>
    </target>
    
    <!--
        Assemble the configuration and resource files in an AMP file structure. The files
        module.properties and file-mapping.properties must be present in the root of the 
        project.
        
        This creates a structure which can be deployed into an exising share.war file using the 
        Alfresco Module Management Tool (MMT). See http://wiki.alfresco.com/wiki/AMP_Files.
        
        This mechanism is compatible with all versions of Alfresco Share and can therefore be 
        used as an alternative to the JAR extension mechanism introduced in version 3.3.
        
        Note that this mechanism will place files directly into the webapp structure when the 
        AMP is deployed, rather than the extension mechanisms used by the JAR and ZIP files that
        ensure files are placed outside the webapp for safety during upgrades, etc.
        
        In this case this should be acceptable since the MMT modifies the WAR file itself 
        rather than just the exploded files, and AMPs can always be re-applied if needed.
        
        In version 3.3 and above the JAR file mechanism is recommended as an alternative for all
        non-complex extensions.
    -->
    <target name="build-amp">
    	
    	<!-- Copy properties files -->
        <copy todir="${build.amp.dir}" file="${amp.file.properties}" failonerror="true" />
        <copy todir="${build.amp.dir}" file="${amp.file.mappings}" failonerror="true" />
    	
    	<!-- Copy config files -->
        <mkdir dir="${build.amp.dir}/config" />
        <copy todir="${build.amp.dir}/config">
            <fileset dir="${config.dir}" includes="${config.includes}" excludes="${config.excludes}" />
        </copy>
    	
    	<!-- Copy resource files -->
        <mkdir dir="${build.amp.dir}/web" />
        <copy todir="${build.amp.dir}/web">
            <fileset dir="${res.dir}" includes="${build.res.includes}" excludes="${build.res.excludes}" />
        </copy>
    </target>
    
    <!-- Assemble the AMP file -->
    <target name="dist-amp" depends="clean, prepare, build-amp">
        <zip destfile="${dist.dir}/${amp.name}">
            <fileset dir="${build.amp.dir}" />
        </zip>
    </target>
	
</project>
