Wednesday, February 12, 2014

Use Maven to Manage Javascript Libraries

Usually we download javascript libraries and add them to web applications. It is simple and easy. However, it becomes headache when we need more and more libraries and the application grows big.

The problem is that some libraries are only compatible with others at certain versions. Also during the lifetime of the application, we need to add new features which will only be achievable with a newer version of some libraries. Now, are they still compatible? Or are they not anymore?

We will inevitably need to run testings back and forth to find the right mix. So what shall we do to minimize the hustle and headache?

Here is how I do it.

I download javascript libraries and install them into MyEclipse local maven repository. This is not ideal, because my teammates will need to repeat the installation. Ideally, we should be able to download it from central maven repository just like spring-core and spring-ws.

Webjars.org gives it a nice try to centralize popular javascript libraries and provide them as jar files. But I don't like its concept to use them as resources via classpath.

So I use maven overlays.

Here is how I define them in pom.xml.



It's not over yet. The Maven install process will create a myApp.war file, but MyEclipse doesn't deploy it to my JBoss server. So again I turn to Maven to solve this problem.

Here is how I deploy myApp.war to my JBoss server.



I use cargo-maven2-plugin which is not ideal either. Simply because some guys keep on thinking older is better, instead of upgrading to JBoss 7, we stuck with JBoss 5.1. Otherwise, jboss-as-maven-plugin would be beautiful.

Besides I use ${jboss.home} to point to my JBoss installation which is very necessary. Because I will check in the pom.xml to Subversion and my teammates will get the same pom.xml. If I hard code my JBoss home directory here, they will need to change it for their environment. So the solution is to introduce the settings.xml.



This settings.xml needs to put under C:\Users\[userid]\.m2 directory.

Now everything should be good.

No comments: