YUI Compressor Maven插件不压缩JS文件

时间:2010-10-01 04:31:16

标签: maven-2 yui-compressor

我正在使用YUI Compressor压缩我的网络应用程序中的JS文件。

我已根据the plugin's site上的说明配置了该插件。

这是POM插件conf:

   <plugin>
    <groupId>net.sf.alchim</groupId>
    <artifactId>yuicompressor-maven-plugin</artifactId>
    <version>0.7.1</version>  
    <executions>
      <execution>
     <phase>compile</phase>
        <goals>
         <goal>jslint</goal>
          <goal>compress</goal>
        </goals>
      </execution>
    </executions>        
    <configuration>
    <failOnWarning>true</failOnWarning>
      <nosuffix>true</nosuffix>
      <force>true</force>
      <aggregations>
       <aggregation>
          <!-- remove files after aggregation (default: false) -->
          <removeIncluded>false</removeIncluded>
          <!-- insert new line after each concatenation (default: false) -->
          <insertNewLine>false</insertNewLine>
          <output>${project.basedir}/${webcontent.dir}/js/compressedAll.js</output>
          <!-- files to include, path relative to output's directory or absolute path-->
          <!--inputDir>base directory for non absolute includes, default to parent dir of output</inputDir-->
          <includes>                
            <include>**/autocomplete.js</include>
            <include>**/calendar.js</include>
            <include>**/dialogs.js</include>
            <include>**/download.js</include>
            <include>**/folding.js</include>
            <include>**/jquery-1.4.2.min.js</include>
            <include>**/jquery.bgiframe.min.js</include>
            <include>**/jquery.loadmask.js</include>
            <include>**/jquery.printelement-1.1.js</include>
            <include>**/jquery.tablesorter.mod.js</include>
            <include>**/jquery.tablesorter.pager.js</include>
            <include>**/jquery.dialogs.plugin.js</include>
            <include>**/jquery.ui.autocomplete.js</include>                
            <include>**/jquery.validate.js</include>  
            <include>**/jquery-ui-1.8.custom.min.js</include>
            <include>**/languageDropdown.js</include>
            <include>**/messages.js</include>
            <include>**/print.js</include>
            <include>**/tables.js</include>
            <include>**/tabs.js</include>
            <include>**/uwTooltip.js</include>
          </includes>
          <!-- files to exclude, path relative to output's directory-->

        </aggregation>
      </aggregations>
    </configuration>
    <dependencies>
         <dependency> 
 <groupId>rhino</groupId>  
  <artifactId>js</artifactId>       
  <scope>compile</scope>  
  <version>1.6R5</version> 
</dependency>  
<dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-plugin-api</artifactId>
        <version>2.0.7</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-project</artifactId>
        <version>2.0.7</version>
        <scope>provided</scope>
    </dependency><dependency>
        <groupId>net.sf.retrotranslator</groupId>
        <artifactId>retrotranslator-runtime</artifactId>
        <version>1.2.9</version>
        <scope>runtime</scope>
    </dependency>

    </dependencies>
  </plugin>

这是压缩时的日志:

These will use the artifact files already in the core ClassRealm instead, to allow them to be included in PluginDescriptor.getArtifacts().


