简体   繁体   English

如何使Maven yuicompressor和tomcat插件发挥出色

[英]How to get maven yuicompressor and tomcat plugins to play nice

I'd like to compress all of my javascript files and aggregate them using YUICompressor, and I saw that there was a maven plugin to allow me to do this. 我想压缩所有的JavaScript文件并使用YUICompressor对其进行汇总,我发现有一个maven插件可以让我执行此操作。 I got it working for the most part. 我大部分时间都在工作。

I am also using the Mojo tomcat plugin as well. 我也正在使用Mojo tomcat插件。 When i go to run the tomcat:run goal, tomcat does not read from the target's output directory (this is where the YUI compressor put my javascript files) - but rather, it reads from the actual source files in my "src/main/ webapp/scripts" directory. 当我运行tomcat:run目标时,tomcat不会从目标的输出目录中读取(这是YUI压缩器放置我的javascript文件的位置)-而是从“ src / main / webapp / scripts”目录。 Of course, the aggregated javascript file (all.js) is not there. 当然,聚合的javascript文件(all.js)不存在。

I have a few questions. 我有几个问题。

  1. How can I get the tomcat plugin to read the target's output folder that the yui compressor plugin created? 如何获取tomcat插件以读取yui压缩器插件创建的目标的输出文件夹?

  2. Do I have to run the yui compressor maven goal every time I want to update my javascript files during development? 我每次在开发过程中要更新javascript文件时都必须运行yui压缩器maven目标吗?

  3. Is there a better way to achieve this? 有没有更好的方法来实现这一目标? Essentially, my end goal is to be able to develop JavaScript and test my source files in development mode, but I want to compress and aggregate the files and use the all.js script when the application is running in production mode. 本质上,我的最终目标是能够在开发模式下开发JavaScript并测试我的源文件,但是当应用程序在生产模式下运行时,我想压缩和聚合文件并使用all.js脚本。

While the Rails people have certainly figured this out, this seems to be a non-trivial thing to do with Maven and Spring. 尽管Rails员工确实已经弄清楚了这一点,但对于Maven和Spring来说,这似乎并不是一件容易的事。

I would appreciate any and all assistance on how I can get this running correctly. 我将非常感谢您提供有关如何正常运行的任何帮助。 Thanks! 谢谢!

I was just investigating this very problem and found my answer by looking at the plugin documentation . 我只是在研究这个问题,并通过查看插件文档找到了答案。

mvn tomcat:run - Runs the current project as a dynamic web application using an embedded Tomcat server. mvn tomcat: run-使用嵌入式Tomcat服务器将当前项目作为动态Web应用程序运行。

What this means in practice is that the package execution phase has not been reached when the embedded tomcat runs. 实际上,这意味着在运行嵌入式tomcat时尚未达到包执行阶段。

The answer is to instead use: 答案是改为使用:

mvn tomcat:run-war - Runs the current project as a packaged web application using an embedded Tomcat server. mvn tomcat:run- war-使用嵌入式Tomcat服务器将当前项目作为打包的Web应用程序运行。

This allows the maven build to get as far as packaging the WAR file and therefore allows the yuicompressor-maven-plugin to do what it needs to before the embedded tomcat starts up. 这使得Maven构建可以将WAR文件打包,因此yuicompressor-maven-plugin可以在嵌入式tomcat启动之前完成所需的工作。

As for having to run it every time, you should attach the run of the yui plugin to the "generate-sources" execution phase. 至于每次都必须运行它,您应该将yui插件的运行附加到“ generate-sources”执行阶段。

Add the following to your plugin (the important part is the "phase" element to attach it to the lifecycle): 将以下内容添加到您的插件中(重要的部分是将其附加到生命周期的“阶段”元素):

  <plugin>
    <groupId>net.alchim31.maven</groupId>
    <artifactId>yuicompressor-maven-plugin</artifactId>
    <executions>
      <execution>
        <phase>generate-sources</phase>
        <goals>
          <goal>compress</goal>
        </goals>
      </execution>
    </executions>  
    <configuration>...</configuration>      
  </plugin>

This way the plugin will run every build during the generate-sources phase. 这样,插件将在generate-sources阶段运行所有构建。 So any time you change your java scripts which you have configured the plugin for, the output .js file will be updated as soon as you run something like: 因此,每当您更改为插件配置的Java脚本时,只要运行以下命令,输出的.js文件就会立即更新:

mvn compile
mvn test
mvn install
mvn package

and so forth. 等等。


The above does cause the minified (and possibly aggregated) files to be created earlier in the lifecycle but tomcat:run cannot find them! 上面的方法确实导致在生命周期的早期创建了压缩文件(可能是聚合文件),但是tomcat:run找不到它们!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用yuicompressor-maven-plugin让maven与缩小的文件建立战争 - How to get maven to build a war with minified files using yuicompressor-maven-plugin 如何让ArrayList <Integer>和Scanner玩得很好? - How to get ArrayList<Integer> and Scanner to play nice? 我如何让Groovy和JAXB一起玩得很好 - How do I get Groovy and JAXB to play nice together 如何使Spring JPA,Hibernate和OSGi发挥出色? - How to Get Spring JPA, Hibernate, and OSGi to Play Nice? 如何让GWT 2.0和Restlet 2.0发挥出色 - How to get GWT 2.0 and Restlet 2.0 to play nice 如何让 PowerShell 与不同文件中的类一起玩? - How to get PowerShell to play nice with classes in different files? 目前是否可以通过Maven构建Eclipse插件并具有良好的IDE集成? - Is it currently possible to build Eclipse Plugins by Maven AND have nice IDE Integration? 是否可以使使用 Avro Maven 插件生成的 POJO 与 Jackson 兼容? - Is it possible to make POJOs generated with the Avro Maven plugin play nice with Jackson? 如何改善在Tomcat上运行的Java Processies - How to nice Java processies running on Tomcat 如何让Maven使用某些插件的最新版本? - How to get Maven to use the latest versions of certain plugins?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM