maven实现js、css文件压缩

https://blog.csdn.net/qq34847009/article/details/84181416

不支持:let语法

<plugin>
    <!-- YUI Compressor Maven压缩插件 -->
    <groupId>net.alchim31.maven</groupId>
    <artifactId>yuicompressor-maven-plugin</artifactId>
    <version>1.5.1</version>
    <executions>
        <execution>
            <phase>prepare-package</phase>
            <goals>
                <!-- 只能最小化,不能混淆 -->
                <goal>compress</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <!-- 读取js,css文件采用UTF-8编码 -->
        <encoding>UTF-8</encoding>
        <!-- 不显示js可能的错误 -->
        <jswarn>false</jswarn>
        <!-- 若存在已压缩的文件,会先对比源文件是否有改动  有改动便压缩,无改动就不压缩 -->
        <force>true</force>
        <!-- 压缩成一行 -->
        <linebreakpos>-1</linebreakpos>
        <!-- 压缩之前先执行聚合文件操作 -->
        <preProcessAggregates>true</preProcessAggregates>
        <!-- 压缩后保存文件后缀 -->
        <suffix>.min</suffix>
        <!-- 源目录,即需压缩的根目录 -->
        <sourceDirectory>src/main/resources/static/</sourceDirectory>
        <!-- 输出目录,即压缩后的目录-->
        <outputDirectory>src/main/resources/static/</outputDirectory>
        <!-- 压缩js和css文件 -->
        <includes>
            <include>**/*.js</include>
            <include>**/*.css</include>
        </includes>
        <!-- 以下目录和文件不会被压缩 -->
        <excludes>
            <exclude>**/jquery-ui.css</exclude>
            <exclude>**/jquery-3.2.1.js</exclude>
            <exclude>**/jquery-ui.js</exclude>
            <exclude>**/jquery.validate.js</exclude>
            <exclude>**/*.min.js</exclude>
            <exclude>**/*.min.css</exclude>
        </excludes>
    </configuration>
</plugin>

https://stackoverflow.com/questions/49000121/spring-boot-minify-javascript

我用这个

 

<!-- https://blog.csdn.net/lianggeblog/article/details/100047723 -->
<plugin>
    <!-- https://mvnrepository.com/artifact/com.samaxes.maven/minify-maven-plugin -->
    <!-- 可以压缩css和js文件,但缺点是生成压缩后的目标文件不能是(ES6=>ECMASCRIPT2015)版本,使用WHITESPACE_ONLY模式时也不行,最高只能生成ES5。 -->
    <groupId>com.samaxes.maven</groupId>
    <artifactId>minify-maven-plugin</artifactId>
    <version>${minify-maven-plugin.version}</version>
    <executions>
        <execution>
            <id>minify-js-css</id>
            <phase>prepare-package</phase>
            <configuration>
                <!-- 读取js,css文件采用UTF-8编码 -->
                <charset>UTF-8</charset>
                <!-- 源目录,即需压缩的根目录 -->
                <webappSourceDir>${basedir}/src/main/resources/static</webappSourceDir>
                <!-- 输出目录,即压缩后的目录-->
                <webappTargetDir>${project.build.outputDirectory}/static</webappTargetDir>
                <!-- js压缩目录 -->
                <jsSourceDir>js</jsSourceDir>
                <!-- 压缩js文件 -->
                <jsSourceIncludes>
                    <include>**/*.js</include>
                </jsSourceIncludes>
                <!-- 以下文件不会被压缩 -->
                <jsSourceExcludes>
                    <exclude>**/*.min.js</exclude>
                    <exclude>**/mapObj.js</exclude>
                </jsSourceExcludes>
                <!-- css压缩目录 -->
                <cssSourceDir>css</cssSourceDir>
                <!-- 压缩css文件 -->
                <cssSourceIncludes>
                    <include>**/*.css</include>
                </cssSourceIncludes>
                <!-- 以下文件不会被压缩 -->
                <cssSourceExcludes>
                    <exclude>**/*.min.css</exclude>
                </cssSourceExcludes>
                <!-- js压缩引擎,默认值为YUI。可选值: -->
                <!-- YUI: 使用YUI Compressor压缩; -->
                <!-- CLOSURE: 使用Google Closure Compiler压缩 -->
                <jsEngine>CLOSURE</jsEngine>
                <!-- 使用closure压缩时的压缩级别,默认值为SIMPLE_OPTIMIZATIONS。可选值: -->
                <!-- WHITESPACE_ONLY:只压缩空格和转换一些特殊符号 -->
                <!-- SIMPLE_OPTIMIZATIONS:简单的压缩 -->
                <!-- ADVANCED_OPTIMIZATIONS:高级压缩,此压缩方式下可能会将引用的第3方框架的方法名称修改,导致js报错;慎用。 -->
                <closureCompilationLevel>SIMPLE_OPTIMIZATIONS</closureCompilationLevel>
                <!-- 不合并js/css -->
                <skipMerge>true</skipMerge>
                <!-- 压缩后保存文件后缀 -->
                <suffix>.min</suffix>
                <!-- 显示附加信息的消息和警告 -->
                <verbose>true</verbose>
            </configuration>
            <goals>
                <!-- 只能最小化,不能混淆 -->
                <goal>minify</goal>
            </goals>
        </execution>
    </executions>
