Overview

This document outlines an easy way to create JS-based artifacts and use those at runtime in your web applications.You can use Maven's dependency mechanism in your Java web application module POMs to include JS based artifacts and other static resources for use at runtime. The resources will be discovered at runtime by JavascriptDependencyFilter. JavascriptDependencyFilter can be configured to cache and even gzip resources according to configuration, as well as make lazagnia (the latter has not been thoroughly tested yet).

Structure of a JS project

A simple JS Maven project structure looks like this:

 /my-super-js-lib
 +-src
 | +-main
 |  +-js
 +-pom.xml    
           

As you can see we use a structure quite similar to the one used in a Maven based Java project. The main difference is the source directory name ("js" instead of "java") in the same location (${baseDir}/src/main).

Sample POM

Here's a minimal POM for the above project structure:

<?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>tld.copmpany.project</groupId>
  <artifactId>my-super-js-lib</artifactId>
  <name>My Super JS Library</name>
  <version>1.0b</version>
  <packaging>jar</packaging>
  <build>
    <!-- tell Maven to package you JS files as resources -->
    <resources>
      <resource>
        <directory>src/main/js</directory>
        <!-- true or false depending on your taste :-) -->
        <filtering>true</filtering>
      </resource>
    </resources>
  </build>
  <reporting>
    <plugins>
      <plugin>
        <!-- Use maven-jstools-plugin for your reports! -->
        <groupId>gr.abiss.mvn.plugins</groupId>
        <artifactId>maven-jstools-plugin</artifactId>
        <version>0.1</version>
        <configuration>
          <jsDir>src/main/js</jsDir>
        </configuration>
        <reportSets>
          <reportSet>
            <reports>
              <report>jslint</report>
              <report>jsdoc</report>
            </reports>
          </reportSet>
        </reportSets>
      </plugin>
    </plugins>
  </reporting>
</project>
            

This will create JS JAR artifacts. Suppose you have a gr/abiss/js/superlib package (directory) structure under src/main/js, the resulting JAR file will have the following structure:

 /my-super-js-lib-1.0b.jar (the archive itself)
 +-META-INF
 +-gr
  +-abiss
   +-js
    +-superlib
     +-SomeSrcipt.js
     +-SomeOtherSrcipt.js
          

For a more complete example of a JS project POM, you can always check out Sarissa 's POM here .

JavascriptDependencyFilter: Fetching packaged JS resources at runtime

All well and simple up to now, the question is: suppose you use such a JAR as a dependency in your WAR project, how will it work at runtime? Easy: just add maven-jstools-plugin as a normal dependency in your WAR POM and JavascriptDependencyFilter in your webapp config (i.e. web.xml) to intercept requests for JS files or other static resources packaged in your JS-based dependencies. That javadoc link has all the details :-)