gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 08:10:35 29 4
gpt4 key购买 nike

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

  1. 我的配置正确地压缩和聚合了 JavaScript 文件,但是如果我运行 mvn package 来制作一个 war 文件,maven 会在复制 src 之前压缩 JavaScript 文件/main/webapp 文件夹。这将覆盖所有压缩的 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>

还是我只是以错误的方式实现了我的目标?

这是打包war时的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 是否是您认为的工作的错误工具?还是 maven/java 不适合繁重的 javascript 开发?为什么这么难?

最佳答案

我遇到了类似的问题,我将阶段更改为 package.希望这也会对其他人有所帮助。

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

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

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

prepare-resources 之前的任何阶段都不起作用,因为构建 war 时,它会从原始源位置获取 js,从而覆盖目标目录中的缩小 js(在 prepare-resources 阶段创建)。

当您删除 nosuffix 时,缩小会起作用,因为在打包阶段,文件名不同,因此不会覆盖文件,您将在目标目录中看到缩小和未缩小的 js 文件。

关于java - YUI Compressor Maven 插件在错误的时间执行,而 tomcat 插件未使用 yui-compressor 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8351842/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com