![](/img/trans.png)
[英]How to compress JS and css in java at build time using maven-minify-plugin with YUI compressor
[英]YUI Compressor Maven plugin executed at wrong time and tomcat plugin not using yui-compressor output
我正在嘗試使用MUI的YUI Compressor插件來壓縮我的CSS和JavaScript,但是遇到兩個問題。
我的配置可以正確壓縮和聚合JavaScript文件,但是如果我運行mvn package
制作war文件,則maven將在復制到src/main/webapp
文件夾之前壓縮JavaScript文件。 這將覆蓋所有壓縮的CSS和JavaScript文件。 我怎樣才能解決這個問題?
如何獲取tomcat maven插件以使用壓縮的JavaScript文件而不是src/main/webapp/scripts
文件夾中的文件? 當我的應用程序嘗試讀取all.js
,它失敗了,因為它位於target/
目錄中,而不是在src/main/webapps
文件夾中。
<!-- Tomcat --> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>tomcat-maven-plugin</artifactId> <version>1.1</version> <configuration> <warFile>target/myapp-1.0.war</warFile> </configuration> </plugin> <!-- YUI Compressor --> <plugin> <groupId>net.alchim31.maven</groupId> <artifactId>yuicompressor-maven-plugin</artifactId> <executions> <execution> <goals> <goal>compress</goal> </goals> </execution> </executions> <configuration> <excludes> <exclude>**/jwplayer.js</exclude> <exclude>**/audio-player-noswfobject.js</exclude> <exclude>**/audio-player-uncompressed.js</exclude> <exclude>**/audio-player.js</exclude> <exclude>**/jscharts.js</exclude> <exclude>**/jquery-ui-1.8.16.custom.min.js</exclude> </excludes> <nosuffix>true</nosuffix> <jswarn>false</jswarn> <force>false</force> <aggregations> <aggregation> <removeIncluded>false</removeIncluded> <insertNewLine>true</insertNewLine> <output>${project.build.directory}/${project.build.finalName}/scripts/all.js</output> <includes> <include>**/json/json2.js</include> <include>**/jwplayer/jwplayer.js</include> <include>**/font/font.js</include> <include>**/underscore/underscore.js</include> <include>**/jquery/jquery-1.7.1.js</include> <include>**/jquery/jquery-ui-1.8.16.custom.min.js</include> <include>**/jquery/jquery.cookie.js</include> <include>**/jquery/jquery.fancybox.js</include> <include>**/jquery/jquery.highlight.js</include> <include>**/jquery/jquery.jcrop.js</include> <include>**/jquery/jquery.tmpl.js</include> <include>**/jquery/farbtastic.js</include> <include>**/jscharts/jscharts.js</include> <include>**/myapp/homepage.js</include> </includes> </aggregation> </aggregations> </configuration> </plugin>
還是我只是以錯誤的方式實現目標?
這是打包戰爭時的行家輸出。 您可以看到,即使我在打包前階段告訴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
此壓縮。
Maven只是您認為工作的錯誤工具嗎? 還是不是maven / java只是不進行大量JavaScript開發? 為什么這么難?
我遇到了類似的問題,因此將階段更改為打包。 希望這也會對其他人有所幫助。
這很棘手。 這與nosuffix config選項有關。 如果刪除nosuffix選項,則縮小將按預期工作。
如果確實需要后綴,則需要將執行階段更改為“ package”。
<executions>
<execution>
<phase>package</phase>
<goals>
......
.....
prepare-resources之前的任何階段都不起作用,因為在戰爭爆發后,它將從原始源位置獲取js,從而覆蓋了目標目錄中縮小的js(在prepare-resources階段創建的js)。
刪除nosuffix時,縮小功能起作用,因為在打包階段,文件名是不同的,因此不會覆蓋文件,並且您會在目標目錄中看到縮小和未縮小的js文件。
要解決此問題,您可以喜歡。
(1)將您的個人編碼javascript放在src / main / js中,而不是src / main / webapp中。 將mvn約定用於JS放置。 *注意:對於您所包含的庫,例如jQuery或其他任何庫,您只需將它們轉儲到src / main / webapp中,就可以了。 這些已經最小化了您永遠不會修改的javascript。
(2)將您的YUI插件調整為(a)忽略您未編寫代碼的javascript,(b)將壓縮的javascript編譯到輸出文件夾。
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>yuicompressor-maven-plugin</artifactId>
<version>1.5.1</version>
<executions>
<execution>
<goals>
<goal>compress</goal>
</goals>
</execution>
</executions>
<configuration>
<nosuffix>true</nosuffix>
<excludes>
<exclude>**/ui-framework/**</exclude>
</excludes>
<outputDirectory>${yui.outputdir}</outputDirectory>
</configuration>
</plugin>
因此,我們可以進行以下練習。
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壓縮文件夾。 這樣,兩個文件夾中都沒有重疊的文件,輸出目錄中沒有文件覆蓋。
以下代碼段對此進行了說明:
$ 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文件,以了解特殊文件夾:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.5</version>
<configuration>
<webResources>
<resource>
<directory>${yui.outputdir}</directory>
<targetPath>js</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
如果您使用Jetty進行快速開發,則需要對該插件做一些進一步的研究,以確保該插件在src / main / js中找到您的手工編碼javascript。
那應該工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.