2

I am trying to use the YUI compressor maven plugin in my Spring MVC Web Application by following the instructions in the http://www.baeldung.com/maven-minification-of-js-and-css-assets. I have added the following to my pom.xml file:

   <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>
               <webappDirectory>${project.build.directory}/min</webappDirectory>
                <excludes>
                    <exclude>**/*.min.js</exclude>
                    <exclude>**/handlebars-3133af2.js</exclude>
                    <exclude>**/require.js</exclude>
                </excludes>
            </configuration>
    </plugin>

I have many CSS and JS files in the sub-folders under src/main/webapp and according to the documentation, all these should be minified. But when I run the maven clean install, neither do I see any log related to minification in my console nor do I find any minified file in my war file.

All I want is to save the minified file under a subfolder named min with the same filename in the same folder as the existing CSS or JS file

The console log now is:

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building smartwcm-services 6.3.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ smartwcm-services ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 36 resources
[INFO] 
[INFO] --- yuicompressor-maven-plugin:1.5.1:compress (default) @ smartwcm-services ---
[ERROR] E:\projects\smartwcm\source-code\smartwcm-services\src\main\webapp\layout\common\adminv3\ca\css\content\design\azure.js [1:2]: illegal character
[ERROR] ...E:\projects\smartwcm\source-code\smartwcm-services\src\main\webapp\layout\common\adminv3\ca\css\content\design\azure.js:line 1:column 2:illegal character
    /* Note: jquery script is assumed to be loaded prior to this script */
[ERROR] E:\projects\smartwcm\source-code\smartwcm-services\src\main\webapp\layout\common\adminv3\ca\css\content\design\azure.js [1:2]: syntax error
[ERROR] ...E:\projects\smartwcm\source-code\smartwcm-services\src\main\webapp\layout\common\adminv3\ca\css\content\design\azure.js:line 1:column 2:syntax error
    /* Note: jquery script is assumed to be loaded prior to this script */
[ERROR] E:\projects\smartwcm\source-code\smartwcm-services\src\main\webapp\layout\common\adminv3\ca\css\content\design\azure.js [1:3]: illegal character
[ERROR] ...E:\projects\smartwcm\source-code\smartwcm-services\src\main\webapp\layout\common\adminv3\ca\css\content\design\azure.js:line 1:column 3:illegal character
    /* Note: jquery script is assumed to be loaded prior to this script */
[ERROR] E:\projects\smartwcm\source-code\smartwcm-services\src\main\webapp\layout\common\adminv3\ca\css\content\design\azure.js [1:0]: Compilation produced 3 syntax errors.
[ERROR] ...E:\projects\smartwcm\source-code\smartwcm-services\src\main\webapp\layout\common\adminv3\ca\css\content\design\azure.js:line 1:column 0:Compilation produced 3 syntax errors.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.060 s
[INFO] Finished at: 2016-12-29T11:25:32+05:30
[INFO] Final Memory: 19M/111M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal net.alchim31.maven:yuicompressor-maven-plugin:1.5.1:compress (default) on project smartwcm-services: Execution default of goal net.alchim31.maven:yuicompressor-maven-plugin:1.5.1:compress failed: Compilation produced 3 syntax errors. -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginExecutionException

1 Answer 1

1

If you run without nosuffix option in your pom.xml like the configuration you have posted you will get mimimised files in target\min folder. You will see below log in maven

[INFO] --- yuicompressor-maven-plugin:1.5.1:compress (default) @ spring-static-resources ---
[INFO] bootstrap.css (127343b) -> bootstrap-min.css (106005b)[83%]
[INFO] foo.js (44b) -> foo-min.js (37b)[84%]
[INFO] utils.js (48b) -> utils-min.js (28b)[58%]
[INFO] main.js (138b) -> main-min.js (100b)[72%]
[INFO] router.js (86b) -> router-min.js (64b)[74%]
[INFO] bootstrap.css (127343b) -> bootstrap-min.css (106005b)[83%]
[INFO] foo.js (44b) -> foo-min.js (37b)[84%]
[INFO] bootstrap.css (127343b) -> bootstrap-min.css (106005b)[83%]
[INFO] myCss.css (127343b) -> myCss-min.css (106005b)[83%]
[INFO] bootstrap.css (127343b) -> bootstrap-min.css (106005b)[83%]
[INFO] total input (637075b) -> output (530291b)[83%]

When you run with <nosuffix>true</nosuffix> you will get below log. This option will give you minified files in target\min folder but will keep the file name same.

[INFO] --- yuicompressor-maven-plugin:1.5.1:compress (default) @ spring-static-resources ---
[INFO] bootstrap.css (127343b) -> bootstrap.css (106005b)[83%]
[INFO] foo.js (44b) -> foo.js (37b)[84%]
[INFO] utils.js (48b) -> utils.js (28b)[58%]
[INFO] main.js (138b) -> main.js (100b)[72%]
[INFO] router.js (86b) -> router.js (64b)[74%]
[INFO] bootstrap.css (127343b) -> bootstrap.css (106005b)[83%]
[INFO] foo.js (44b) -> foo.js (37b)[84%]
[INFO] bootstrap.css (127343b) -> bootstrap.css (106005b)[83%]
[INFO] myCss.css (127343b) -> myCss.css (106005b)[83%]
[INFO] bootstrap.css (127343b) -> bootstrap.css (106005b)[83%]
[INFO] total input (637075b) -> output (530291b)[83%]

To include these minimised files in war and not the original ones you need to set <webappDirectory> in pom.xml. So your configuration should look like below

 <plugin>
         <groupId>net.alchim31.maven</groupId>
            <artifactId>yuicompressor-maven-plugin</artifactId>
            <version>${yuicompressor-maven-plugin.version}</version>
            <executions>
                <execution>
                    <goals>
                        <goal>compress</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <nosuffix>true</nosuffix>
               <webappDirectory>${project.build.directory}/min</webappDirectory>
                <excludes>
                    <exclude>**/*.min.js</exclude>
                    <exclude>**/handlebars-3133af2.js</exclude>
                    <exclude>**/require.js</exclude>
                </excludes>
            </configuration>
    </plugin>

Above logs are from github project https://github.com/eugenp/tutorials/tree/master/handling-spring-static-resources. Check the pom.xml for reference in this project.

Sign up to request clarification or add additional context in comments.

7 Comments

I dont see anything in my log with the current configuration
Can you please post your mvn console log? Are you running the build for same github project?
I have updated the question with the log and the updated pom.xml file
Build is failing as there are some errors in "azure.js" file. Can you please exclude this file as well and try? Or see if you can fix "syntax error" and "illegal character" error as shown in maven build log in "azure.js".
[INFO] --- yuicompressor-maven-plugin:1.5.1:compress (default) @ smartwcm-services --- [INFO] content-list.css (755b) -> content-list.css (673b)[89%] [INFO] content.css (684b) -> content.css (544b)[79%] [ERROR] Failed to execute goal net.alchim31.maven:yuicompressor-maven-plugin:1.5.1:compress (default) on project smartwcm-services: Execution default of goal net.alchim31.maven:yuicompressor-maven-plugin:1.5.1:compress failed: String index out of range: 1709 -> [Help 1], after excluding azure.js
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.