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.

Tweet about this on TwitterShare on FacebookShare on RedditShare on StumbleUponShare on LinkedInShare on Google+
Pages:
Edit