YUI Compressor Maven插件在错误的时间执行,tomcat插件没有使用YUI - Compressor输

YUI Compressor Maven plugin executed at wrong time and tomcat plugin not using yui-compressor output

本文关键字:插件 Compressor YUI tomcat 执行 Maven 错误 时间      更新时间:2023-09-26

我正在尝试使用YUI Compressor plugin for 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文件夹中。

        <!-- 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在打包war时的输出。您可以看到,即使我在预打包阶段告诉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是一个错误的工具,你认为的工作?或者maven/java只是不符合繁重的javascript开发?为什么这么难?

我遇到了一个类似的问题,我把我的阶段改为package。希望这对其他人也有帮助。

这很棘手。这与nosuffix配置选项有关。如果您删除了nosuffix选项,则会按预期进行缩小。

如果你真的需要没有后缀,那么你需要把执行阶段改为"package"。

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

准备资源之前的任何阶段都不起作用,因为当war被构建时,它会从原始源位置获取js,从而覆盖目标目录中的最小化js(在准备资源阶段创建)。

当你删除nosuffix时,因为在打包阶段,文件名是不同的,所以不会覆盖文件,你会在目标目录中看到最小化和非最小化的js文件。

要解决这个问题,您可以这样做。

(1)把你的个人代码放在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插件默认情况下不会复制你的javascript(听起来很奇怪,对吧?)

默认情况下,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] ------------------------------------------------------------------------

你可以调优你的war文件,以了解你的未被压缩的yui压缩JS文件的特殊文件夹:

   <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。

应该可以。