Fork me on GitHub

Transform files list to file with JavaScript imports

This example shows how to use the transformList goal to generate an import-scripts.jsp file with JavaScript imports. POM define application which can be build in two modes:

  1. production - merge and minify all JavaScript files to one file (all.js)
  2. development - include all not minified scripts (great for debug)
<project>
   ...
   <properties>
      <!-- disable development mode (production be default) -->
      <mode.development>false</mode.development>
      <!-- minify-maven-plugin requires exclusion processed files -->
      <war.excludes>js/**/*</war.excludes>
   </properties>
   ...
   <build>
      <plugins>
         ...
         <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
               <excludes>${war.excludes}</excludes>
            </configuration>
         </plugin>
         <plugin>
            <groupId>com.samaxes.maven</groupId>
            <artifactId>minify-maven-plugin</artifactId>
            <version>1.7.4</version>
            <executions>
               <execution>
                  <goals>
                     <goal>minify</goal>
                  </goals>
                  <configuration>
                     <!-- includes all files from src/main/webapp/js directory -->
                     <jsSourceDir>js</jsSourceDir>
                     <jsIncludes>
                        <jsInclude>**/*</jsInclude>
                     </jsIncludes>
                     <!-- save merged and minified JavaScript to /target/<full-application-name>/js/all.js -->
                     <jsFinalFile>all.js</jsFinalFile>
                     <!-- max 100 characters per line -->
                     <yuiLineBreak>100</yuiLineBreak>
                     <!-- disable plugin in development mode -->
                     <skipMerge>${mode.development}</skipMerge>
                     <skipMinify>${mode.development}</skipMinify>
                     <!-- disable creation of all.min.js (save minified code to all.js) -->
                     <nosuffix>true</nosuffix>
                  </configuration>
               </execution>
            </executions>
         </plugin>
      </plugins>
   </build>
   <profiles>
      <profile>
         <id>DEVELOPMENT</id>
         <properties>
            <!-- enable development mode -->
            <mode.development>true</mode.development>
            <!-- file with all.js import will be replaced by generated by directory-content-maven-plugin -->
            <!-- this value also removes exclusion of js/**/* files -->
            <war.excludes>WEB-INF/import-scripts.jsp</war.excludes>
         </properties>
         <build>
            <plugins>
               <plugin>
                  <groupId>biz.gabrys.maven.plugins</groupId>
                  <artifactId>directory-content-maven-plugin</artifactId>
                  <version>2.0.1</version>
                  <executions>
                     <execution>
                        <goals>
                           <goal>transformList</goal>
                        </goals>
                        <configuration>
                           <!-- includes all files from src/main/webapp/js directory -->
                           <sourceDirectory>src/main/webapp/js</sourceDirectory>
                           <includes>
                              <include>**/*</include>
                           </includes>
                           <!-- save imports list to import-scripts.jsp -->
                           <!-- this file will not be overwrite because is excluded for maven-war-plugin -->
                           <outputFile>${project.build.directory}/${project.artifactId}-${project.version}/WEB-INF/import-scripts.jsp</outputFile>
                           <xsltFile>import-scripts.xsl</xsltFile>
                           <separator>/</separator>
                        </configuration>
                     </execution>
                  </executions>
               </plugin>
            </plugins>
         </build>
      </profile>
   </profiles>
<project>

src/main/webapp/WEB-INF/import-scripts.jsp content:

<%-- maven will ignore this and create another file in development mode (more information in pom.xml) --%>
<script src="js/all.js"></script>

import-scripts.xsl content:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="html" />

   <xsl:template match="/files">
      <xsl:apply-templates select="file" />
   </xsl:template>

   <xsl:template match="file">
      <xsl:text disable-output-escaping="yes"><![CDATA[<script src="/js/]]></xsl:text>
      <xsl:value-of select="fullPath" />
      <xsl:text disable-output-escaping="yes"><![CDATA["></script>]]>&#10;</xsl:text>
   </xsl:template>
</xsl:stylesheet>