</plugin>
<plugin>
    <!-- https://mvnrepository.com/artifact/com.github.blutorange/closure-compiler-maven-plugin -->
    <!-- 可以支持生成压缩后的js文件格式为(ES6=>ECMASCRIPT_2015)以后的版本,但是不支持css压缩。 -->
    <groupId>com.github.blutorange</groupId>
    <artifactId>closure-compiler-maven-plugin</artifactId>
    <version>${closure-compiler-maven-plugin.version}</version>
    <executions>
        <execution>
            <id>minify-es6-only</id>
            <phase>prepare-package</phase>
            <configuration>
                <!-- https://blutorange.github.io/closure-compiler-maven-plugin/minify-mojo.html -->
                <!-- 读取js文件采用UTF-8编码 -->
                <charset>UTF-8</charset>
                <encoding>UTF-8</encoding>
                <!-- 源目录,即需压缩的根目录 -->
                <baseSourceDir>${basedir}/src/main/resources/static</baseSourceDir>
                <!-- 输出目录,即压缩后的目录-->
                <baseTargetDir>${project.build.outputDirectory}/static</baseTargetDir>
                <!-- js压缩目录 -->
                <sourceDir>js</sourceDir>
                <!-- 压缩js文件 -->
                <includes>
                    <include>mapObj.js</include>
                </includes>
                <!-- 以下文件不会被压缩 -->
                <excludes>
                    <exclude>**/*.min.js</exclude>
                </excludes>
                <!-- https://github.com/google/closure-compiler -->
                <!-- 使用closure压缩时的压缩级别,默认值为SIMPLE_OPTIMIZATIONS。可选值: -->
                <!-- WHITESPACE_ONLY:只压缩空格和转换一些特殊符号 -->
                <!-- SIMPLE_OPTIMIZATIONS:简单的压缩 -->
                <!-- ADVANCED_OPTIMIZATIONS:高级压缩,此压缩方式下可能会将引用的第3方框架的方法名称修改,导致js报错;慎用。 -->
                <closureCompilationLevel>SIMPLE_OPTIMIZATIONS</closureCompilationLevel>
                <closureEmitUseStrict>true</closureEmitUseStrict>
                <!-- js压缩后的文件格式版本,注意与浏览器的兼容性,默认输出(ES6=>ECMASCRIPT_2015),(ES8=>ECMASCRIPT_2017) -->
                <closureLanguageOut>ECMASCRIPT_2017</closureLanguageOut>
                <!-- https://github.com/google/closure-compiler/wiki/Warnings -->
                <!-- https://blutorange.github.io/closure-compiler-maven-plugin/minify-mojo.html -->
                <skipMerge>true</skipMerge>
                <skipMinify>false</skipMinify>
            </configuration>
            <goals>
                <!-- 只能最小化,不能混淆 -->
                <goal>minify</goal>
            </goals>
        </execution>
    </executions>
</plugin>

 

https://blog.csdn.net/qq34847009/article/details/84181416

复杂没用

<plugin>
    <!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-dependency-plugin -->
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>3.1.2</version>
    <executions>
        <execution>
            <phase>validate</phase>
            <goals>
                <goal>copy</goal>
            </goals>
            <configuration>
                <artifactItems>
                    <artifactItem>
                        <!-- https://mvnrepository.com/artifact/com.google.javascript/closure-compiler -->
                        <groupId>com.google.javascript</groupId>
                        <artifactId>closure-compiler</artifactId>
                        <version>v20210106</version>
                        <type>jar</type>
                        <overWrite>true</overWrite>
                        <outputDirectory>${project.build.directory}/compressor</outputDirectory>
                        <destFileName>closure-compiler.jar</destFileName>
                    </artifactItem>
                    <artifactItem>
                        <!-- https://mvnrepository.com/artifact/com.yahoo.platform.yui/yuicompressor -->
                        <groupId>com.yahoo.platform.yui</groupId>
                        <artifactId>yuicompressor</artifactId>
                        <version>2.4.8</version>
                        <type>jar</type>
                        <overWrite>true</overWrite>
                        <outputDirectory>${project.build.directory}/compressor</outputDirectory>
                        <destFileName>yuicompressor.jar</destFileName>
                    </artifactItem>
                </artifactItems>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-antrun-plugin -->
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.8</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <target>
                    <echo message="开始压缩 JS 文件. 请耐心等待!" />
                    <property name="yuicompressor" value="${project.build.directory}/compressor/yuicompressor.jar" />
                    <property name="closure-compiler" value="${project.build.directory}/compressor/closure-compiler.jar" />
                    <property name="css.compress.directory" value="${project.basedir}/src/main/resources/static/css" />
                    <property name="js.compress.directory" value="${project.basedir}/src/main/resources/static/js" />
                    <apply executable="java" parallel="false" failonerror="true" dest="${js.compress.directory}">
                        <fileset dir="${js.compress.directory}" includes="**/*.js" />
                        <arg line="-jar" />
                        <arg path="${closure-compiler}" />
                        <arg line="--warning_level DEFAULT" />
                        <arg line="--compilation_level SIMPLE_OPTIMIZATIONS" />
                        <arg line="--js" />
                        <srcfile />
                        <arg line="--js_output_file" />
                        <targetfile />
                        <mapper type="regexp" from="^(.*)\.(js)$" to="\1.min.\2" />
                    </apply>
