帮助使用 maven、spring、freemarker 等针对不同环境(测试/开发/生产)设置 YUI 压缩机

发布于 2024-10-07 03:26:24 字数 3876 浏览 5 评论 0原文

我几乎已经完成将我的项目设置为使用正常的 javascript 文件或不同环境的最低版本,但有一件事我无法弄清楚 - 至少不优雅。

我已经将项目的 Maven POM 设置为使用 YUI 压缩器:

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

这没有问题。

我还创建了一个 freemarker 宏来包含各种 css 和 javascript 文件...如果我们想使用“min”版本,它将在其中添加后缀:

<#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="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>

我现在唯一的问题是让 Spring 或 Maven 放置“true/false”如果我在测试环境或生产环境中,“(或者我想它也可以在我的 freemarker 脚本中设置后缀)。你们是如何处理这个问题的?老实说,我不想在部署应用程序时手动进行任何修改。我只想告诉maven,“使用我的生产环境”,它就可以工作了。

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="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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等你爱我 2024-10-14 03:26:25

我不确定它如何与 FreeMarker 一起使用,但使用 Velocity 模板引擎,您可以在模型中配置您希望始终显示的某些属性。因此,您可以将 FreeMarker/Velocity 配置 bean 与自定义属性列表连接起来,其中 suffix 的值可能来自存储在通过 PropertyPlaceholderConfigurer

因此,您可以让生产属性文件具有 suffix = min,而其他文件的属性环境具有 suffix =

至于 Maven 对不同环境使用不同属性,您可以将 Maven 配置为 根据运行时参数过滤构建中的资源,即生产配置文件可以加载一组与默认不同的过滤器值。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文