Using Maven to Package JavaScript Library

Hopefully, the article about using JavaScript libraries should have swayed you should use Maven or some other repository to disseminate your libraries. But now you’ve hit a point that you have found a library that doesn’t exists in any Maven Repository (at least not ones that you have access to). What to do now? No worries. Publishing an artifact is easy.

First, assuming your code is in the location ${basedir}/src/main/javascript, create a file in ${basedir}/src/main/resources called “assembly.xml”. This file should contain the following:

This file will tell the assembly plugin how to package (into a zip, gzip-tarball, and bzip2-tarball) and what to include (“*”, or everything – you can include only specific things or exclude specific things. Please see the Assembly Plugin for more information about configuring includes/excludes).

Next, you need to tell maven to execute the Assembly plugin. Do this by adding the following to your pom.xml:

What if the code isn’t yours, though? OK, now we are in a strange place. First, make sure that the licensing allows you to republish this if you are publishing this somewhere public. If it is Apache 2 or Gnu, you won’t have an issue republishing it unmodified. But check to be sure.

First, you need to download the code from the external repository. The following is code that will download Google’s Closure JavaScript library. This uses the SCM plugin. This is the same plugin that acts on your code in your SCM section, allowing you to do mvn scm commands. Why don’t we put it in the SCM section? Well, cause now we know that your code will live in a different place than the SCM plugin. So you are going to need to fill out the SCM section with your SCM details.

Ok, that will have downloaded this into your target/checkout folder. Now, jut make a quick tweak to what we’ve already done. Change the directory that the assembly.xml is pointing at to:

And Voila! Now, if you just use the Maven release plugin or follow standard deployment procedures, this will be released for everyone in your company (or in the world, depending on where you are publishing to)!