YUI压缩机压缩和替换构建

时间:2013-03-07 14:12:09

标签: java javascript maven yui

我试图为maven项目找到js/css压缩工具,所以我找到了这个帖子 - Maven Javascript Compressor

一切正常,但有一个问题我还在试图解决。

如果未设置后缀yui,则默认设置*-min并按旧文件创建新文件。所以,例如,如果我在使用该插件构建maven后有scripts.js,我将同时拥有scripts.jsscripts-min.js个文件。我只需要*-min文件。如果我将<nosuffix>设置为true,则表示压缩成功,但它不会覆盖该文件。

有没有办法只能使用同名的压缩文件?

2 个答案:

答案 0 :(得分:0)

结束为maven ant插件编写这个ant脚本:

<delete includeEmptyDirs="false">
    <fileset dir="${basedir}/target/${project.build.finalName}/resources/js" excludes="**/*-min.js" />          
</delete>
<move todir="${basedir}/target/${project.build.finalName}/resources/js" includeemptydirs="false">
    <fileset dir="${basedir}/target/${project.build.finalName}/resources/js" />
    <mapper type="glob" from="*-min.js" to="*.js" />
</move>

<delete includeEmptyDirs="false">
<fileset dir="${basedir}/target/${project.build.finalName}/resources/css"
    excludes="**/*-min.css, **/*.ttf, **/*.png, **/*.jpg" />
</delete>
<move todir="${basedir}/target/${project.build.finalName}/resources/css" includeemptydirs="false">
    <fileset dir="${basedir}/target/${project.build.finalName}/resources/css" />
    <mapper type="glob" from="*-min.css" to="*.css" />
</move>

简要说明:该脚本首先删除旧的未压缩文件,然后将*-min.js*-min.css文件重命名为*.js*.css

工作正常,刚刚测试过。此外,如果您在/js/css文件夹(不是.js.css)中有一些不同的文件,请确保您没有删除这些文件,并将这些文件放入在exclude属性中。

答案 1 :(得分:0)

是的,您正在使用的maven Javascript Compressor会压缩文件。 但在 maven包装生命周期期间,压缩发生在 prepare-package 阶段,之后,在包装阶段期间,maven-war-插件将复制原始文件并覆盖压缩器刚刚执行的操作。 下面的配置是我如何解决问题,我将js文件压缩到另一个文件夹,并在打包阶段将其复制回来。

    <plugin>
            <groupId>net.alchim31.maven</groupId>
            <artifactId>yuicompressor-maven-plugin</artifactId>
            <version>1.5.1</version>
            <executions>
                <execution>
                <id>yui-compress</id>
                <phase>prepare-package</phase>
                    <goals>
                        <goal>compress</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <excludes>
                    <exclude>**/*-min.js</exclude>
                    <exclude>**/*.min.js</exclude>
                    <exclude>**/*-min.css</exclude>
                    <exclude>**/*.min.css</exclude>
                    <exclude>**/amazeui*</exclude>
                    <exclude>**/*.css</exclude>
                </excludes>
                <encoding>UTF-8</encoding>
                <nosuffix>true</nosuffix>
                <statistics>false</statistics>
                <linebreakpos>5000</linebreakpos>
                <useSmallestFile>true</useSmallestFile>
                <jswarn>false</jswarn>
                <warSourceDirectory>${basedir}/web</warSourceDirectory>
                <webappDirectory>${project.build.directory}/min</webappDirectory>
                <skip>false</skip>
            </configuration>
        </plugin> 
         <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
                <webResources>
                    <resource>
                        <directory>${project.build.directory}/min</directory>
                    </resource>
                </webResources>
            </configuration>
        </plugin>