2014-09-19 91 views
0

我使用Maven yui压缩程序插件和Maven一个jar插件来创建一个容器js和css资源的jar文件。看起来,虽然,我的压缩的CSS文件总是“一个编译后面”,这意味着我必须编译应用程序两次,以接受一个CSS更改。这里是我的构建POM的部分插件Yui压缩器文件在Jar包装之前未刷新

 <!-- 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是存在的,但它不是在瓶子里。我想比较一下,在单瓶包装之前将文件“冲洗”出来。有没有人见过这个?

例子:我已将此添加的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 --- 
... 
+0

我已经更新了答案排序,但它可以得到改善。什么都出现在您的src/main/resources目录中?它只有.coffee脚本和saas源文件吗? – coderplus 2014-09-21 04:21:58

+0

好的问题,是的,关键是要把他们放在流程来源阶段。我会看看project.build。目录也是如此,谢谢你的支持。 – 2014-09-21 18:21:38

回答

1

是的,这是由于maven-resources-pluginyuicompressor-maven-plugin之前执行的事实,因此您的缩小文件未被复制到最终的jar文件中。

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

看看下面的构建片段。我已经在必要时加入了评论。这段代码是基于所有的咖啡脚本(.coffee文件),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

0

答案是把CoffeeScript的,无礼的话,和YUI压缩的过程中源相

<phase>process-sources</phase>