我正在尝试设置 YUI 压缩器,以便每次我构建要部署的 war 时,它都会缩小我的 JS 并将其还原以进行缓存清除。我已经设法得到它,所以可以使用 maven 版本作为后缀的缩小文件,但原始文件也仍然存在,而且我每次构建时都需要更新所有 href(并且在本地我仍然想使用原件)。谁能建议如何解决这个问题?
这是我的插件:
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>yuicompressor-maven-plugin</artifactId>
<version>1.1</version>
<configuration>
<excludes>
<exclude>**/farbtastic/*</exclude>
<exclude>**/jquery.js</exclude>
<exclude>**/*.css</exclude>
</excludes>
<suffix>.${version}</suffix>
</configuration>
<executions>
<execution>
<goals>
<goal>compress</goal>
</goals>
</execution>
</executions>
</plugin>
最佳答案
版本
1.1 已经很老了。我们使用 1.3.2
在本地我仍然想使用原件
你应该使用 profiles为了这。您应该有两个配置文件(或者可能更多)。
- 一个用于开发/本地
- 一个用于生产/发布/UAT 等(只有这个将包含最小化插件)
这是我们实现的东西,并且在很长一段时间内都运行良好:请尝试这种方式(注意 - 请参阅下面的 war 插件 中的内容,这非常重要。您可能需要更改最小化的 targetPath):
<profile>
<id>Production</id>
.......
.......
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>yuicompressor-maven-plugin</artifactId>
<version>1.3.2</version>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>compress</goal>
</goals>
</execution>
</executions>
<configuration>
<nosuffix>true</nosuffix>
<encoding>UTF-8</encoding>
<!-- Show warnings decided on value of variable set in each profile -->
<jswarn>${yuiCompression.warn}</jswarn>
<webappDirectory>${project.build.directory}/minimized</webappDirectory>
<excludes>
<exclude>**/farbtastic/*</exclude>
<exclude>**/jquery.js</exclude>
<exclude>**/*.css</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<argLine>-Xmx1024m</argLine>
<dependentWarExcludes>....</dependentWarExcludes>
<warSourceExcludes>....</warSourceExcludes>
<packagingExcludes>....</packagingExcludes>
<packagingExcludes>....</packagingExcludes>
<webResources>
<resource>
<directory>${project.build.directory}/minimized</directory>
<targetPath>/</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
让我知道它是否有效。
需要文件版本号并让 hrefs 更新以指向最新版本的问题
我猜你的意思是缓存控制的版本控制(基本上是为了确保总是在发布时加载新的 js/css?)。这个插件不会那样做。您的代码如何知道要使用哪个版本?
我们使用自定义标签库实现了这个:之后我们的 Js/Css 声明如下所示:
<custom:ScriptTag src="/js/xyz.js"></custom:ScriptTag>
<custom:CssTag rel="stylesheet" type="text/css" href="/style/someStyle.css"/>
编译/呈现的 jsp 如下所示:
<script type='text/javascript' src='/js/4112-13178/xyz.js' ></script>
<link type='text/css' href='/style/4112-13178/someStyle.css' rel='stylesheet' />
4112-13178 之间是release-svnversion。
一旦它被引用,一个 url 重写过滤器(基于 Tuckey 或 Apache mod_rewrite,如果你在前面使用 apache)将它重写为:
<script type='text/javascript' src='/js/xyz.js' ></script>
当浏览器向服务器请求时。
解决方案 2 - 版本控制(使用 YUI 插件但不使用 URLRewrite)
- 您可以使用 YUI 插件将版本添加到 js。
- 然后将版本号存储在属性文件中。
- 编写一个自定义标签库(如上文所述),使用上述解决方案中给出的版本号呈现
此解决方案的优点是您不需要重写 url,但仍需要自定义标签库。
关于javascript - Maven YUI 压缩 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21056400/