开发者

Help setting up YUI Compressor with maven, spring, freemarker etc. for different environments (test/development/production)

I am almost done setting my project to use normal javascript files or the min versions for different environments, but there's one thing I can't figure out - at least not elegantly.

I've set up my project's Maven POM to use the YUI compressor:

        <plugin>
            <groupId>net.alchim31.maven</groupId>
            <artifactId>yuicompressor-maven-plugin</artifactId>
            <version>1.1</version>
            <configuration>
                <suffix>.min</suffix>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>compress</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

This works no problem.

I also created a freemarker macro to include various css and javascript files... and if we want to use "min" versions, it will add the suffix into them:

<#macro cssAndJavaScript public useMin>
    <#assign suffix = "" />

    <#if useMin>
        <#assign suffix = ".min" />
    </#if>

    <#if public>
        <link rel="stylesheet" type="text/css" href="${base}/css/public/base${suffix}.css" media="screen, projection"/>
        <link rel="stylesheet" type="text/css" href="${base}/css/public/webpageLayout${suffix}.css" media="screen, projection"/>
        <link rel="stylesheet" type="text/css" href="${base}/css/public/forms${suffix}.css" media="screen, projection"/>
        <link rel="stylesheet" type="text/css" href="${base}/css/public/publicWebpages${suffix}.css" media="screen, projection" />
        <link rel="stylesheet" type="text/css" href="${base}/css/public/login${suffix}.css" media="screen, projection" />
    <#else>
        <link rel="stylesheet" type="text/css" href="${base}/css/ui-lightness/jquery-ui-1.8.6.custom${suffix}.css" media="screen, projection"/>
        <link rel="stylesheet" type="text/css" href="${base}/css/jcrop/jquery.jcrop${suffix}.css" media="screen, projection"/>
        <link rel="stylesheet" type="text/css" href="${base}/css/application/base${suffix}.css" media="screen, projection"/>
        <link rel="stylesheet" type="text/css" href="${base}/css/application/webpageLayout${suffix}.css" media="screen, projection"/>
        <link rel="stylesheet" type="text/css" href="${base}/css/application/forms${suffix}.css" media="screen, projection"/>
        <link rel="stylesheet" type="text/css" href="${base}/css/application/entityListing${suffix}.css" media="screen, projection"/>
        <link rel=开发者_Python百科"stylesheet" type="text/css" href="${base}/css/application/entityView${suffix}.css" media="screen, projection"/>
        <link rel="stylesheet" type="text/css" href="${base}/css/application/questions${suffix}.css" media="screen, projection"/>
        <link rel="stylesheet" type="text/css" href="${base}/css/application/categoryQuestions${suffix}.css" media="screen, projection"/>
        <link rel="stylesheet" type="text/css" href="${base}/css/application/miniScoreCard${suffix}.css" media="screen, projection"/>
    </#if>

    <script src="${base}/js/jquery/jquery-1.4.2${suffix}.js"></script>
    <script src="${base}/js/jquery/jquery-ui-1.8.6.custom${suffix}.js"></script>
    <script src="${base}/js/jquery/jquery.tmpl${suffix}.js"></script>
    <script src="${base}/js/jquery/jquery.jcrop${suffix}.js"></script>
    <script src="${base}/js/json/json2${suffix}.js"></script>
    <script src="${base}/js/jwplayer/jwplayer${suffix}.js"></script>
    <script src="${base}/js/core${suffix}.js"></script>
</#macro>

My only problem now is getting Spring or Maven to put "true/false" (or I guess it could just set the suffix too) in my freemarker script for me if I am in test environment or production environment. How have you guys dealt with this issue? Honestly, I don't want to do any manually fudging around when I deploy my app. I just want to tell maven, "Use my production environment" and it works.


I'm not sure how it works with FreeMarker, but with the Velocity template engine, you can configure certain properties in the model that you would like to always appear. You would therefore wire up the FreeMarker/Velocity config bean with your lists of custom properties, where the value for suffix could come from a value stored in a properties file accessed with PropertyPlaceholderConfigurer.

Thus you could have the production properties file have suffix = min, and the properties environment for other files have suffix =.

As far as using different properties for different environments with Maven, you can configure Maven to filter the resources in your build based on runtime arguments, i.e. a production profile could load a different set of filter values than the default <profile>.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