Usage of YUI Compressor Maven Mojo minifying javascript

会有一股神秘感。 提交于 2019-12-22 21:25:10

问题


I work on a struts2 project using maven to compile. I am trying to minify the javascript files, which are located in different locations.

<plugin>
    <groupId>net.alchim31.maven</groupId>
    <artifactId>yuicompressor-maven-plugin</artifactId>
    <version>1.3.0</version>
    <executions>
      <execution>
        <goals>
          <goal>compress</goal>
        </goals>
      </execution>
    </executions>        
    <configuration>
      <nosuffix>true</nosuffix>
    </configuration>
  </plugin>

I assume that by doing this, all js files will be minified and replaced the original file in the production war file (according to definition for nosuffix).

However it doesn't seem to be the case. How can I acheve this?

Secondly, if I choose to use the one with the suffix, I assume I have to manually change the scripts reference in my jsp files is that correct? If so, how do I set it so that it will remove the original without the suffix?

Thanks.


回答1:


I have found working solution here. Basically you need to place the following into you pom.xml (replacing only the path to you js and css files inside src/main/webapp folder):

    <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
                <webResources>
                    <!-- Add minified resources -->
                    <resource>
                        <directory>${project.build.directory}/minimized</directory>
                        <targetPath>/</targetPath>
                        <filtering>false</filtering>
                    </resource>
                </webResources>
            </configuration>
        </plugin>

        <plugin>
            <groupId>net.alchim31.maven</groupId>
            <artifactId>yuicompressor-maven-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>compress</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <nosuffix>true</nosuffix>
            </configuration>
        </plugin>
    </plugins>

    <pluginManagement>
        <plugins>
            <!-- Javascript and CSS files compression -->
            <plugin>
                <groupId>net.alchim31.maven</groupId>
                <artifactId>yuicompressor-maven-plugin</artifactId>
                <version>1.1</version>
                <configuration>
                    <!-- Don't output in the default webapp location, since the war plugin will overwrite the files in there
                    with the original, uncompressed ones. -->
                    <webappDirectory>${project.build.directory}/minimized</webappDirectory>
                    <jswarn>false</jswarn>
                    <!-- Overwrite existing files -->
                    <nosuffix>true</nosuffix>
                    <includes>
                        <include>%path to your js and css files inside src/main/webapp%/**/*</include>
                    </includes>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
    </build>


来源:https://stackoverflow.com/questions/11836599/usage-of-yui-compressor-maven-mojo-minifying-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!