YUI压缩器Maven插件无法压缩JS文件。

6

我正在使用YUI压缩器来压缩我的Web应用程序中的JS文件。

我已按插件网站上指示的配置插件。

这是POM插件的配置:

   <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:我已经对日志中的一些文本进行了混淆,以遵循公司的合规规则,因此您可能会发现某些地方不匹配。

我遇到了类似的问题。 "minified" 文件保留了所有的换行符。虽然拼接起来可以用,但这很奇怪... - Benxamin
这个问题可能有解决方案,但插件版本1.5.1存在一个错误,因为它无法针对某些简单的情况进行配置。例如,如果我们想要配置webappDirectory和sourceDirectory。如果sourceDirectory与默认值不同,则插件会执行两次压缩,一次使用配置的值,另一次使用默认值,这样就破坏了sourceDirectory配置的目的。我无法从需要在压缩之前进行更改的目录中进行压缩。 - user250343
4个回答

5
根据我所读的,您没有配置检查或压缩js文件的位置(因为您没有使用默认配置)。您只配置了聚合。
要获得更快的响应,请使用yuicompressor-maven-plugin问题跟踪器
解决方案(从项目FAQ中复制/粘贴):
因为:
- 您设置了<nosuffix>true</nosuffix> - 您的脚本位于src/main/webapp下,并且maven-war-plugin会将src/main/webapp中的所有文件复制到target目录中(无论时间戳如何...),并在同一执行中创建war文件。
解决方法:
  • move your script dir out from 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>
    
  • OR exclude "scripts" from the list of files to copy

    <!-- 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>
    

2

我知道这篇文章很老,但我也遇到了同样的问题,这就是我找到的答案。希望这能帮助其他人。

这很棘手。这与nosuffix配置选项有关。如果您删除nosuffix选项,则缩小工作正常。

如果您确实需要nosuffix,则需要将执行阶段更改为“package”。

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

任何在“准备资源”阶段之前的步骤都不起作用,因为当WAR包构建完成后,它会从原始位置拾取js文件并覆盖目标目录中“准备资源”阶段创建的压缩过的js文件。如果在打包阶段移除“nosuffix”,压缩功能将正常运行,因为在打包阶段,文件名是不同的,所以不会发生文件覆盖,你会在目标目录中看到同时存在压缩和未压缩的js文件。至少这是我理解的。

1
在这种情况下,您在webapp目录中有经过缩小的文件,但在生成的.war文件中没有(检查内容,war插件先运行(复制+存档在单个传递中)。您必须排除复制的文件:请参见https://github.com/davidB/yuicompressor-maven-plugin/wiki/FAQ。 - David Bernard
1
你是对的。实际上,我通过在yuicompressor插件中添加<webappDirectory>,然后将该目录作为资源添加到maven-war-plugin中来解决了它。<configuration> <nosuffix>true</nosuffix> <jswarn>false</jswarn> <webappDirectory>${project.build.directory}/min</webappDirectory> - Inxsible

0

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

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

执行了两次,但仍然没有运气,情况还是一样的。 - hanumant
从pom中删除了jslint目标,只执行压缩以查看其是否有效。结果是不行的,压缩没有起作用,只是聚合了文件。 - hanumant

0
YUI压缩器(作为此Maven插件的依赖项)使用Mozilla Rhino的"补丁"版本来:
  • 解释JavaScript
  • 压缩本地变量名称("patch")
通过在pom中添加对Rhino的依赖关系(在您之前的问题中),我认为这些"补丁"将被覆盖,使得这个Maven插件成为无用的:名称压缩、删除空格行为将不复存在。
因此,请从您的pom中删除Rhino,回到解决您的第一个问题!

我遇到了同样的问题,而没有明确定义Rhino。 - Jan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接