在Jar包装之前,Yui Compressor文件没有刷新

时间:2014-09-19 20:16:43

标签: java css maven jar yui-compressor

我正在使用Maven yui压缩器插件,并使用Maven一个jar插件来创建容器js和css资源的jar文件。但是,似乎我的压缩css文件始终是“一个编译器”,这意味着我必须编译应用程序两次以获取css更改。这是我的构建插件的pom部分

        <!-- Coffee -->
        <plugin>
            <groupId>com.github.iron9light</groupId>
            <artifactId>coffeescript-maven-plugin</artifactId>
            <version>1.1.2</version>
            <configuration>
                <srcDir>src/main/resources</srcDir>
                <outputDir>src/main/resources</outputDir>
                <bare>false</bare>
                <modifiedOnly>false</modifiedOnly>
                <allowedDelete>true</allowedDelete>
            </configuration>
            <executions>
                <execution>
                    <id>coffeescript</id>
                    <phase>generate-resources</phase>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

        <!-- Sass -->
        <plugin>
            <groupId>org.jasig.maven</groupId>
            <artifactId>sass-maven-plugin</artifactId>
            <version>1.1.1</version>
            <executions>
                <execution>
                    <goals>
                        <goal>update-stylesheets</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <sassSourceDirectory>src/main/resources</sassSourceDirectory>
                <destination>src/main/resources</destination>
            </configuration>
        </plugin>

        <!-- YUI Compressor -->
        <plugin>
            <groupId>net.alchim31.maven</groupId>
            <artifactId>yuicompressor-maven-plugin</artifactId>
            <version>1.1</version>
            <executions>
                <execution>
                    <goals>
                        <goal>compress</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <excludes>
                    <exclude>**/*.min.js</exclude>
                    <exclude>**/*.min.css</exclude>
                </excludes>
                <suffix>.min</suffix>
                <outputDirectory>src/main/resources</outputDirectory>
            </configuration>
        </plugin>

        <!-- Compiler -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.0</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>

        <!-- Jar -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>com.example.application.web.JettyStarter</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>

        <!-- One Jar -->
        <plugin>
            <groupId>org.dstovall</groupId>
            <artifactId>onejar-maven-plugin</artifactId>
            <version>1.4.4</version>
            <executions>
                <execution>
                    <configuration>
                        <onejarVersion>0.97</onejarVersion>
                        <classifier>onejar</classifier>
                    </configuration>
                    <goals>
                        <goal>one-jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

当我检查我的源代码树时,我看到缩小的css在那里,但它不在jar中。我想比较一下,在一罐包装之前将文件“冲洗”出去。谁看过这个吗?

示例:我将此添加到style.css

.clear-both {
    clear: both;
}

在源代码树的style.min.css中

.clear-both{clear:both;}

但是当我通过服务器请求此文件时:

GET css/style.min.css

那条线不存在。

这可能与使用资源插件执行插件的顺序有关:

[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ MyApp ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 661 resources
[INFO]
[INFO] --- yuicompressor-maven-plugin:1.1:compress (default) @ MyApp ---
...

2 个答案:

答案 0 :(得分:1)

是的,这是因为maven-resources-plugin已在yuicompressor-maven-plugin之前执行,因此您的缩小文件未被复制到最终的jar中。

重新排列阶段,将帮助您解决此问题,但解决此问题的最佳方法是避免在源树中生成缩小/压缩/编译的文件。这些文件不应该是源代码树的一部分,但应该生成到构建的输出目录中。使用Maven的${project.build.directory}(目标目录)进行各种操作和处理。这还将确保在maven构建(mvn clean install)期间始终清除所有生成的文件

查看下面的构建代码段。我已在必要时加入评论。该片段是在假设所有咖啡脚本(。咖啡文件),saas模板(.scss文件),其他css(未压缩的.css文件),js(未压缩的.js文件)和其他文件(比如图像等)的情况下制作的。都出现在src / main / resources中。该片段只包装缩小的js和css,未压缩的js / css不会打包到jar中。

如果要对此进行测试,请在使用此代码段执行任何maven构建之前,从src \ main \ resources中删除所有生成的css文件,js文件和缩小文件。

<build>
<resources>
    <resource>
        <directory>src/main/resources</directory>
        <!-- removing coffee scripts, saas templates, js and css from the final 
            output,we will only allow compressed files to go into the final jar
            maven-resources-plugin:resources will respect these exclusions -->
        <excludes>
            <exclude>**/**.coffee</exclude>
            <exclude>**/**.scss</exclude>
            <exclude>**/**.css</exclude>
            <exclude>**/**.js</exclude>
        </excludes>
    </resource>
</resources>
<plugins>
    <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <groupId>org.apache.maven.plugins</groupId>
        <version>2.6</version>
        <executions>
            <!-- copy uncompressed css and js to a staging directory for compressing -->
            <execution>
                <id>copy-uncompressed-css-and-js</id>
                <phase>process-resources</phase>
                <goals>
                    <goal>copy-resources</goal>
                </goals>
                <configuration>
                    <resources>
                        <resource>
                            <directory>src/main/resources</directory>
                            <includes>
                                <include>**/*.js</include>
                                <include>**/*.css</include>
                            </includes>
                        </resource>
                    </resources>
                    <outputDirectory>${project.build.directory}/uncompressed-css-and-js</outputDirectory>
                </configuration>
            </execution>
        </executions>
    </plugin>
    <!-- Coffee -->
    <!-- compiling coffee scripts and generating the uncompressed js files 
        into a staging directory for compressing -->
    <plugin>
        <groupId>com.github.iron9light</groupId>
        <artifactId>coffeescript-maven-plugin</artifactId>
        <version>1.1.2</version>
        <configuration>
            <srcDir>src/main/resources/coffee</srcDir>
            <outputDir>${project.build.directory}/uncompressed-css-and-js</outputDir>
            <bare>false</bare>
            <modifiedOnly>false</modifiedOnly>
            <allowedDelete>true</allowedDelete>
        </configuration>
        <executions>
            <execution>
                <id>coffeescript</id>
                <phase>compile</phase>
                <goals>
                    <goal>compile</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    <!-- Sass -->
    <!-- compiling saas scripts and generating the uncompressed css files 
        into a staging directory for compressing -->
    <plugin>
        <groupId>org.jasig.maven</groupId>
        <artifactId>sass-maven-plugin</artifactId>
        <version>1.1.1</version>
        <executions>
            <execution>
                <id>saas</id>
                <phase>compile</phase>
                <goals>
                    <goal>update-stylesheets</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <sassSourceDirectory>src/main/resources/saas</sassSourceDirectory>
            <destination>${project.build.directory}/uncompressed-css-and-js</destination>
        </configuration>
    </plugin>
    <!-- YUI Compressor -->
    <!-- compressing all css/jss files in the staging directory and generating 
        the output in the target/classes folder(which will end up in the final jar -->
    <plugin>
        <groupId>net.alchim31.maven</groupId>
        <artifactId>yuicompressor-maven-plugin</artifactId>
        <version>1.1</version>
        <executions>
            <execution>
                <id>compress</id>
                <phase>process-classes</phase>
                <goals>
                    <goal>compress</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <suffix>.min</suffix>
            <sourceDirectory>${project.build.directory}/uncompressed-css-and-js</sourceDirectory>
            <outputDirectory>${project.build.outputDirectory}</outputDirectory>
        </configuration>
    </plugin>
</plugins>

参考:查看Maven default lifecycle

中阶段的顺序

答案 1 :(得分:0)

答案是将CoffeeScript,Sass和YUI压缩放在流程源阶段

<phase>process-sources</phase>