18

I've been struggling with getting this plugin to play nicely with the maven-war-plugin for a couple of hours now and I thought it was time to ask for help. I have the plugin defined as follows:

<plugin>
    <groupId>net.alchim31.maven</groupId>
    <artifactId>yuicompressor-maven-plugin</artifactId>
    <version>1.3.0</version>
    <executions>
        <execution>
            <id>compressyui</id>
            <phase>process-resources</phase>
            <goals>
                <goal>compress</goal>
            </goals>
            <configuration>
                <nosuffix>true</nosuffix>
                <warSourceDirectory>${basedir}/WebContent</warSourceDirectory>
                <jswarn>false</jswarn>
            </configuration>
        </execution>
    </executions>
</plugin>

If I remove nosuffix=true then I can see the compressed/minified -min.js files get into the war as expected, but with this flag on they are being overwritten by the maven-war-plugin (I'm assuming) when it builds the war file. I really need the file names to remain the same though ... does anyone have an idea of what I need to change in order to use the same filenames and still get the minified versions into the final war?

Jean-Rémy Revy
  • 5,607
  • 3
  • 39
  • 65
Dave Maple
  • 8,102
  • 4
  • 45
  • 64
  • This case may have solution but the plugin version 1.5.1 has a bug because it cannot be configured for some simple cases. Say we want to configure webappDirectory and sourceDirectory. If sourceDirectory is configured different from the default then the plugin executes compression twice once with the configured value and then with the default value which defeats the purpose of sourceDirectory configuration. I haven't been able to compress from a directory that had to have changes made before compression. – user250343 Aug 22 '20 at 15:44

7 Answers7

26

OK. I finally figured this out. You need to define a <webappDirectory> in the yuicompressor plugin that can then be referenced as a <resource> in the maven-war-plugin. In the example below I'm using <directory>${project.build.directory}/min</directory>

<plugin>
    <groupId>net.alchim31.maven</groupId>
    <artifactId>yuicompressor-maven-plugin</artifactId>
    <version>1.3.0</version>
    <executions>
        <execution>
            <id>compressyui</id>
            <phase>process-resources</phase>
            <goals>
                <goal>compress</goal>
            </goals>
            <configuration>
                <nosuffix>true</nosuffix>
                <warSourceDirectory>${basedir}/WebContent</warSourceDirectory>
                <webappDirectory>${project.build.directory}/min</webappDirectory>
                <jswarn>false</jswarn>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <artifactId>maven-war-plugin</artifactId>
    <executions>
        <execution>
            <id>default-war</id>
            <phase>package</phase>
            <goals>
                <goal>war</goal>
            </goals>
            <configuration>
                <warSourceDirectory>${basedir}/WebContent</warSourceDirectory>
                <encoding>UTF-8</encoding>
            </configuration>
        </execution>
    </executions>
    <configuration>
        <warSourceDirectory>${basedir}/WebContent</warSourceDirectory>
        <encoding>UTF-8</encoding>
        <webResources>
            <resource>
                <directory>${project.build.directory}/min</directory>
            </resource>
        </webResources>
    </configuration>
</plugin>
Jean-Rémy Revy
  • 5,607
  • 3
  • 39
  • 65
Dave Maple
  • 8,102
  • 4
  • 45
  • 64
  • 2
    Thank you, thank you, thank you! I had the same problem and could not find any sensible solution until now. YUI plugin vocabulary is highly confusing imo :/ – fbiville Feb 07 '13 at 00:49
  • Doesn't work for me. The war plugin will copy web resources (minified files) first, the overwrite with contents of war source directory. – Jakob Kruse Jul 08 '14 at 13:34
  • Yeah that's why you'd put them into the /min directory and then in the maven-war-plugin define that /min directory as a as in the example above. – Dave Maple Jul 10 '14 at 20:32
  • hi, I use your way but `mvn install` indict that `[ERROR] Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.2:war (default-war) on project gif-www: Execution default-war of goal org.apache.maven.plugins:maven-war-plugin:2.2:war failed: basedir /home/ximing/work/gifmiao/gif-www/target/min does not exist -> [Help 1] ` – armnotstrong Dec 28 '16 at 10:06
  • sounds like a permissions issue @armnotstrong -- what user does mvn run as and does it have write permissions in that directory? – Dave Maple Dec 29 '16 at 17:42
  • Hi, @DaveMaple This plugin will compress all .js and .css files under src/main/webapp. But in my case I have my .js and .css files under src/main/resources/static, so how can I change that default path to this? Thanks in advance. – Vibhav Chaddha Dec 16 '19 at 07:10
8

Just configure 'warSourceExcludes' on the WAR plugin.

<plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <warSourceExcludes>**/*.css,**/*.js</warSourceExcludes>
    </configuration>
</plugin>
Thrawn
  • 81
  • 1
  • 1
4

I would like to add the configuration which worked for me:

First, to fix m2e complaining about the 'Plugin execution not covered by lifecycle' I added the following in the parent pom taken from this post:

  <pluginManagement>
    <plugins>
        <!--This plugin's configuration is used to store Eclipse 
            m2e settings only. It has no influence on the Maven build itself. -->
        <plugin>
            <groupId>org.eclipse.m2e</groupId>
            <artifactId>lifecycle-mapping</artifactId>
            <version>1.0.0</version>
            <configuration>
                <lifecycleMappingMetadata>
                    <pluginExecutions>
                        <pluginExecution>
                            <pluginExecutionFilter>
                                <groupId>net.alchim31.maven</groupId>                               
                                <artifactId>yuicompressor-maven-plugin</artifactId>
                                <versionRange>[1.0.0,)</versionRange>
                                <goals>
                                    <goal>compress</goal>
                                </goals>
                            </pluginExecutionFilter>
                            <action>
                                <execute />
                            </action>
                        </pluginExecution>
                    </pluginExecutions>
                </lifecycleMappingMetadata>
            </configuration>
        </plugin>
    </plugins>
</pluginManagement>

Then in the war pom I put:

<build>
    <plugins>
        <plugin>
        <groupId>net.alchim31.maven</groupId>
        <artifactId>yuicompressor-maven-plugin</artifactId>
        <version>1.4.0</version>
        <executions>
        <execution>
            <goals>
              <goal>compress</goal>
            </goals>
            <configuration>
               <linebreakpos>300</linebreakpos>
               <excludes>
                 <exclude>**/*-min.js</exclude>
                 <exclude>**/*.min.js</exclude>
                 <exclude>**/*-min.css</exclude>
                 <exclude>**/*.min.css</exclude>
               </excludes>              
               <nosuffix>true</nosuffix>
            </configuration>
        </execution>
        </executions>
        </plugin>
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <warSourceExcludes>**/*.css,**/*.js</warSourceExcludes>
            </configuration>
        </plugin>
    </plugins>
</build>

This generates the minified css and js files in the project build target directory while excluding the original files.

I hope this saves someone time.

Community
  • 1
  • 1
TooSerious
  • 409
  • 4
  • 13
2

this is my configuration, and it works fine in my maven web project:

    <!-- js/css compress -->
<plugin>
    <groupId>net.alchim31.maven</groupId>
    <artifactId>yuicompressor-maven-plugin</artifactId>
    <version>1.3.2</version>
    <configuration>
        <excludes>
            <exclude>**/*-min.js</exclude>
            <exclude>**/*.min.js</exclude>
            <exclude>**/*-min.css</exclude>
            <exclude>**/*.min.css</exclude>
        </excludes>
        <jswarn>false</jswarn>
        <nosuffix>true</nosuffix>
    </configuration>
    <executions>
        <execution>
            <id>compress_js_css</id>
            <phase>process-resources</phase>
            <goals>
                <goal>compress</goal>
            </goals>
        </execution>
    </executions>
</plugin>
<!-- war -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.3</version>
    <configuration>
        <webResources>
            <resource>
                <directory>${project.build.directory}/${project.build.finalName}/resources</directory>
                <targetPath>/resources</targetPath>
                <filtering>false</filtering>
            </resource>
        </webResources>
        <webXml>src/main/webapp/WEB-INF/web.xml</webXml>
    </configuration>
</plugin>
vivia
  • 2,983
  • 1
  • 13
  • 8
0

The approach I use is a bit different.

First, I've configured my IDE to run mvn process-resources before the compilation/packaging. This way the files are created before the war is assembled.

It is very important to set <nosuffix>false</nosuffix> and <outputDirectory>${basedir}/src/main/resources/</outputDirectory> so the files can be created in the same directory without replacing your original source files.

 <plugin>
    <groupId>net.alchim31.maven</groupId>
    <artifactId>yuicompressor-maven-plugin</artifactId>
    <version>1.3.2</version>
    <configuration>
        <preProcessAggregates>false</preProcessAggregates>
        <excludes>
            <exclude>**/*-min.js</exclude>
            <exclude>**/*.min.js</exclude>
            <exclude>**/*-min.css</exclude>
            <exclude>**/*.min.css</exclude>
        </excludes>
        <jswarn>false</jswarn>
        <nosuffix>false</nosuffix> <!-- VERY IMPORTANT WILL REPLACE YOUR FILES IF YOU SET nosuffix TO TRUE OR DONT SET IT AT ALL -->
        <outputDirectory>${basedir}/src/main/resources/</outputDirectory> <!-- by default the plugin will copy the minimized version to target directory -->
        <failOnWarning>false</failOnWarning>
    </configuration>

    <executions>
        <execution>
            <id>compress_js_css</id>
                <phase>process-resources</phase>
            <goals>
                <goal>compress</goal>
            </goals>
        </execution>
    </executions>
</plugin>
lmiguelmh
  • 3,074
  • 1
  • 37
  • 53
0

As Jakob Kruse say, you must deal with the *.js, but no *.min.js, so my configurations is below, please notice the use of %regex[] :

   <plugin>
       <groupId>net.alchim31.maven</groupId>
       <artifactId>yuicompressor-maven-plugin</artifactId>
       <version>1.4.0</version>
       <executions>
           <execution>
               <id>compressyui</id>
               <phase>process-resources</phase>
               <goals>
                   <goal>compress</goal>
               </goals>
               <configuration>
                   <nosuffix>true</nosuffix>
                   <warSourceDirectory>${basedir}/WebContent</warSourceDirectory>
                   <webappDirectory>${project.build.directory}/min</webappDirectory>
                   <jswarn>false</jswarn>  
            <excludes>
                <exclude>**/*-min.js</exclude>
                <exclude>**/*.min.js</exclude>
                <exclude>**/*-min.css</exclude>
                <exclude>**/*.min.css</exclude>

                <exclude>**/jquery.window.js</exclude>
                ......
                <exclude>**/compile.js</exclude>
            </excludes>
               </configuration>
           </execution>
       </executions>
   </plugin>

   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.4</version>
    <configuration>
     <warSourceDirectory>WebContent</warSourceDirectory>     
     <packagingExcludes>servlet-api*.jar,target/test-classes/*</packagingExcludes>
     <warSourceExcludes>test/**,%regex[.*(!min).js],%regex[.*(!min).css]</warSourceExcludes>
    
     <webResources>
                  <resource>
                     <directory>${project.build.directory}/min</directory>
                 </resource>
     </webResources>
    
    </configuration>
   </plugin>
Yu Jiaao
  • 4,444
  • 5
  • 44
  • 57
0

Without pom.xml change

mvn net.alchim31.maven:yuicompressor-maven-plugin:compress

To force compress every js and css files and fail if warning

mvn net.alchim31.maven:yuicompressor-maven-plugin:compress \
-Dmaven.yuicompressor.force=true \
-Dmaven.yuicompressor.failOnWarning=true \

For more options: http://davidb.github.io/yuicompressor-maven-plugin/usage_compress.html

PKPrabu
  • 409
  • 4
  • 18