[DEBUG] Configuring mojo 'net.sf.alchim:yuicompressor-maven-plugin:0.7.1:jslint' 
[DEBUG]   (f) failOnWarning = true
[DEBUG]   (f) jswarn = true
[DEBUG]   (f) outputDirectory = C:\test\target\classes
[DEBUG]   (f) project = MavenProject: com.test.test1:test2:19-SNAPSHOT @ C:\test\pom.xml
[DEBUG]   (f) resources = [Resource {targetPath: null, filtering: false, FileSet {directory: C:\test\src,
 PatternSet [includes: {}, excludes: {**/*.class, **/*.java, site/*}]}}]
[DEBUG]   (f) sourceDirectory = C:\test\src\..\js
[DEBUG]   (f) warSourceDirectory = C:\test\src\main\webapp
[DEBUG]   (f) webappDirectory = C:\test\target\test2-19-SNAPSHOT
[DEBUG] -- end configuration --
[INFO] [yuicompressor:jslint {execution: default}]
[INFO] nb warnings: 0, nb errors: 0
[DEBUG] Configuring mojo 'net.sf.alchim:yuicompressor-maven-plugin:0.7.1:compress' -->
[DEBUG]   (f) removeIncluded = false
[DEBUG]   (f) insertNewLine = false
[DEBUG]   (f) output = C:\test\WebContent\js\compressedAll.js
[DEBUG]   (f) includes = [**/autocomplete.js, **/calendar.js, **/dialogs.js, **/download.js, **/folding.js, **/jquery-1.4.2.min.js, **/jquery.bgifram
e.min.js, **/jquery.loadmask.js, **/jquery.printelement-1.1.js, **/jquery.tablesorter.mod.js, **/jquery.tablesorter.pager.js, **/jquery.dialogs.p
lugin.js, **/jquery.ui.autocomplete.js, **/jquery.validate.js, **/jquery-ui-1.8.custom.min.js, **/languageDropdown.js, **/messages.js, **/print.js, *
*/tables.js, **/tabs.js, **/uwTooltip.js]
[DEBUG]   (f) aggregations = [net.sf.alchim.mojo.yuicompressor.Aggregation@65646564]
[DEBUG]   (f) disableOptimizations = false
[DEBUG]   (f) encoding = Cp1252
[DEBUG]   (f) failOnWarning = true
[DEBUG]   (f) force = true
[DEBUG]   (f) gzip = false
[DEBUG]   (f) jswarn = true
[DEBUG]   (f) linebreakpos = 0
[DEBUG]   (f) nomunge = false
[DEBUG]   (f) nosuffix = true
[DEBUG]   (f) outputDirectory = C:\test\target\classes
[DEBUG]   (f) preserveAllSemiColons = false
[DEBUG]   (f) project = MavenProject: com.test.test1:test2:19-SNAPSHOT @ C:\test\pom.xml
[DEBUG]   (f) resources = [Resource {targetPath: null, filtering: false, FileSet {directory: C:\test\src,
 PatternSet [includes: {}, excludes: {**/*.class, **/*.java, site/*}]}}]
[DEBUG]   (f) sourceDirectory = C:\test\src\..\js
[DEBUG]   (f) statistics = true
[DEBUG]   (f) suffix = -min
[DEBUG]   (f) warSourceDirectory = C:\test\src\main\webapp
[DEBUG]   (f) webappDirectory = C:\test\target\test2-19-SNAPSHOT
[DEBUG] -- end configuration --
[INFO] [yuicompressor:compress {execution: default}]
[INFO] generate aggregation : C:\test\WebContent\js\compressedAll.js
[INFO] compressedAll.js (407505b)
[INFO] nb warnings: 0, nb errors: 0
[DEBUG] Configuring mojo 'org.apache.maven.plugins:maven-resources-plugin:2.2:testResources' -->
[DEBUG]   (f) filters = []
[DEBUG]   (f) outputDirectory = C:\test\target\test-classes
[DEBUG]   (f) project = MavenProject: com.test.test1:test2:19-SNAPSHOT @ C:\test\pom.xml
[DEBUG]   (f) resources = [Resource {targetPath: null, filtering: false, FileSet {directory: C:\test\test
, PatternSet [includes: {}, excludes: {**/*.class, **/*.java}]}}]
[DEBUG] -- end configuration --

问题是,虽然文件聚合成一个文件,但它没有压缩。

上面的链接使用版本1.1,我使用的插件版本是0.7.1。这会有什么不同吗?

有人能告诉我这里有什么问题吗?

PS:我在日志中混淆了一些文字以遵守我公司的合规规则,因此您可能会在某些地方发现不匹配。

4 个答案:

答案 0 :(得分:5)

根据我的阅读,您不配置应检查或压缩js文件的位置(因为您不使用默认配置)。您只需配置聚合。

为了更快速地使用了issue tracker of yuicompressor-maven-plugin

解决方案(从project FAQ复制/粘贴)

因为:

  • 您设置<nosuffix>true</nosuffix>

  • 你的脚本在src / main / webapp和maven-war-plugin下将src / main / webapp中的每个文件复制到目标(无论时间戳,......)并进行战争(在同一执行中)

解决方法:

  • 将您的脚本目录从webapp

    移出
    <plugin>
      <groupId>@project.groupId@</groupId>
      <artifactId>yuicompressor-maven-plugin</artifactId>
      ...
      <configuration>
        <!-- default ${project.build.sourceDirectory}/../js == src/main/js -->
        <sourceDirectory>src/main/javascript</sourceDirectory>
    
  • 或者从要复制的文件列表中排除“脚本”

    <!-- adapted from https://github.com/davidB/yuicompressor-maven-plugin/blob/master/src/it/demo01/pom.xml -->
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-war-plugin</artifactId>
      <configuration>
        <warSourceExcludes>scripts/**</warSourceExcludes>
      </configuration>
    </plugin>
    

答案 1 :(得分:2)

我知道这篇文章很老,但我遇到了同样的事情,这就是我发现的。希望这也有助于其他人。

这很棘手。这与nosuffix配置选项有关。如果删除nosuffix选项,则缩小按预期工作。

如果你真的必须有nosuffix,那么你需要将执行阶段改为“package”。

    <executions>
      <execution>
        <phase>package</phase>
          <goals>
    ......
    .....

在准备资源之前的任何阶段都不起作用,因为在构建战争时,它从原始源位置获取js,从而覆盖目标目录中的缩小的js(在准备资源阶段创建)。

当你删除nosuffix时缩小是有效的,因为在打包阶段,文件名不同,所以没有文件的覆盖,你会在目标目录中看到缩小和非缩小的js文件。

至少这是我的理解。

答案 2 :(得分:0)

您可以尝试为jslint定义单独的执行并压缩,看看是否有效吗?

<executions>
    <execution>
        <goals>
            <goal>jslint</goal>         
        </goals>
        <configuration> ... </configuration>
    </execution>
    <execution>
        <goals>
            <goal>compress</goal>         
        </goals>
        <configuration> ... </configuration>
    </execution>
</executions>

答案 3 :(得分:0)

YUI压缩器(作为此maven插件中的依赖项包含)使用“修补”版本的Mozilla Rhino:

  • 解释javascript
  • 压缩局部变量名称(“补丁”)

通过将自己的依赖添加到Rhino(in your previous question),我认为这些“补丁”被覆盖,将这个maven插件变成无用的东西:名称压缩,空格删除行为消失了。

所以从你的pom中移除rhino,然后回去解决你的第一个问题!