Alfresco – Version Stamp Your AMP

I always find myself renaming AMP files after building them – I add the module version to the name. Being relatively lazy I decided to see if I could have Alfresco’s ANT build do this for me.

After a bit of trial and error I figured it out and created an ANT macro to do it for me.

I put my macro in a separate file called abstractive-macros.xml.

It has 2 parameters:

  1. modpropsdir is the directory containing the module.properties file. This is where we read the module version. It’s probably already defined in your build.properties under a property called dir.module.MYPROJECT.property.
  2. ampnameproperty is the NAME of the property that contains the AMP file name. We just want the name of the property (not its value) because we want to update the property itself so anything else that uses it will have the new name.

The macro:

  • pulls in the contents of module.properties
  • pulls the extension from the amp file name
  • gets the “module.version” from module.properties
  • builds a new amp file name that includes the version

It turns out you can use javascript within an ANT script – I didn’t know that. You can also use all kinds of other scripts such as groovy, ruby, jython, judoscript, etc.

Here’s the complete macro xml.

abstractive-macros.xml

<project name="abstractive-macros">

   <macrodef name="versionamp" description="Appends the module version to the amp file name">

      <!-- modpropsdir is the VALUE of the dir.module.XXXXX.property -->
      <attribute name="modpropsdir" />

      <!-- ampnameproperty is the NAME of the file.name.amp.XXXXX property
           and its value will be updated to include the module version -->
      <attribute name="ampnameproperty" />

      <sequential>
        <!-- append module version to amp file name -->
        <property file="@{modpropsdir}/module.properties"/>
        <script language="javascript"> <![CDATA[
          amp = project.getProperty("@{ampnameproperty}");
          title = amp.substring(0, amp.indexOf(".amp"));

          version = project.getProperty("module.version");
          project.setProperty("@{ampnameproperty}", title + "_" + version + ".amp");
        ]]> </script>
      </sequential>
   </macrodef>
</project>

The only change to build.xml is to include our new macro xml file.

build.xml

...
  <import file="macros.xml" />
  <import file="abstractive-macros.xml" />
  <import file="projects.xml" />
  ...

And in projects.xml we make the call to the macro. This is done in the package-MYMODULE-extension target. I pass it the path to module properties and the NAME of the property that contains the amp file name.

projects.xml

...
  <target name="package-MYMODULE-extension" depends="package-MYMODULE-jar">

    <versionamp modpropsdir="${dir.module.MYMODULE.property}"
      ampnameproperty="file.name.amp.MYMODULE" />

    <zip destfile="${dir.module.MYMODULE.dist}/${file.name.amp.MYMODULE}" update="true">
      <zipfileset file="${dir.module.MYMODULE.property}/module.properties" />
      <zipfileset file="${dir.module.MYMODULE.dist}/${file.name.jar.MYMODULE}" prefix="lib" />
      <zipfileset dir="${dir.module.MYMODULE}/${dir.name.lib}" prefix="lib" />
      <zipfileset dir="${dir.module.MYMODULE.config}" prefix="config">
        <exclude name="**/module.properties" />
      </zipfileset>
    </zip>
  </target>
  ...

And that’s all there is to it.  So if:

  • your module.properties contains module.version=1.10
  • in your build.properties your file.name.amp.MYPROJECT=MYPROJECT.amp

and you run the package-MYPROJECT-extension target it will generate an amp called MYPROJECT_1.10.amp.

This entry was posted in Alfresco, Alfresco Share, ECM by tim.frith. Bookmark the permalink.
tim.frith

About tim.frith

Alfresco Certified Engineer

Senior ECM Consultant
Tim holds a diploma in Computer Systems Technology from Camosun College as well as a Bachelor of Commerce Degree with a Finance specialization from the University of Alberta. He has over 10 years of experience developing web applications with Java as well as extensive database development experience with Oracle, IBM DB2, MS SQL Server, and MySQL.

In recent years he has focused on Enterprise Content Management, with specific training and experience in Alfresco's content management suite.

And finally, Tim is a strong believer that the Edmonton Oilers will soon reclaim their rightful place at the top of the hockey world. Yes, Stanley will once again come to town.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>