Using Google Closure Compiler with Maven Web Application


References

Project Structure

.
|-- pom.xml
`-- src
    `-- main
        |-- java
        |-- resources
        |-- webapp
        |   |-- WEB-INF
        |   |   `-- web.xml
        |   |-- index.jsp
        |   `-- my-app
        |       |-- css
        |       |-- image
        |       `-- js
        |           |-- properties.js // filtering
        |           `-- my.js
        `-- webresources
            |-- his-app
            |   `-- his.interface.js
            |-- her-app-production
            |   |-- css
            |   |-- images
            |   `-- js
            `-- her-app-staging
                |-- css
                |-- images
                `-- js

Web Resources

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-war-plugin</artifactId>
  <configuration>
    <webResources combine.children="append"> <!-- wow! -->
      <resource>
        <directory>src/main/webapp</directory>
        <filtering>true</filtering>
      </resource>
      <resource>
        <directory>src/main/webresources/his-app</directory>
        <targetPath>his-app</targetPath>
      </resource>
    </webResources>
  </configuration>
</plugin>

Profiling

<profile>
  <id>staging</id>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <configuration>
          <webResources>
            <resource>
              <directory>src/main/webresources/her-app-staging</directory>
              <targetPath>her-app</targetPath>
            </resource>
          </webResources>
        </configuration>
      </plugin>
    </plugins>
  </build>
</profile>
<profile>
  <id>production</id>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <configuration>
          <webResources>
            <resource>
              <directory>src/main/webresources/her-app-production</directory>
              <targetPath>her-app</targetPath>
            </resource>
          </webResources>
        </configuration>
      </plugin>
    </plugins>
  </build>
</profile>

Closuring

<profile>
  <id>closure</id>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <executions>
          <execution>
            <id>closure-war</id>
            <phase>prepare-package</phase>
            <goals>
              <goal>war</goal>
            </goals>
            <configuration>
              <webXml>${project.basedir}/src/main/webapp/WEB-INF/web.xml</webXml>
              <warSourceIncludes>my-app/js/**/*.js</warSourceIncludes>
            </configuration>
          </execution>
          <execution>
            <id>default-war</id> <!-- !!! overridden !!! -->
            <phase>package</phase>
            <goals>
              <goal>war</goal>
            </goals>
            <configuration>
              <warSourceExcludes>my-app/js/**/*.js</warSourceExcludes>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.7</version>
        <executions>
          <execution>
            <id>closure-compile</id>
            <phase>prepare-package</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <target>
                <taskdef resource="net/sf/antcontrib/antlib.xml"/>
                <property name="fileset.dir"
                         value="${project.build.directory}/${project.artifactId}/mh-app/js"/>
                <property name="closure.class"
                          value="com.google.javascript.jscomp.CommandLineRunner" />
                <property name="compilation.level" value="SIMPLE"/>
                <echo message="fileset.dir: ${fileset.dir}"/>
                <echo message="closure.class: ${closure.class}"/>
                <echo message="compilation.level: ${compilation.level}"/>
                <for param="file" parallel="false">
                  <path>
                    <fileset dir="${fileset.dir}">
                      <include name="**/*.js"/>
                    </fileset>
                  </path>
                  <sequential>
                    <property name="source" value="@{file}"/>
                    <property name="target" value="@{file}.closured"/>
                    <java classname="${closure.class}" classpathref="maven.plugin.classpath"
                          fork="true" failonerror="true">
                      <arg value="--compilation_level"/>
                      <arg value="${compilation.level}"/>
                      <arg value="--js"/>
                      <arg value="${source}"/>
                      <arg value="--js_output_file"/>
                      <arg value="${target}"/>
                    </java>
                    <echo message="moving ${target} to ${source}"/>
                    <move file="${target}" toFile="${source}"/>
                  </sequential>
                </for>
              </target>
            </configuration>
          </execution>
        </executions>
        <dependencies>
          <dependency>
            <groupId>ant-contrib</groupId>
            <artifactId>ant-contrib</artifactId>
            <version>1.0b3</version>
            <exclusions>
              <exclusion>
                <groupId>ant</groupId>
                <artifactId>ant</artifactId>
              </exclusion>
            </exclusions>
          </dependency>
          <dependency>
            <groupId>org.apache.ant</groupId>
            <artifactId>ant-nodeps</artifactId>
            <version>1.8.1</version>
         </dependency>
          <dependency>
            <groupId>com.google.javascript</groupId>
            <artifactId>closure-compiler</artifactId>
            <version>v20140814</version>
          </dependency>
        </dependencies>
      </plugin>
    </plugins>
  </build>
</profile>

Build

$ mvn -Pproduction,closure clean package
...
[INFO] --- maven-war-plugin:2.4:war (closure-war) @ hpp-web-client-flash ---
...
[INFO] --- maven-antrun-plugin:1.7:run (closure-compile) @ hpp-web-client-flash ---
...
[INFO] --- maven-war-plugin:2.4:war (default-war) @ hpp-web-client-flash ---
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
...
$ 
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s