Java 如何让 maven 使用 yuicompressor-maven-plugin 用缩小的文件构建战争
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19823752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get maven to build a war with minified files using yuicompressor-maven-plugin
提问by Zipper
So I'm trying something I thought would be rather simple, I basically want maven to minify all my js and css files for me before building a war. My plugins look like this:
所以我正在尝试一些我认为相当简单的东西,我基本上希望 maven 在构建战争之前为我缩小我所有的 js 和 css 文件。我的插件看起来像这样:
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<version>1.0.0</version>
<configuration>
<manifestLocation>META-INF</manifestLocation>
<instructions>
<Export-Package>!test.impl,test*</Export-Package>
<Import-Package>*</Import-Package>
<!--
add ,plugin.xml if it's present i.e.
src/main/resources,plugin.xml
-->
<Include-Resource>src/main/resources</Include-Resource>
</instructions>
</configuration>
</plugin>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>yuicompressor-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<goals>
<goal>compress</goal>
</goals>
</execution>
</executions>
<configuration>
<nosuffix>true</nosuffix>
</configuration>
</plugin>
The problem is that the YUI plugin does correctly minify the files, but just before the war is built it looks like it copies over all the files from my main source directory and thus wipes out the changes the YUI plugin had done.
问题是 YUI 插件确实正确地缩小了文件,但就在构建战争之前,它看起来像是复制了我的主源目录中的所有文件,从而消除了 YUI 插件所做的更改。
I'm calling maven by the following: mvn compile war:war
. I've been playing around for awhile with different settings, but so far I haven't found a way around this.
我通过以下调用行家:mvn compile war:war
。我已经使用不同的设置玩了一段时间,但到目前为止我还没有找到解决这个问题的方法。
What I would like is for just after the war has copied over the files it needed from the src directory it would run the YUI plugin, but I tried all the permutations of phases on the YUI plugin, but that didn't seem to make any difference.
我想要的是在战争从 src 目录复制它需要的文件后,它会运行 YUI 插件,但我尝试了 YUI 插件上的所有阶段排列,但这似乎没有任何效果区别。
I've googled around, but pretty much everything I've read so far seems to indiciate that I should just need to drop the YUI plugin in there like I have and everything should magically work. So far I haven't seem to have found the magic.
我已经用谷歌搜索过,但到目前为止我读到的几乎所有内容似乎都表明我应该只需要像我一样将 YUI 插件放在那里,一切都应该神奇地工作。到目前为止,我似乎还没有找到魔法。
采纳答案by Angular University
What happens is that the config above is running the compressor on the process-resources phase, but then the package phase overwrites these files with the original ones.
发生的情况是,上面的配置在 process-resources 阶段运行压缩器,但随后 package 阶段用原始文件覆盖了这些文件。
By changing the phase to package, it should work:
通过将阶段更改为包,它应该可以工作:
<execution>
<phase>package</phase>
<goals>
<goal>compress</goal>
</goals>
Now the compression is done after the files where copied to target in order to build the WAR content.
现在压缩是在将文件复制到目标以构建 WAR 内容之后完成的。
The reason why this is happening is that only compressing files without concatenating them or renaming them with a suffix is not the most common use case for the plugin.
发生这种情况的原因是,仅压缩文件而不连接它们或使用后缀重命名它们并不是该插件最常见的用例。
Normally we want to compress and concatenate files into only one file, and give it a new name.
通常我们只想将文件压缩和连接成一个文件,并给它一个新名称。
The new name is usually something like originalname-min.css / original.name-min.js where .min is the suffix, so removing the nosuffix option on the config above would also work.
新名称通常类似于 originalname-min.css / original.name-min.js,其中 .min 是后缀,因此删除上面配置中的 nosuffix 选项也可以。
Edit: Log example
编辑:日志示例
[INFO] --- yuicompressor-maven-plugin:1.1:compress (default) @ yui-compressor-test -
[INFO] prettify.css (817b) -> prettify.css (617b)[75%]
[INFO] total input (1510b) -> output (1134b)[75%]
回答by Samuel Santos
You should take a look to Minify Maven Pluginwhich sounds like the thing you need.
Let me know if you need any help configuring it.
你应该看看Minify Maven Plugin这听起来像你需要的东西。
如果您需要任何配置帮助,请告诉我。
回答by yuliskov
as stated above maven-war-plugin overwrites files created by minify plugin. it seems that this cannot be changed. however wanted behavior may be achieved by simply changing project structure.
如上所述 maven-war-plugin 覆盖由 minify 插件创建的文件。这似乎无法改变。然而,可以通过简单地改变项目结构来实现想要的行为。
here example what I did with my project. I'm using minify-maven-pluginand springframework, static files are stored in staticdirectory.
这里以我对我的项目所做的为例。我正在使用minify-maven-plugin和spring框架,静态文件存储在静态目录中。
1) move staticdirectory from src/main/webappto src/main/resources
1) 将静态目录从src/main/webapp 移动到src/main/resources
2) change minify plugin config in pom.xml. so source points to resources and target points to classes:
2) 更改 pom.xml 中的 minify 插件配置。所以源指向资源,目标指向类:
<webappSourceDir>src/main/resources</webappSourceDir>
<webappTargetDir>target/classes</webappTargetDir>
<jsSourceDir>static</jsSourceDir>
<cssSourceDir>static</cssSourceDir>
3) change spring config. so spring is serving static files from class path:
3)更改弹簧配置。所以 spring 正在从类路径提供静态文件:
<mvc:resources location="classpath:/static/" mapping="/static/**"/>
and now
现在
mvn clean && mvn package
produces correct war with minified files inside /WEB-INF/classes/static
使用 /WEB-INF/classes/static 中的缩小文件产生正确的战争
回答by koga
Use the war generator and add the configuration to exclude the sources file. Example:
使用 war 生成器并添加配置以排除源文件。例子:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>${war-plugin.version}</version>
<configuration>
<warSourceExcludes>js/**/*.js</warSourceExcludes>
</configuration>
</plugin>
After that you need to include your target files to your war. You do this by setting the "prepare-package" phase of maven lifecycle (I'm using Minify plugin) and adding the files on Minify configuration (jsSourceIncludes,cssSourceIncludes,...)
之后,您需要将目标文件包含到战争中。您可以通过设置 maven 生命周期的“准备包”阶段(我正在使用 Minify 插件)并在 Minify 配置(jsSourceIncludes、cssSourceIncludes...)上添加文件来完成此操作
For example:
例如:
<plugin>
<groupId>com.samaxes.maven</groupId>
<artifactId>minify-maven-plugin</artifactId>
<version>1.7.2</version>
<executions>
<execution>
<id>default-minify</id>
<goals>
<goal>minify</goal>
</goals>
<phase>prepare-package</phase>
<configuration>
<jsSourceDir>/js</jsSourceDir>
<jsSourceIncludes>
<jsSourceInclude>/**/*.js</jsSourceInclude>
</jsSourceIncludes>
</configuration>
</execution>
</executions>
</plugin>
回答by Kristof Neirynck
The accepted answer doesn't work.
接受的答案不起作用。
Much better is this answer (as pointed out by koga in his comment): https://stackoverflow.com/a/11495021/11451
这个答案要好得多(正如 koga 在他的评论中指出的那样):https: //stackoverflow.com/a/11495021/11451
Here is what I ended up doing:
这是我最终做的:
Step 1: minify the css and js
第 1 步:缩小 css 和 js
<plugin>
<groupId>com.samaxes.maven</groupId>
<artifactId>minify-maven-plugin</artifactId>
<version>1.7.2</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>minify</goal>
</goals>
</execution>
</executions>
<configuration>
<charset>UTF-8</charset>
<skipMerge>true</skipMerge>
<cssSourceDir>resources</cssSourceDir>
<jsSourceDir>resources</jsSourceDir>
<jsEngine>CLOSURE</jsEngine>
<nosuffix>true</nosuffix>
<webappTargetDir>${project.build.directory}/minify</webappTargetDir>
<cssSourceIncludes>
<cssSourceInclude>**/*.css</cssSourceInclude>
</cssSourceIncludes>
<cssSourceExcludes>
<cssSourceExclude>**/*.min.css</cssSourceExclude>
</cssSourceExcludes>
<jsSourceIncludes>
<jsSourceInclude>**/*.js</jsSourceInclude>
</jsSourceIncludes>
<jsSourceExcludes>
<jsSourceExclude>**/*.min.js</jsSourceExclude>
</jsSourceExcludes>
</configuration>
</plugin>
Step 2: overwrite css and js in the war with minified files
第 2 步:用缩小的文件覆盖 war 中的 css 和 js
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<webResources>
<resource>
<directory>${project.build.directory}/minify</directory>
</resource>
</webResources>
</configuration>
</plugin>