Skip to content

Instantly share code, notes, and snippets.

@wabson
Last active December 28, 2015 08:49
Show Gist options
  • Save wabson/7474079 to your computer and use it in GitHub Desktop.
Save wabson/7474079 to your computer and use it in GitHub Desktop.
Share Extras common build scripts - used across all Share Extras projects which are built with Ant. File build.xml should not contain any project-specific code, but this can be placed in project.xml if it is necessary to add new targets or override existing targets.To run the build call `ant` or `ant -f project.xml`. You do not need to supply a …
<!--
***********************************
Share Extras build script
***********************************
Author: Will Abson
Version: 1.6.2
Provides a set of targets for building extensions to Alfresco Share in ZIP, AMP 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, AMP 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
/source/web - All static resource files, e.g. CSS, JS
For building AMP files, a file module.properties is also required in the project's root
directory. A file-mapping.properties file in the same location may optionally be provided.
During the build process, temporary a 'build' and directory will be created in the base project
directory, which may be removed at any time using the 'clean' target.
-->
<project basedir="." default="dist-amp-jar" name="Share Extras Build Script"
xmlns:artifact="antlib:org.apache.maven.artifact.ant">
<!-- Allow override properties -->
<property file="build.properties" />
<property file="${user.home}/build.properties" />
<property file="${user.home}/.ant/build.properties" />
<!-- Property default values. May be overridden using above files or via command-line args -->
<property name="project.name" value="share-extension" />
<property name="project.version" value="1.0" />
<property name="jar.name" value="${project.name}-${project.version}.jar" />
<property name="zip.name" value="${project.name}-${project.version}.zip" />
<property name="amp.name" value="${project.name}-${project.version}.amp" />
<property name="amp.version" value="${project.version}" />
<property name="src.zip.name" value="share-extension-src.zip" />
<property name="config.includes" value="**/*.*" />
<property name="config.excludes" value="" />
<property name="build.res.includes" value="**/*.*" />
<property name="build.res.excludes" value="" />
<property name="build.res.dir" value="webapps/share" />
<property name="build.classes.dir" value="shared/classes" />
<property name="build.lib.dir" value="shared/lib" />
<property name="yuicompress.warn" value="false" />
<!-- Tomcat properties to reload web scripts or the manager webapp -->
<property name="tomcat.url" value="http://localhost:8080" />
<property name="tomcat.repo.url" value="${tomcat.url}" />
<property name="tomcat.share.url" value="${tomcat.url}" />
<!-- Tomcat properties to reload web scripts -->
<property name="webapp.alfresco.path" value="/alfresco" />
<property name="webapp.share.path" value="/share" />
<property name="post.verbose" value="false" />
<property name="repo.admin.username" value="admin" />
<property name="repo.admin.password" value="admin" />
<property name="repo.scripts.index" value="${tomcat.repo.url}${webapp.alfresco.path}/service/index" />
<property name="share.scripts.index" value="${tomcat.share.url}${webapp.share.path}/page/index" />
<!-- Properties to access the Tomcat Manager application -->
<property name="webapp.manager.url" value="${tomcat.url}/manager" />
<property name="webapp.manager.username" value="admin" />
<property name="webapp.manager.password" value="" />
<!-- Paths to local Tomcat instances -->
<property name="tomcat.repo.home" value="${tomcat.home}" />
<property name="tomcat.share.home" value="${tomcat.home}" />
<!-- Maven repo parameters -->
<property name="repository.name" value="share-extras-snapshots" />
<property name="repository.url" value="https://artifacts.alfresco.com/nexus/content/repositories/${repository.name}" />
<!-- Additional property values. Generally should not be overridden -->
<property name="config.dir" value="${basedir}/config" />
<property name="res.dir" value="${basedir}/source/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="${build.dir}/dist" />
<property name="amp.file.properties" value="module.properties" />
<property name="amp.file.mappings" value="file-mapping.properties" />
<!-- Define optional tasks -->
<!--
<taskdef name="reload" classname="org.apache.catalina.ant.ReloadTask" />
<taskdef name="post" classname="net.sf.antcontrib.net.PostTask" />
-->
<!-- Alias targets -->
<target name="build-zip" depends="build-zip-tomcat" />
<target name="dist-zip" depends="dist-zip-tomcat" />
<!-- Additional classpath and task definitions -->
<path id="yuicompressor.classpath">
<fileset dir="lib">
<include name="yuicompressor-2.4.7.jar"/>
<include name="yui-compressor-ant-task-0.5.jar"/>
</fileset>
</path>
<taskdef name="yuicompress" classname="net.noha.tools.ant.yuicompressor.tasks.YuiCompressorTask">
<classpath>
<path refid="yuicompressor.classpath" />
</classpath>
</taskdef>
<path id="ml-ant-http.classpath">
<fileset dir="lib">
<include name="ml-ant-http-1.1.1.jar" />
</fileset>
</path>
<taskdef name="http" classname="org.missinglink.ant.task.http.HttpClientTask">
<classpath>
<path refid="ml-ant-http.classpath" />
</classpath>
</taskdef>
<path id="maven-ant-tasks.classpath" path="lib/maven-ant-tasks-2.1.4-SNAPSHOT.jar" />
<typedef resource="org/apache/maven/artifact/ant/antlib.xml" uri="antlib:org.apache.maven.artifact.ant"
classpathref="maven-ant-tasks.classpath" />
<!-- Pre-flight checks -->
<target name="check-buildfile" description="Check that the correct build file was called">
<condition property="wrong.buildfile">
<and>
<available file="${basedir}/project.xml" />
<not><matches string="${ant.file}" pattern="[/\\]project.xml$" /></not>
</and>
</condition>
</target>
<!-- Main build target definitions -->
<!-- Clean out the build and distribution directories -->
<target name="clean" description="Clean out all build directories" depends="check-buildfile">
<!-- Check the correct file was called -->
<fail if="wrong.buildfile">Please use project.xml to build this project</fail>
<delete dir="${build.dir}" />
<delete dir="${dist.dir}" />
</target>
<!-- Create required prerequisite directory structure -->
<target name="prepare" description="Create initial build structures">
<mkdir dir="${build.dir}" />
<mkdir dir="${dist.dir}" />
</target>
<!-- Build a source ZIP file -->
<target name="dist-src" depends="" description="Build a ZIP file containing the project source code">
<zip destfile="${dist.dir}/${src.zip.name}">
<fileset dir="${basedir}">
<exclude name="build/**" />
</fileset>
</zip>
</target>
<!-- Build-jar sub-tasks - allows us to override these more easily -->
<target name="build-jar-prepare" depends="">
<mkdir dir="${build.jar.dir}" />
</target>
<target name="build-jar-assemble" depends="">
<!-- Copy configuration files, web scripts, etc. directly into the JAR so they appear on the
classpath. -->
<copy todir="${build.jar.dir}" includeEmptyDirs="false">
<fileset dir="${config.dir}" includes="${config.includes}">
<!-- Spring config -->
<exclude name="web-application-config.xml" />
<exclude name="surf-config.xml" />
<exclude name="alfresco/slingshot-application-context.xml" />
<exclude name="alfresco/web-extension/custom-slingshot-application-context.xml" />
<!-- Surf config -->
<exclude name="alfresco/share*-config.xml" />
<exclude name="alfresco/web-extension/share-config-custom.xml" />
<exclude name="share-config-custom.xml" />
<!-- Global excludes -->
<exclude name="${config.excludes}" />
</fileset>
<filterset>
<filter token="VERSION" value="${project.version}"/>
</filterset>
</copy>
<!-- Copy web-tier resources into the JAR. These can then be loaded by browsers via Share's resources
servlet by prefixing their path with '/res' -->
<mkdir dir="${build.jar.dir}/META-INF" />
<copy todir="${build.jar.dir}/META-INF" includeEmptyDirs="false">
<fileset dir="${res.dir}" includes="${build.res.includes}" excludes="${build.res.excludes}" />
</copy>
<!-- Map alfresco/web-extension/share-config-custom.xml to META-INF/share-config-custom.xml in the JAR -->
<copy todir="${build.jar.dir}/META-INF" includeEmptyDirs="false">
<fileset dir="${config.dir}">
<filename name="alfresco/web-extension/share-config-custom.xml" />
</fileset>
<globmapper from="alfresco/web-extension/*.xml" to="*.xml" handledirsep="true" />
</copy>
<copy todir="${build.jar.dir}/META-INF" includeEmptyDirs="false">
<fileset dir="${config.dir}">
<filename name="share-config-custom.xml" />
</fileset>
</copy>
</target>
<target name="build-jar-jscompress" depends="">
<!-- Minify JS -->
<yuicompress fromdir="${res.dir}" todir="${build.jar.dir}/META-INF" excludes="**/*-min.js" warn="${yuicompress.warn}">
<include name="**/*.js" />
</yuicompress>
</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 both repository and
web-tier web scripts, Spring/Surf configuration and static assets to be added to the servlet
container as a single library. For more complex extensions, AMP files should be used.
This target excludes the files custom-slingshot-application-context.xml, share-config-custom.xml
and similar files from being copied into the classpath as these should not generally be included
within JAR files as it could lead to multiple copies of them being present. If found however,
alfresco/web-extension/share-config-custom.xml will be copied into the META-INF directory,
from where it can be loaded in 3.4.
-->
<target name="build-jar" depends="build-jar-prepare,build-jar-assemble,build-jar-jscompress"
description="Assemble configuration and resource files in a JAR file structure">
</target>
<!-- Build the JAR file -->
<target name="dist-jar" depends="clean, prepare, build-jar"
description="Build a JAR file containing configuration and resource files">
<jar destfile="${dist.dir}/${jar.name}">
<fileset dir="${build.jar.dir}" />
</jar>
</target>
<!-- build-zip-tomcat sub-tasks -->
<target name="build-zip-tomcat-assemble">
<!-- Copy web assets -->
<copy todir="${build.zip.dir}/${build.res.dir}">
<fileset dir="${res.dir}" includes="${build.res.includes}" excludes="${build.res.excludes}" />
</copy>
</target>
<target name="build-zip-tomcat-jscompress">
<!-- Minify JS -->
<yuicompress fromdir="${res.dir}" todir="${build.zip.dir}/${build.res.dir}" excludes="**/*-min.js" warn="${yuicompress.warn}">
<include name="**/*.js" />
</yuicompress>
</target>
<!--
Assemble the configuration and resource files in a file structure suitable for deploying
into an existing Tomcat installation, with the following directories.
/shared/classes/alfresco Repository/Share configuration files
/webapps/share Share web resources
By default web resources are placed in directly in the 'share' web application. You
can specify a different directory by overriding the build.res.dir property value. Set
this to 'webapps/ROOT/share-extension' to use the pre-3.3 recommended layout.
The share-extension directory is not an official location for storing web resources
required by extensions but worked as an interim measure for versions of Alfresco prior to
version 3.3, provided that the ROOT webapp is enabled in your Tomcat instance.
By default configuration is placed in directly in Tomcat's shared/classes directory. You
can specify a different directory by overriding the build.classes.dir property value. Set
this to 'webapps/share/WEB-INF/classes' to place files on the classpath inside the webapp.
Unlike build-jar this target does not exclude any particular configuration files from the
file structure, however the hotcopy-tomcat-zip task will err on the side of caution and
will not copy files such as share-config-custom.xml itself into Tomcat.
-->
<target name="build-zip-tomcat" depends="build-zip-prepare,build-zip-config,build-zip-tomcat-assemble,build-zip-tomcat-jscompress"
description="Assemble the configuration and resource files for a Tomcat deployment structure">
</target>
<target name="build-zip-prepare">
<mkdir dir="${build.zip.dir}/${build.classes.dir}" />
<mkdir dir="${build.zip.dir}/${build.res.dir}" />
</target>
<target name="build-zip-config">
<copy todir="${build.zip.dir}/${build.classes.dir}">
<fileset dir="${config.dir}" includes="${config.includes}" excludes="${config.excludes}" />
<filterset>
<filter token="VERSION" value="${project.version}"/>
</filterset>
</copy>
</target>
<!-- Build the Tomcat ZIP file -->
<target name="dist-zip-tomcat" depends="clean, prepare, build-zip-tomcat"
description="Build a ZIP file containing the customisations that can be deployed in an existing Tomcat installation">
<zip destfile="${dist.dir}/${zip.name}">
<fileset dir="${build.zip.dir}" />
</zip>
</target>
<!-- build-zip-tomcat sub-tasks -->
<target name="build-amp-assemble">
<!-- Copy properties files -->
<copy todir="${build.amp.dir}" file="${amp.file.properties}" failonerror="true">
<filterset>
<filter token="VERSION" value="${amp.version}"/>
</filterset>
</copy>
<copy todir="${build.amp.dir}" file="${amp.file.mappings}" failonerror="false" />
<!-- 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}" />
<filterset>
<filter token="VERSION" value="${project.version}"/>
</filterset>
</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>
<target name="build-amp-jscompress">
<!-- Minify JS -->
<yuicompress fromdir="${res.dir}" todir="${build.amp.dir}/web" excludes="**/*-min.js" warn="${yuicompress.warn}">
<include name="**/*.js" />
</yuicompress>
</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" depends="build-amp-assemble,build-amp-jscompress"
description="Assemble the configuration and resource files in an AMP file structure">
</target>
<!-- Build the AMP file -->
<target name="dist-amp" depends="clean, prepare, build-amp" description="Build an AMP file containing all customisations">
<zip destfile="${dist.dir}/${amp.name}">
<fileset dir="${build.amp.dir}" />
</zip>
</target>
<!-- Build an AMP file in non-exploded form -->
<target name="dist-amp-jar" depends="clean, dist-jar" description="Build an AMP file in non-exploded form. The AMP file will contain only a single JAR.">
<!-- Copy properties files -->
<copy todir="${build.amp.dir}" file="${amp.file.properties}" failonerror="true">
<filterset>
<filter token="VERSION" value="${project.version}"/>
</filterset>
</copy>
<copy todir="${build.amp.dir}" file="${amp.file.mappings}" failonerror="false" />
<!-- Copy JAR file -->
<mkdir dir="${build.amp.dir}/lib" />
<copy todir="${build.amp.dir}/lib" file="${dist.dir}/${jar.name}" failonerror="true" />
<!-- Create the AMP -->
<zip destfile="${dist.dir}/${amp.name}">
<fileset dir="${build.amp.dir}" />
</zip>
</target>
<!-- Shared path definition used to copy files into the local Tomcat instance(s) -->
<patternset id="hotcopy-tomcat-zip-patternset">
<!-- Spring config -->
<exclude name="**/classes/web-application-config.xml" />
<exclude name="**/classes/surf-config.xml" />
<exclude name="**/classes/alfresco/slingshot-application-context.xml" />
<exclude name="**/classes/alfresco/web-extension/custom-slingshot-application-context.xml" />
<!-- Surf config -->
<exclude name="**/classes/alfresco/share*-config.xml" />
<exclude name="**/classes/alfresco/web-extension/share-config-custom.xml" />
</patternset>
<!--
Hot copy individual files into a local Tomcat instance.
In version 3.3 and above the JAR file mechanism is recommended for distributing your
customisations, but this target can still be used during development as it allows you to
reload changes without restarting Tomcat.
-->
<target name="hotcopy-tomcat-zip" depends="build-zip-tomcat" description="Hot copy individual files into a local Tomcat instance">
<echo message="Copying repository files" />
<copy todir="${tomcat.repo.home}" includeEmptyDirs="false">
<fileset dir="${build.zip.dir}">
<patternset refid="hotcopy-tomcat-zip-patternset" />
<!-- Exclude static resources -->
<exclude name="${build.res.dir}/**" />
<!-- Exclude web-tier config -->
<exclude name="**/classes/alfresco/site-webscripts/**" />
<exclude name="**/classes/alfresco/site-data/**" />
<exclude name="**/classes/alfresco/web-extension/**" />
<exclude name="**/classes/org/springframework/extensions/surf/**" />
</fileset>
</copy>
<echo message="Copying Share files" />
<copy todir="${tomcat.share.home}" includeEmptyDirs="false">
<fileset dir="${build.zip.dir}">
<patternset refid="hotcopy-tomcat-zip-patternset" />
<!-- Exclude repo-tier web scripts config -->
<exclude name="**/classes/alfresco/extension/**" />
<exclude name="**/classes/alfresco/templates/webscripts/**" />
</fileset>
</copy>
</target>
<!--
Hot copy JAR file into a local Tomcat instance.
-->
<target name="hotcopy-tomcat-jar" depends="dist-jar" description="Hot copy JAR file into a local Tomcat instance">
<mkdir dir="${tomcat.repo.home}/${build.lib.dir}" />
<mkdir dir="${tomcat.share.home}/${build.lib.dir}" />
<copy todir="${tomcat.repo.home}/${build.lib.dir}">
<fileset file="${dist.dir}/${jar.name}" />
</copy>
<copy todir="${tomcat.share.home}/${build.lib.dir}">
<fileset file="${dist.dir}/${jar.name}" />
</copy>
</target>
<!--
Web script reloading from Ant. These tasks use the HTTP task from
http://code.google.com/p/missing-link/.
-->
<target name="reload-webscripts-repo" depends="" description="Reload repository webscripts">
<http url="${repo.scripts.index}"
method="POST"
printrequest="false"
printrequestheaders="false"
printresponse="false"
printresponseheaders="false"
expected="200"
failonunexpected="true">
<credentials username="${repo.admin.username}" password="${repo.admin.password}" />
<query>
<parameter name="reset" value="on" />
</query>
</http>
</target>
<target name="reload-webscripts-share" depends="" description="Reload Share webscripts">
<http url="${share.scripts.index}"
method="POST"
printrequest="false"
printrequestheaders="false"
printresponse="false"
printresponseheaders="false"
expected="200"
failonunexpected="true">
<credentials username="${repo.admin.username}" password="${repo.admin.password}" />
<query>
<parameter name="reset" value="on" />
</query>
</http>
</target>
<!--
Uncomment to enable web application reloading from Ant. These tasks use the optional Tomcat
ant tasks from catalina-ant.jar distributed with Tomcat.
To use these tasks you will need to add catalina-ant.jar to your Ant libs and uncomment the
task definitions at the start of this file.
-->
<!--
<target name="reload-webapp-alfresco" description="Reload alfresco web application" depends="">
<reload
url="${webapp.manager.url}"
username="${webapp.manager.username}"
password="${webapp.manager.password}"
path="${webapp.alfresco.path}"/>
</target>
<target name="reload-webapp-share" description="Reload share web application" depends="">
<reload
url="${webapp.manager.url}"
username="${webapp.manager.username}"
password="${webapp.manager.password}"
path="${webapp.share.path}"/>
</target>
-->
<target name="mvn-install" depends="dist-amp-jar" description="Install to local Maven repo">
<artifact:pom id="mypom" file="pom.xml" />
<artifact:install file="${dist.dir}/${jar.name}">
<pom refid="mypom"/>
<attach file="${dist.dir}/${amp.name}" type="amp" />
</artifact:install>
</target>
<target name="mvn-deploy" depends="dist-amp-jar" description="Deploy to remote Maven repo">
<echo message="Uploading to Maven repo as user '${repository.username}'"></echo>
<artifact:pom id="mypom" file="pom.xml" />
<artifact:deploy file="${dist.dir}/${jar.name}">
<remoteRepository url="${repository.url}">
<authentication username="${repository.username}" password="${repository.password}" />
</remoteRepository>
<pom refid="mypom"/>
<attach file="${dist.dir}/${amp.name}" type="amp" />
</artifact:deploy>
</target>
</project>
<project basedir="." default="dist-jar" name="Project-specific Build Script">
<!-- Import properties and targets from stock build script -->
<import file="build.xml" />
<!-- Override any targets provided by build.xml below -->
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment