In the course of creating Jangaroo 2, and utilizing a new feature of Java Servlet 3, we can now present a light-weight and easy way to use Maven for managing JavaScript library versions and their dependencies and adding them to your Web application.
Approach and Related Work
Note that we are not the only ones who provide Maven JAR artifacts containing repackaged JavaScript libraries. Independently, James Ward created a project called WebJars, which does something quite similar as proposed here. He also uses JARs and Maven, but the difference is that he is targeting other Web servers and languages, essentially using the JAR as a ZIP. Our approach is to use the Servlet 3 JAR layout, essentially putting all Web resources under the path META-INF/resources. The main advantage is that every Servlet 3 compliant application server (Jetty 8, Tomcat 7, Glassfish 3, Resin 4, WebSphere 8, WebLogic 12) understands this standardized format (JSR-315) immediately without any plugin, Servlet or other extension. Another benefit is that you can add files to the JAR that are not supposed to be Web resources, e.g. other meta data and of course Java classes, so that you could for example unite code implementing the client- and server-part of some service in a single JAR.An organizational advantage we have over James' artifacts is that we deploy all repackaged artifacts of Open Source libraries (almost all JavaScript libraries are Open Source!) directly to the Maven Central repository, saving you the need to add <repository> sections to your Maven POM and leaving you independent from our Maven repository being online.
Try It Out!
The first available artifact is a repackaging of Ext JS 3.4.0 (yes, it is an old version, but take it as an example of the mechanism) and a very light-weight example project using this artifact. The following steps are needed once to have an environment to develop Maven-managed JavaScript Web applications:- Install a recent Java SDK like 6 or 7
- Install Apache Maven, current version 3.0.4
- [Install git]
Checkpoint: calling mvn -v on a shell should give you output similar to this:
> mvn -v
Apache Maven 3.0.4 (r1232337; 2012-01-17 09:44:56+0100)
Maven home: ...
Java version: 1.6.0_25, vendor: Sun Microsystems Inc.
Java home: ...
Default locale: ...
OS name: ...
To try the Ext JS example project, download or clone from github:
> git clone git://github.com/fwienber/webjars-extjs-example.git
Then, all you need to do is build the project (which downloads all needed stuff from Maven Central when invoked for the first time) and start a Jetty Web server via Maven, simply via
> mvn jetty:start
The default port is 8080, so you can watch the resulting Web app in any browser using http://localhost:8080. To prove that Ext JS has been loaded and works, the app just opens an Ext alert box.
Only three small text files are needed to
- download the desired Ext JS library,
- download Jetty webserver,
- configure Jetty to serve the contents of the Ext JS library and the local project resources, and
- start Jetty in development mode on localhost port 8080.
The three files are
- pom.xml ―The Maven "Project Object Model" that configures the build,
- src/main/webapp/index.html ―the HTML file containing the simply JavaScript application,
- src/main/webapp/WEB-INF/web.xml ―Java Web configuration file.
Let me give you a walk-through of these three files.
pom.xml is the key to letting Maven create and manage your application. Fortunately, this POM is quite simple, since we take advantage of conventions, and Maven prefers convention over configuration.
<?xml version='1.0' encoding='UTF-8'?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.jangaroo.webjars.examples</groupId>
<artifactId>webjars-extjs-example</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>webjars-extjs-example</name>
<description>...</description>
<build>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>8.1.3.v20120416</version>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>net.jangaroo.com.sencha</groupId>
<artifactId>ext-js</artifactId>
<version>3.4.0</version>
</dependency>
</dependencies>
</project>
After the inevitable XML header (which is always the same), every Maven project model needs to specify a unique combination of groupId and artifactId, and a version. For this test project, they really do not matter, since the generated artifact will never be deployed to a repository. The packaging type war tells Maven that we are building a Web application (Java terminology: Web ARchive). name and description are just for human readability.
The only plugin that needs to be mentioned explicitly is jetty-maven-plugin, and you also need to specify the exact version you want to use. Well, it's still only a couple of lines to download and start a Web server, isn't it?
To download a serve all Ext JS 3.4.0 resources, a simple dependency suffices, thanks to the standard-conform JAR layout and central deployment of the artifact.
index.html is the simplest HTML page you could think of that starts an Ext JS application. The only interesting thing here is how the resource paths have been chosen, because you do not actually "see" the Ext JS resources―they are hidden in a JAR somewhere in your local Maven repository (see below). For every JavaScript library repackaging, we have to agree upon a standard path where it is served in the Web application. For Ext JS, we chose /ext-js/ as the base path. The file structure below this base path is exactly as in the ZIP that can be obtained from Sencha's Ext JS 3 download page.
web.xml is only needed to disable a Jetty feature ("file locking") that hinders development, and to satisfy the Maven WAR plugin, which would have to be configured to not <failOnMissingWebXml> otherwise.
The Jetty Maven plugin configures Jetty in a clever way: library dependencies like Ext JS, which have been downloaded to your local Maven repository automatically (usually ~/.m2/repository), are served directly from that JAR. That means no files are extracted, not even the JARs needed by your Web app are copied. All local project Web resources are taken directly from src/main/webapp. Try changing index.html (e.g. the alert message) and reload in the browser: it works without further build or deployment!
If you want a complete Web app that could be deployed into a remote Java application server, you can invoke mvn package, and a directory target/webjars-extjs-example-0.1-SNAPSHOT is created which contains all needed resources. This directory is also zipped as target/webjars-extjs-example-0.1-SNAPSHOT.war.
Unpacking all Web resources contained in JARs, e.g. to deploy them to a static Web server like Apache HTTP Server, is also possible via Maven, but I'll cover that in a later update.
Mighty Maven
What the simple example does not show is that, if ext-js 3.4.0 had a dependency on some other artifact, say ext-core 1.0, you wouldn't have to care about it. Simply specifying a dependency on ext-js lets Maven collect all transitive dependencies and add the corresponding artifacts to your Web application. Furthermore, if two libraries you use depend upon the same third library, Maven takes care of the third library being included only once, and offers dependency management to resolve possible version conflicts.All in all, given the complexity of the problem it solves, Maven provides a light-weight way to manage complex JavaScript Web applications, making it easy to keep track of libraries used, their versions, and their dependencies. Furthermore, Java Servlet 3 helps structure a Web application as a composite of modules instead of a huge bunch of files, which speeds up build process and test system start-up.