Good practice :
The major js and css framework/lib provide source code in both version (one big, lot of small), or provide a online tool to generate the big.
The following option allow you to use/store small files into your source and generate the big at build time. The aggregation is done after yuicompression
To Compress every js and css files and aggregate every js file under ${project.build.directory}/${project.build.finalName}/static/ into all.js :
<project> ... <build> <plugins> ... <plugin> <groupId>net.alchim31.maven</groupId> <artifactId>yuicompressor-maven-plugin</artifactId> <executions> <execution> <goals> <goal>compress</goal> </goals> </execution> </executions> <configuration> <nosuffix>true</nosuffix> <aggregations> <aggregation> <!-- remove files after aggregation (default: false) <removeIncluded>true</removeIncluded> --> <!-- insert new line after each concatenation (default: false) --> <insertNewLine>true</insertNewLine> <output>${project.build.directory}/${project.build.finalName}/static/all.js</output> <!-- files to include, path relative to output's directory or absolute path--> <!--inputDir>base directory for non absolute includes, default to parent dir of output</inputDir--> <includes> <include>${basedir}/src/licenses/license.js</include> <include>**/*.js</include> </includes> <!-- files to exclude, path relative to output's directory <excludes> <exclude>**/*.pack.js</exclude> <exclude>**/compressed.css</exclude> </excludes> --> </aggregation> </aggregations> </configuration> </plugin> ... </plugins> </build> ... </project>
Using removeIncluded option, remove file, but the war plugin will then add file. So if you want to remove file after aggregation and don't want war plugin copy them, then you need to use warSourceExcludes :
<project> ... <build> <plugins> ... <plugin> <groupId>net.alchim31.maven</groupId> <artifactId>yuicompressor-maven-plugin</artifactId> <executions> <execution> <goals> <goal>compress</goal> </goals> </execution> </executions> <configuration> <nosuffix>true</nosuffix> <aggregations> <aggregation> <!-- remove files after aggregation (default: false) --> <removeIncluded>true</removeIncluded> <!-- insert new line after each concatenation (default: false) --> <insertNewLine>true</insertNewLine> <output>${project.build.directory}/${project.build.finalName}/static/all-2.js</output> <!-- files to include, path relative to output's directory --> <includes> <include>toAggregateAndRemove/**.js</include> </includes> </aggregation> </aggregations> </configuration> </plugin> ... <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <configuration> <warSourceExcludes>**/toAggregateAndRemove/**</warSourceExcludes> </configuration> </plugin> ... </plugins> </build> ... </project>
When aggregating minified js files, the copyright headers have been stripped out which is fine because we don't want to repeat it several times in the output file. However it would be great to be able to insert one at beginning of output file.
For simple cases, the maven-license-plugin is enough if you use the same header for all files but it is not enough if you want to have a different header per aggregation (different libraries with different licensing schemes).
<includes> <include>${project.build.sourceDirectory}/../webapp/js/mylib/copyright.txt</include> <include>mylib/**/*.js</include> </includes>