<!--                                <move todir="${js.compress.directory}">-->
<!--                                    <fileset dir="${js.compress.directory}" includes="**/*.js" />-->
<!--                                    <mapper type="regexp" from="^(.*)-min\.(js)$" to="\1.\2" />-->
<!--                                </move>-->
                    <echo message="成功压缩 JS 文件" />
                    <echo message="开始压缩 CSS 文件. 请耐心等待!" />
                    <apply executable="java" parallel="false" failonerror="true" dest="${css.compress.directory}">
                        <fileset dir="${css.compress.directory}" includes="**/*.css" />
                        <arg line="-jar" />
                        <arg path="${yuicompressor}" />
                        <arg line="--charset UTF-8" />
                        <arg value="--type"/>
                        <arg value="css"/>
                        <srcfile />
                        <arg line="-o" />
                        <targetfile />
                        <mapper type="regexp" from="^(.*)\.(css)$" to="\1.min.\2" />
                    </apply>
<!--                                <move todir="${css.compress.directory}">-->
<!--                                    <fileset dir="${css.compress.directory}" includes="**/*.css" />-->
<!--                                    <mapper type="regexp" from="^(.*)-min\.(css)$" to="\1.\2" />-->
<!--                                </move>-->
                    <echo message="成功压缩 CSS 文件" />
                </target>
            </configuration>
        </execution>
    </executions>
</plugin>

 

 

 

posted @ 2021-02-20 11:25  —八戒—  阅读(1043)  评论(0编辑  收藏  举报