YUI Compressor Maven插件在错误的时间执行,而tomcat插件不使用yui-compressor输出

我正在尝试使用YUI Compressor插件为maven压缩我的CSS和JavaScript,但我遇到了两个问题。

  1. 我的配置正确压缩和聚合JavaScript文件,但是如果我运行mvn package来生成war文件,maven会在它复制到src/main/webapp文件夹之前压缩JavaScript文件。 这会覆盖所有压缩的css和JavaScript文件。 我怎样才能解决这个问题?

  2. 如何让tomcat maven插件使用压缩的JavaScript文件而不是我的src/main/webapp/scripts文件夹中的文件? 当我的应用程序试图读取all.js ,它失败了,因为它位于target/目录而不是我的src/main/webapps文件夹中。

        org.codehaus.mojo tomcat-maven-plugin 1.1  target/myapp-1.0.war     net.alchim31.maven yuicompressor-maven-plugin    compress      **/jwplayer.js **/audio-player-noswfobject.js **/audio-player-uncompressed.js **/audio-player.js **/jscharts.js **/jquery-ui-1.8.16.custom.min.js  true false false   false true ${project.build.directory}/${project.build.finalName}/scripts/all.js  **/json/json2.js **/jwplayer/jwplayer.js **/font/font.js **/underscore/underscore.js **/jquery/jquery-1.7.1.js **/jquery/jquery-ui-1.8.16.custom.min.js **/jquery/jquery.cookie.js **/jquery/jquery.fancybox.js **/jquery/jquery.highlight.js **/jquery/jquery.jcrop.js **/jquery/jquery.tmpl.js **/jquery/farbtastic.js **/jscharts/jscharts.js **/myapp/homepage.js      

或者我只是以错误的方式实现我的目标?

这是包装战争时的maven输出。 你可以看到,即使我告诉yui:compress目标在预包装阶段,它仍然发生在复制资源之前,因为这发生在package阶段:

 [INFO] ------------------------------------------------------------------------ [INFO] Building My App 1.0 [INFO] ------------------------------------------------------------------------ Downloading: http://repo1.maven.org/maven2/net/alchim31/maven/yuicompressor-maven-plugin/maven-metadata.xml Downloading: http://snapshots.repository.codehaus.org/net/alchim31/maven/yuicompressor-maven-plugin/maven-metadata.xml Downloading: http://download.java.net/maven/2/net/alchim31/maven/yuicompressor-maven-plugin/maven-metadata.xml Downloading: http://oss.sonatype.org/content/groups/public/net/alchim31/maven/yuicompressor-maven-plugin/maven-metadata.xml Downloaded: http://oss.sonatype.org/content/groups/public/net/alchim31/maven/yuicompressor-maven-plugin/maven-metadata.xml (442 B at 2.1 KB/sec) Downloaded: http://repo1.maven.org/maven2/net/alchim31/maven/yuicompressor-maven-plugin/maven-metadata.xml (403 B at 0.9 KB/sec) [INFO] [INFO] --- maven-resources-plugin:2.4.3:resources (default-resources) @ myapp --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 26 resources [INFO] Copying 4 resources [INFO] [INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ myapp --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-compiler-plugin:2.3.2:compile (default) @ myapp --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- aspectj-maven-plugin:1.3:compile (default) @ myapp --- [INFO] [INFO] --- maven-resources-plugin:2.4.3:testResources (default-testResources) @ myapp --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 11 resources [INFO] [INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ myapp --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- aspectj-maven-plugin:1.3:test-compile (default) @ myapp --- [INFO] No modifications found skipping aspectJ compile [INFO] [INFO] --- maven-surefire-plugin:2.6:test (default-test) @ myapp --- [INFO] Tests are skipped. [INFO] [INFO] --- yuicompressor-maven-plugin:1.2:compress (default) @ myapp --- [INFO] 960.css (9989b) -> 960.css (5897b)[59%] [INFO] base.css (24210b) -> base.css (16437b)[67%] [INFO] reset.css (2062b) -> reset.css (1096b)[53%] [INFO] text.css (1270b) -> text.css (598b)[47%] [INFO] tinymce.css (1994b) -> tinymce.css (1277b)[64%] [INFO] jquery.fancybox-1.3.4.css (8852b) -> jquery.fancybox-1.3.4.css (6975b)[78%] [INFO] farbtastic.css (1362b) -> farbtastic.css (478b)[35%] [INFO] jquery.jcrop.css (748b) -> jquery.jcrop.css (582b)[77%] [INFO] base.css (34567b) -> base.css (25034b)[72%] [INFO] jquery-ui-1.8.11.custom.css (33994b) -> jquery-ui-1.8.11.custom.css (25351b)[74%] .... (tons of javascript files) [INFO] why.js (515b) -> why.js (354b)[68%] [INFO] underscore.js (26960b) -> underscore.js (9472b)[35%] [INFO] total input (1832512b) -> output (1198425b)[65%] [INFO] generate aggregation : C:\Users\egervari\IdeaProjects\myapp-development\target\myapp-1.0\scripts\all.js [INFO] all.js (564342b) [INFO] nb warnings: 0, nb errors: 0 [INFO] [INFO] --- maven-war-plugin:2.1.1:war (default-war) @ myapp --- [INFO] Packaging webapp [INFO] Assembling webapp [myapp] in [C:\Users\egervari\IdeaProjects\myapp-development\target\myapp-1.0] [INFO] Processing war project [INFO] Copying webapp resources [C:\Users\egervari\IdeaProjects\myapp-development\src\main\webapp] 

