Search code examples
mavenpom.xmlminifyyui-compressor

yuicompressor maven is not working


I am working in Spring boot web application with maven build. I want to compress all js & css files. I have chosen YUI compression. When I build my application yui compression not happened. I am getting following message for all js & css files.

[INFO] nothing to do, **css\base.css is younger than original, use 'force' option or clean your target

What I am missing ?

Here is my pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>

      <groupId>MyApp</groupId>
      <artifactId>MyApp</artifactId>
      <version>0.0.1</version>
      <packaging>war</packaging>
    <!--   <url>http://maven.apache.org</url> -->

        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        </properties>

        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.4.2.RELEASE</version>
        </parent>

        <dependencies>
            my dependencies
        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.3</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>

                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <executable>true</executable>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>net.alchim31.maven</groupId>
                    <artifactId>yuicompressor-maven-plugin</artifactId>
                    <version>1.5.1</version>
                    <executions>
                      <execution>
                        <goals>
                          <goal>compress</goal>
                        </goals>
                      </execution>
                    </executions>        
                    <configuration>
                      <nosuffix>true</nosuffix>
                    </configuration>
                  </plugin> 
            </plugins>
        </build>
    </project>

Project structure : enter image description here


Solution

  • It's not an error and works as expected. The plugin creates a minified version of the resource only when it's minified version doesn't exist or when source file has been changed. When plugin found that minified version was created later than source file, it doesn't do minification and assumes that nothing to be done.

    As you can see, message suggests you to use force option (in this case, minified version will be always generated but it will be slower) or clean the target (execute mvn clean to remove all generated files so they will be generated again).


    UPDATED:

    I was able to reproduce the issue. It happens because yuicompressor-maven-plugin is being executed after maven-resource-plugin. The former was copied all the files from src/main/resources to the target directory and when yuicompressor was being executed, it found that the files already here (non-minified of course) and show this message.

    To fix this, first, we need to configure resource plugin to exclude resources:

    <build>
        <resources>
            <resource>
                <directory>${basedir}/src/main/resources/public</directory>
                <excludes>
                    <exclude>*.js</exclude>
                    <exclude>*/*.js</exclude>
                </excludes>
            </resource>
        </resources>
    </build>
    

    But it didn't solve it because after that I found that yuicompressor doesn't process these files. This is because the plugin was looking in the wrong directory and we have to configure it also:

    <plugin>
        <groupId>net.alchim31.maven</groupId>
        <artifactId>yuicompressor-maven-plugin</artifactId>
        <version>1.5.1</version>
        ...
        <configuration>
            <nosuffix>true</nosuffix>
            <sourceDirectory>src/main/resources/public</sourceDirectory>
        </configuration>
    </plugin>