Skip to content Skip to sidebar Skip to footer

Requirejs Compilation In Maven Project With External Js Dependencies

I have a web project that's built with Maven and I'm trying to figure out the best way to compile the JavaScript files with the RequireJS compiler (this question could apply to any

Solution 1:

Solution 2:

There's a maven plugin specifically targeted for RequireJS optimization at:

https://github.com/mcheely/requirejs-maven-plugin

It should always ship with a recent version of RequireJS, and it's pretty easy to override that by adding a different version to your project, and specifying it's path in the plugin config.

Full disclosure: I wrote the plugin.

Solution 3:

I've come up with the following solution that works for me:

Instead of depending on the WAR overlay for the unpacking of the RequireJS files, I explicitly unpack them using the Dependency plugin:

<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-dependency-plugin</artifactId><executions><execution><phase>compile</phase><goals><goal>unpack</goal></goals><configuration><artifactItems><artifactItem><groupId>org.requirejs</groupId><artifactId>requirejs</artifactId><version>0.22.0</version><type>zip</type><classifier>repackaged</classifier><overWrite>true</overWrite></artifactItem></artifactItems></configuration></execution></executions></plugin>

The phase is set to "compile". This allows me to have the entire contents of the RequireJS package under the "dependency" folder, during compilation. So, the next thing I have is:

<plugin><groupId>org.codehaus.mojo</groupId><artifactId>exec-maven-plugin</artifactId><version>1.1</version><executions><execution><phase>compile</phase><goals><goal>exec</goal></goals><configuration><executable>
              ${project.build.directory}/dependency/requirejs/build/build.bat</executable><workingDirectory>
              ${basedir}/src/main/webapp</workingDirectory><arguments><argument>name=bootstrap</argument><argument>out=scripts/compiled.js</argument><argument>baseUrl=scripts</argument><argument>optimize=closure</argument><argument>
                excludeShallow=plugins/manifest</argument></arguments></configuration></execution></executions></plugin>

This triggers RequireJS compilation inside the "dependency" folder, without touching either my source directory or the WAR directory. I then continue using the WAR overlay plugin to cherry pick the RequireJS files that want to go into the WAR.

Update

Since writing this solution, I've switched to using the "java" goal instead of "exec" because I had problems with using the RequireJS compiler's shell script + batch file through Hudson. I'm basically running the final Java command (generated by the scripts) that run the compiler through Rhino. The Node solution would be slightly different. For RequireJS 0.23.0, it goes something like this:

<plugin><groupId>org.codehaus.mojo</groupId><artifactId>exec-maven-plugin</artifactId><version>1.1</version><executions><execution><id>compile-js</id><phase>compile</phase><goals><goal>exec</goal></goals><configuration><executable>java</executable><workingDirectory>${basedir}/src/main/webapp</workingDirectory><arguments><argument>-classpath</argument><!--argument>${project.build.directory}/dependency/requirejs/build/lib/rhino/js.jar${path.separator}${project.build.directory}/dependency/requirejs/build/lib/closure/compiler.jar</argument --><argument>${project.build.directory}/dependency/requirejs/build/lib/rhino/js.jar</argument><argument>org.mozilla.javascript.tools.shell.Main</argument><argument>${project.build.directory}/dependency/requirejs/bin/x.js</argument><argument>${project.build.directory}/dependency/requirejs/bin/</argument><argument>${project.build.directory}/dependency/requirejs/build/build.js</argument><argument>name=bootstrap</argument><argument>out=scripts/compiled.js</argument><argument>baseUrl=scripts</argument><argument>optimize=none</argument></arguments></configuration></execution></executions></plugin>

Post a Comment for "Requirejs Compilation In Maven Project With External Js Dependencies"