Search code examples
mavenmaven-pluginminifymaven-war-plugin

minify-maven-plugin (com.samaxes.maven v1.7.6) is not replacing the minified files with the original files


I want to minify JS and CSS files in maven project. I have used minify-maven-plugin (com.samaxes.maven v1.7.6). As per the documentation (https://samaxes.github.io/minify-maven-plugin/minify-mojo.html), I have set <nosuffix> and <skipMerge> as true because I want to maintain the file structure and replace the minified files with original files. I have also set the <phase>package</phase>. After generating and deploying the WAR file, the JS and CSS files are not minified, they stay the same as before. I also referred to some stackoverflow answers and set the <warSourceExcludes> option as per the suggestion provided at https://stackoverflow.com/questions/22117824/using-samaxes-minify-nosuffix-to-overwrite-original-files After using the <warSourceExcludes> option, when I deploy the WAR file on the server, the JS and CSS files are not available and the application is showing 404 errors for the same. Please refer to my pom.xml configuration:

        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>3.2.1</version>
            <configuration>
                <warSourceDirectory>WebContent</warSourceDirectory>
                <warSourceExcludes>**/*.css,**/*.js</warSourceExcludes>
            </configuration>
        </plugin>
        <plugin>
            <groupId>com.samaxes.maven</groupId>
            <artifactId>minify-maven-plugin</artifactId>
            <version>1.7.6</version>
            <executions>
                <execution>
                    <id>default-minify</id>
                    <phase>package</phase>
                    <configuration>
                        <charset>UTF-8</charset>
                        <webappSourceDir>${project.basedir}/WebContent</webappSourceDir>

                        <cssSourceDir>./</cssSourceDir>
                        <cssSourceIncludes>
                            <cssSourceInclude>**/*.css</cssSourceInclude>
                        </cssSourceIncludes>
                        <cssSourceExcludes>
                            <cssSourceExclude>**/*.min.css</cssSourceExclude>
                        </cssSourceExcludes>

                        <jsSourceDir>./</jsSourceDir>
                        <jsSourceIncludes>
                            <jsSourceInclude>**/*.js</jsSourceInclude>
                        </jsSourceIncludes>
                        <jsSourceExcludes>
                            <jsSourceExclude>**/*.min.js</jsSourceExclude>
                        </jsSourceExcludes>

                        <jsEngine>CLOSURE</jsEngine>
                        <nosuffix>true</nosuffix>
                        <skipMerge>true</skipMerge>
                    </configuration>
                    <goals>
                        <goal>minify</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

Please suggest a proper solution. Thanks in advance !


Solution

  • The answer given by @Kristof Neirynck for the question How to get maven to build a war with minified files using yuicompressor-maven-plugin works for the issue mentioned. I changed the <phase> property as prepare-package and added

    <webappTargetDir>${project.build.directory}/minify</webappTargetDir> 
    

    in minify-maven-plugin after <webappSourceDir> and in the maven-war-plugin I set the following configuration:

            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.2.1</version>
                <configuration>
                    <warSourceDirectory>WebContent</warSourceDirectory>
                    <webResources>
                        <resource>
                            <directory>${project.build.directory}/minify</directory>
                        </resource>
                    </webResources>
                </configuration>
            </plugin>
    

    After using this configuration, when I deploy the WAR file in the Tomcat the original files are replaced with the minified files.