2010-10-01 101 views
6

我正在使用yui压缩器来压缩我的web应用程序中的js文件。yui压缩机maven插件不压缩js文件

我已经按照yui maven插件站点yui compressor maven plugin上的指示配置了插件。

这是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 = [[email protected]] [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:我在日志中混淆了一些文字,以遵循我公司的合规性。所以你可能会发现它在某个地方不匹配。

+0

我有类似的问题。 “缩小”文件保留所有换行符。不过,连接起作用。很奇怪...... – Benxamin 2012-10-03 23:09:08

回答

0

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

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

执行为两个不同的执行。但没有运气仍然是相同的情况。 – hanumant 2010-10-01 05:04:22

+0

从pom中删除了jslint目标,并仅执行了压缩以查看它是否有效。结果是否定的,压缩不起作用只是聚合。 – hanumant 2010-10-01 06:38:04

0

的YUI压缩机(包括在此Maven插件的依赖)使用“补丁”版本的Mozilla Rhino的为:

  • 解释JavaScript的
  • 压缩局部变量的名称( “patch”)

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

因此,从您的pom中删除犀牛,并回到解决您的第一个问题!

+0

嗯,我没有明确定义犀牛也有同样的问题 – Jan 2010-12-23 16:22:20

5

从我读到的,你不配置js文件应该从哪里检查或压缩(因为你不使用默认配置)的地方。您只配置聚合。

要更快地响应中使用的issue tracker of yuicompressor-maven-plugin

溶液(来自project FAQ复制/粘贴)

因为:

  • 你在src设置<nosuffix>true</nosuffix>

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

解决方法:

  • 脚本目录移出从web应用

    <plugin> 
        <groupId>@[email protected]</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> 
    
2

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

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

如果您确实需要nosuffix,那么您需要将执行阶段更改为“包装”。

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

任何阶段之前准备资源不起作用,因为战争是建立时,它从原来的源位置拿起JS,从而覆盖缩小的JS(创建过程中准备资源相)在目标目录。

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

至少这是我的理解。

+1

在这种情况下,你已经在webapp dir中缩小了,但没有在生成的.war文件中(检查内容,war插件之前运行过(copy + archive in a你必须从副本中排除文件:请参阅https://github.com/davidB/yuicompressor-maven-plugin/wiki/FAQ – 2013-03-15 15:41:10

+1

你是对的我最终通过添加一个在的YUICompressor插件,然后加入相同的目录中的行家-战争插件的资源。 '<配置> \t \t \t \t $ {project.build.directory} /分钟' – Inxsible 2013-03-15 21:30:46