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

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

问题描述

我有一个使用 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 活动配置文件中,以便我可以在 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,我希望 spring 将 profile1development 作为活动配置文件.

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

推荐答案

您必须过滤应用程序的资源,例如一个属性文件,其中包含在 spring 中激活哪个配置文件的信息.

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.

例如

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 配置文件设置 spring 活动配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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