22

I have 100s of javascript files inside the hierarchy of folders and I want two sets of output. one is to have a concatenated version for debugging purposes and the other one is to have a concat + minfy version. I am currently using the below plugin but in this I need to provide each and every file that I need to minify. I am looking for a plugin which needs only parent folder and satisfy the above conditions.

<groupId>com.samaxes.maven</groupId>
<artifactId>minify-maven-plugin</artifactId>
<version>1.7</version>
Dmitry
  • 2,943
  • 1
  • 23
  • 26
Mady
  • 5,016
  • 7
  • 35
  • 46
  • The Minify plugin is based on YUI compressor, and they are both buggy and break with the latest CSS standards. See e.g. https://github.com/samaxes/minify-maven-plugin/issues/161 . See my question https://stackoverflow.com/q/52229690/421049 to try to find a non-buggy alternative. – Garret Wilson Feb 06 '19 at 14:55

3 Answers3

33

YUI compression maven plugin worked for me. I will show what all I did to make it work.

  • To concatenate all the js files I used aggregation.

Details of the elements used.

  • preProcessAggregates - To process aggregation before minification.
  • aggregations - To aggregate the multiple resources in folder hierarchy to a single file.
  • aggregation - There can be multiple aggregation elements inside parent aggregations.
  • insertNewLine - Insert newline after each file eof, while concatenation/aggregation of files.
  • inputDir - Parent directory inside which files would be searched for concatenation/aggregation.
  • sourceDirectory - Directory under which files would be searched for minification.
  • outputDirectory - Directory under which minified output would be placed.
  • nosuffix - If set to true, then plugin will not add '-min' to the minified file.

There are 2 types of <exclude> property:-

  • First is part of aggregation, which basically excludes files from aggregation.
  • Second is part of the plugin to exclude files from minification.

Plugin code:-

<plugin>
  <groupId>net.alchim31.maven</groupId>
  <artifactId>yuicompressor-maven-plugin</artifactId>
  <version>1.3.2</version>
  <configuration>
    <preProcessAggregates>true</preProcessAggregates>
    <aggregations>
      <aggregation>
        <insertNewLine>true</insertNewLine>
        <output>${basedir}/target/single.js</output>
        <inputDir>${basedir}/src/main/resources/js</inputDir>
        <includes>
          <include>**/*.js</include>
        </includes>
        <excludes>
          <exclude>**/*abc.js</exclude>
          <exclude>**/compressed.css</exclude>
        </excludes>
      </aggregation>
    </aggregations>
    <excludes>
      <exclude>**/*-min.js</exclude>
      <exclude>**/*.min.js</exclude>
      <exclude>**/*-min.css</exclude>
      <exclude>**/*.min.css</exclude>
    </excludes>
    <jswarn>false</jswarn>
    <nosuffix>false</nosuffix>
    <sourceDirectory>${basedir}/target</sourceDirectory>
    <outputDirectory>${basedir}/target</outputDirectory>
  </configuration>
  <executions>
    <execution>
      <id>compress_js_css</id>
      <phase>process-resources</phase>
      <goals>
        <goal>compress</goal>
      </goals>
    </execution>
  </executions>
</plugin>
Tom Pietrosanti
  • 4,184
  • 2
  • 24
  • 29
Mady
  • 5,016
  • 7
  • 35
  • 46
  • 1
    Alternatively, the plugin can be found here: http://davidb.github.io/yuicompressor-maven-plugin/ – Stephan Feb 08 '16 at 14:02
  • why did @Mady set nosuffix to false? normally you only care about deploying the minified version right? http://alchim.sourceforge.net/yuicompressor-maven-plugin/index.html – DPM Dec 04 '17 at 16:46
  • YUI Compressor is completely broken with CSS3. See e.g. https://github.com/yui/yuicompressor/issues/268 and https://github.com/yui/yuicompressor/issues/313 . – Garret Wilson Feb 06 '19 at 14:52
15

You should take a look to yui compression maven plugin which sounds like the thing you need.

khmarbaise
  • 92,914
  • 28
  • 189
  • 235
  • Yes, I was using it but not correctly. I was missing the aggregation of resources. – Mady May 26 '13 at 09:34
  • 3
    YUI Compressor is completely broken with CSS3. See e.g. https://github.com/yui/yuicompressor/issues/268 and https://github.com/yui/yuicompressor/issues/313 . – Garret Wilson Feb 06 '19 at 14:55
4

Mady, Minify Maven Plugin does also support include/exclude patterns.
Please take a look at the Lexicographical ordering example page from the plugin documentation.

Samuel Santos
  • 391
  • 1
  • 11
  • Thanks Samuel, yeah it works well, but closure jsEngine is not able to minify, it is breaking at some point. Initially, I was inclined to closure that's why i used the minify-maven-plugin. – Mady May 27 '13 at 06:26