YUI Compressor Maven 插件不压缩 JS 文件

发布于 2024-09-25 19:38:25 字数 7626 浏览 4 评论 0原文

我正在使用 YUI 压缩机来压缩我的网络应用程序中的 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:为了遵循我公司的合规规则,我对日志中的一些文本进行了混淆,因此您可能会发现某些地方不匹配。

I am using YUI Compressor to compress the JS files in my web app.

I have configured the plugin as indicated on the plugin's site.

This is the POM plugin 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>

And here is the log at compress time:

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

The problem is, although the files are getting aggregated into one file, it's happening without compression.

The link above uses version 1.1 and the plugin version I am using is 0.7.1. Is this going to make any difference?

Can someone tell what is wrong here?

PS: I have obfuscated some text in the log to follow the compliance rules of my firm, so you may find mismatches in some places.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

鯉魚旗 2024-10-02 19:38:25

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

为了更快地响应,使用 yuicompressor-maven-plugin 的问题跟踪器

解决方案(复制/paste from 项目常见问题解答

因为:

  • 你设置了true

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

解决方法:

  • 将脚本目录从 web 应用程序中移出

    <前><代码><插件>;
    @project.groupId@
    yuicompressor-maven-plugin;
    ...
    <配置>
    src/main/javascript

  • 或从要复制的文件列表中排除“脚本”

    
    <插件>
      org.apache.maven.plugins
      maven-war-plugin;
      <配置>
        scripts/**
      
    
    

From what I read, you don't configure the place from where js file should be checked or compressed (as you don't use the default configuration). You only configure the aggregation.

For quicker response used the issue tracker of yuicompressor-maven-plugin

The solution (copy/paste from project FAQ )

Because :

  • you set <nosuffix>true</nosuffix>

  • your script in under src/main/webapp and maven-war-plugin copy every files from src/main/webapp to target (regardless timestamp,...) and make the war (in the same execution).

Workarounds :

  • 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>
    
茶色山野 2024-10-02 19:38:25

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

这很棘手。这与 nosuffix 配置选项有关。如果删除 nosuffix 选项,缩小将按预期进行。

如果你确实必须有无后缀,那么你需要将执行阶段更改为“package”。

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

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

当您删除 nosuffix 时,缩小会起作用,因为在打包阶段,文件名不同,因此不会覆盖文件,您将在目标目录中看到缩小和未缩小的 js 文件。

至少这是我的理解。

I know this post is very old, but I came across the same thing and this is what I found. Hopefully this will help someone else as well.

This is tricky. This has to do with the nosuffix config option. If you remove the nosuffix option, the minification works as expected.

If you really have to have nosuffix then you need to change the execution phase to "package".

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

Any phase before prepare-resources doesn't work because when the war is built, it picks up the js from the original source location and thereby overwrites the minified js (created during prepare-resources phase) in the target directory.

Minification works when you remove nosuffix because during package phase, the file names are different, so there is no over-writing of the files and you will see both the minified and non-minified js files in your target directory.

At least that's my understanding.

弥枳 2024-10-02 19:38:25

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

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

Can you try defining separate execution for jslint and compress and see if that works?

<executions>
    <execution>
        <goals>
            <goal>jslint</goal>         
        </goals>
        <configuration> ... </configuration>
    </execution>
    <execution>
        <goals>
            <goal>compress</goal>         
        </goals>
        <configuration> ... </configuration>
    </execution>
</executions>
三生一梦 2024-10-02 19:38:25

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

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

通过将依赖项添加到 Rhino (在您上一个问题中),我认为这些“补丁”被覆盖了,把这个 Maven 插件变成了无用的东西:名称压缩、空格删除行为消失了。

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

The YUI compressor (included as a dependency in this maven plugin) uses a "patched" version of Mozilla Rhino for:

  • interpreting javascript
  • compressing local variables names (the "patch")

By adding yourself the dependency to Rhino (in your previous question), I think these "patches" are overwritten, turning this maven plugin into something useless: the name-compressing, spaces-deleting behaviours are gone.

So remove rhino from your pom, and go back to solving your first problem!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文