Rock the RequireJS in Maven

After a considerable amount of trial and error, I have discovered the following setup steps for enabling requirejs in a maven project.

Integrating Maven

The requirejs maven plugin world has been marked by two packages: brew, and requirejs-maven-plugin. Brew takes the approach of “we’ll do everything and try to support all of the config file format stuff in our pom.” This has the clear disadvantage of depending on these guys to match 100% what the latest r.js and application configuration can do. For a rapidly evolving library, this is no easy feat. As expected, brew ends up falling short of this.

On the flip side, requirejs-maven-plugin offers a lightweight maven “integration-point”, allowing you to have your own version of r.js along with a separate application configuration. Below is the link to the developer’s github project:

Here is a very close approximation to the plugin i used for this to work:

<plugins>
  <plugin>
    <groupId>com.github.mcheely</groupId>
    <artifactId>requirejs-maven-plugin</artifactId>
    <version>1.0.3</version>
    <executions>
      <execution>
        <goals>
          <goal>optimize</goal>
        </goals>
      </execution>
    </executions>
    <configuration>
      <configFile>${basedir}/src/main/config/app.config.js </configFile>
      <optimizerFile>${basedir}/src/main/scripts/r.js</optimizerFile>
      <filterConfig>true</filterConfig>
    </configuration>
  </plugin>
</plugins>

Mavenizing your app config

Due to our settings up above, your application configuration is actually going to be filtered by maven before it is run.

This lets you use the common variables you are used to having in a configuration file. Below is a potential setup:

({
    appDir: "${basedir}/src/main/js/",
    baseUrl: "./",
    dir: "${project.build.directory}/${project.build.finalName}/js",
    modules: [
        {
            name: "main"
        }
    ]
})

Conclusion

The modularity advantages of requirejs can exist inside of your maven projects after all! This solution does not exactly follow the “maven way” of piling verbose configurations inside of your pom.xml, but I think you can survive. If you have gone so far as to mavenize your javascript dependencies, this plugin does state that its default behavior is to look for r.js on the classpath, so I assume that would work as well.

4 Comments

  1. optyler says:

    Hi,

    it seems that this plugin don’t work anymore with the new version of maven/m2e.

    I get this error :

    Plugin execution not covered by lifecycle configuration: com.github.mcheely:requirejs-maven-plugin:1.1.0:optimize (execution: default, phase: process-classes) pom.xml /qesmvc line 370 Maven Project Build Lifecycle Mapping Problem

    Have you got some tips for me?

    Thanks!

  2. Nick says:

    @optyler The plugins for this don’t work with maven 2 after version 1.0.3. That’s why I’m using the older version. Upgrade to maven 3 or downgrade your plugin version.

  3. Upendra says:

    Hello,
    I am using the above plugin version 2.0.0 but I am getting following error

    Failed to execute r.js : Encountered code generation error while compiling script: generated bytecode f or method exceeds 64K limit.

    any tips, help will be appriciated

Start the Discussion!Leave a Reply to Upendra Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.