我想为maven使用YUI Compressor插件来压缩我的CSS和JavaScript,但我遇到了两个问题。YUI Compressor Maven插件在错误的时间执行和tomcat插件不使用yui压缩器输出
我的配置适当压缩和聚合JavaScript文件,但如果我跑
mvn package
做出war文件,Maven将压缩JavaScript文件之前,它的副本在src/main/webapp
文件夹。这将覆盖所有压缩的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>
或者,我只是要我的目标走错了路?
这里是打包战争时的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
之前得到这个压缩文件。
是行家只为你认为错误的工作工具?或者是maven/java只是没有达到与重JavaScript开发鼻烟?为什么这么难?
尝试将yui压缩机置于其他阶段(例如准备包装) – khmarbaise
@khmarbaise我试过这个。问题在于这个阶段仍然发生在包装阶段之前。复制发生在软件包阶段内部。我很茫然。我即将完全放弃这种方法。我认为这种压缩/聚合可能必须在服务器上传后,我上传的战争,其中......说实话...糟透了:(我也不会真的有一个方便的方式来测试我的生产JavaScript通过tomcat在我的开发机器之前,我只是运行tomcat:运行,就是这样,我真的不想让我的开发环境复杂化 – egervari
@khmarbaise看来maven并不是这个意思,也不是春天。大多数现代Web框架现在都有这个特性,但是Java真的处于石器时代,这太糟糕了,这是一个大型的现有项目,我必须得到一些工作,我想我可以将生产JavaScript输出回我的'src/main/webapp/script'目录下,这是我唯一能想到的东西,在我的源代码目录下的每个文件的普通版本和.min版本中生活会很糟糕吗?这看起来不太干净:/ – egervari