即使更改阶段确实有效,那么我也必须在tomcat:run之前tomcat:run此压缩tomcat:run

maven对你认为的工作来说是错误的工具吗? 或者是maven / java只是没有重视javascript开发的鼻烟? 为什么这么难?

我遇到了类似的问题,我将阶段改为打包。 希望这对其他人也有帮助。

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

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

    package  ...... ..... 

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

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

要解决这个问题,你可以尝试一下。

(1)将您的个人编码的javascript放在src / main / js中,而不是src / main / webapp中。 使用mvn约定进行JS放置。 *注意:对于你包含的库,例如jQuery或其他任何东西,你可以将它们转储到src / main / webapp中,这很好。 这些已经最小化了你永远不会修改的javascript。

(2)调整你的YUI插件以(a)忽略你没有编码的javascript,(b)将压缩的javascript编译到输出文件夹。

   net.alchim31.maven yuicompressor-maven-plugin 1.5.1    compress     true  **/ui-framework/**  ${yui.outputdir}   

因此,我们可以进行以下练习。

 mvn process-resources └── target ├── classes │  ├── default.logback.xml │  └── props │  ├── default.props │  └── production.default.props ├── packageLinkDefs.properties └── yui-compressed └── jasmineDummyExample_element_mover.js 

这里的要点是YUI正在将压缩的javascript编译为自己的输出文件夹,而war插件并不知道它。

(3)最后你只需要调整你的war插件来复制yui编译的源代码。

只要你遵循maven的javascript约定,你的javascript应该在src / main / js /下编码,你的war插件默认不会复制你的javascipt(听起来很奇怪吧?)。

默认情况下,您的war插件将复制src / main / webapp下的任何静态资源。 这很方便,因为它允许您指示war插件保持将src / main / webapp复制到最终war文件的默认行为,此外,还要复制/ src / target / yui-compressed文件夹。 这样,两个文件夹中都没有重叠文件,输出目录中没有文件覆盖。

这在以下代码段中说明:

 $ mvn war:war [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building lift-someproject Project 1.0 [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-war-plugin:2.5:war (default-cli) @ lift-someproject --- [INFO] Packaging webapp [INFO] Assembling webapp [lift-someproject] in [D:\scalaWorkspace\liftsomeprojectTwo\lift-someproject\target\lift-someproject] [INFO] Processing war project [INFO] Copying webapp webResources [D:\scalaWorkspace\liftsomeprojectTwo\lift-someproject\target\yui-compressed] to [D:\scalaWorkspace\liftsomeprojectTwo\lift-someproject\target\lift-someproject] [INFO] Copying webapp resources [D:\scalaWorkspace\liftsomeprojectTwo\lift-someproject\src\main\webapp] [INFO] Webapp assembled in [820 msecs] [INFO] Building war: D:\scalaWorkspace\liftsomeprojectTwo\lift-someproject\target\lift-someproject.war [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 12.968s [INFO] Finished at: Tue Dec 23 20:17:59 CET 2014 [INFO] Final Memory: 11M/246M [INFO] ------------------------------------------------------------------------ 

您可以使用未压缩的yui压缩JS文件调整war文件以了解特殊文件夹:

   org.apache.maven.plugins maven-war-plugin 2.5    ${yui.outputdir} js     

如果您使用Jetty进行快速开发,您将需要对该插件进行进一步的修改,以确保它在src / main / js中找到您的手动编码的javascript。

这应该工作。