如何使用maven配置文件设置弹簧活动配置文件 [英] How to set spring active profiles with maven profiles

查看:133
本文介绍了如何使用maven配置文件设置弹簧活动配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个maven作为构建工具的应用程序。

I have an application with maven as a build tool.

我正在使用maven配置文件从不同的配置文件设置不同的属性。

I am using maven profiles to set up different properties from different profiles.

我想做的是maven中的所有活动配置文件都将被移植到spring active配置文件中,所以我可以在bean签名中引用它们( @profile )。但我不知道该怎么做。

What i would like to do is that all active profiles in maven will be ported to spring active profiles as well so i can reference them in bean signature (@profile). but i am not sure how to do it.

例如:考虑以下maven设置

for example: consider the following maven setup

<profiles>
    <profile>
        <id>profile1</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
        </properties>
    </profile>
    <profile>
        <id>profile2</id>
        <properties>
        </properties>
    </profile>
    <profile>
        <id>development</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
        </properties>
    </profile>
    <profile>
        <id>production</id>
        <properties>    
        </properties>
    </profile>
</profiles>

假设我运行maven而没有指定我希望春天有<$ c $的任何其他配置文件c> profile1 和 development 作为活动配置文件。

assuming i run maven with out specifying any other profiles i would like for spring to have profile1 and development as active profiles.

推荐答案

您必须过滤应用程序的资源,例如属性文件,其中包含要在春季激活的配置文件的信息。

You'll have to filter a resources of your application, for instance a property file, that holds the information of which profile to activate in spring.

For实例

spring.profile = ${mySpringProfile}

并为每个配置文件定义此变量的值( mySpringProfile )。

And for each profile, define a value for this variable (mySpringProfile).

在构建过程中,这将根据当前活动配置文件中定义的值进行过滤。

During the build, this will be filtered accordingly to the value defined in the currently active profile.

然后在应用程序的引导期间,您将选择根据此文件的相应配置文件(由于您没有向我们提供更多信息,因此无法帮助您,但这很容易。

Then during the bootstrap of your application you'll select the appropriated profile according to this file (can't help you more as you didn't gave us more information, but this is quite easy.

注意< /强>:我找不到一种方法来获取maven中当前活动的配置文件(类似于包含-P值的project.profiles.active),这就是为什么你必须为每个配置文件设置一个新变量。

Note: I can't find a way to get the currently active profile in maven (something like project.profiles.active that holds your -P values), that's why you'll have to set a new variable for each profile.

注意2 :如果您正在运行Web应用程序,而不是使用此中间文件,请在您的web.xml中过滤此值

Note 2: if you are running a web application, instead of using this intermediate file, filter this value in your web.xml

<context-param>
    <param-name>spring.profiles.active</param-name>
    <param-value>${mySpringProfile}</param-value>
</context-param>

注3 :这实际上是一种不好的做法,你应该设置运行时的配置文件具有系统属性

Note 3: This is actually a bad practice, and you should set the profile at runtime with a system property

这篇关于如何使用maven配置文件设置弹簧活动配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