Abby
Abby

Reputation: 3199

Maven YUI compress javascript

I'm trying to set up YUI compressor so that each time I build a war to deploy, it minifies my JS and reversions it for cachebusting. I've managed to get it so the minified files are available with the maven version as a suffix, but the original files are also still there, plus all my hrefs need updating every time I build (and locally I still want to be using the originals). Can anyone suggest how to resolve this?

Here's what I have for the plugin:

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

Upvotes: 1

Views: 768

Answers (1)

avijendr
avijendr

Reputation: 4153

Version

1.1 is quite old. We use 1.3.2

locally I still want to be using the originals

You should be using profiles for this. You should have two profiles (or may be more).

  • One for Development/Local
  • One for Production/Release/UAT etc (only this will contain the minimize plugin)

This is something we implemented and is working very well for a long time:Please try this way(note - see the stuff in the war plugin as well, below, it's very important. You might need to change the targetPath for the minimized):

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

Let me know if it works.

Issue of wanting file version numbers and having the hrefs update to point at the latest version

I guess by this you mean by versioning for cache control (Basically to make sure always the new js/css is loaded on release?). This plugin doesnt do that. How will your code knows which version to use?

We had implemented this using a custom Tag lib: After that our Js/Css declaration looks like this:

<custom:ScriptTag src="/js/xyz.js"></custom:ScriptTag>
<custom:CssTag rel="stylesheet" type="text/css" href="/style/someStyle.css"/>

And the compiled/rendered jsp looks like this:

<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 in between is the release-svnversion.

And once this is referenced an url rewrite filter (based on Tuckey or Apache mod_rewrite if you use apache in the front) rewrites this to:

<script type='text/javascript' src='/js/xyz.js' ></script> 

when requested by the browser to the server.

Solution 2 - versioning (With YUI plugin but Without URLRewrite)

  • You can add the version to the js using the YUI plugin.
  • Then store the version number in a property file.
  • Write a custom taglib(like explained above) which renders with that version no as given in the solution above

Advantage of this solution is that you don't need an url rewrite, but still would need a custom taglib.

Upvotes: 4

Related Questions