如何使用maven配置文件设置弹簧活动配置文件

Gle*_*eeb 56 java spring maven spring-profiles

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

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

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

例如:考虑以下maven设置

<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>
Run Code Online (Sandbox Code Playgroud)

假设我运行maven而没有指定我希望Spring拥有的任何其他配置文件profile1以及 development作为活动配置文件.

Eug*_*ene 34

有一种更优雅的方式可以同时在2个maven + spring配置文件之间切换.

首先,向POM添加配置文件(注意 - maven + spring配置文件由单个系统变量激活):

<profiles>
    <profile>
        <id>postgres</id>
        <activation>
            <activeByDefault>true</activeByDefault>
            <property>
                <name>spring.profiles.active</name>
                <value>postgres</value>
            </property>
        </activation>
        <dependencies>
            <dependency>
                <groupId>postgresql</groupId>
                <artifactId>postgresql</artifactId>
                <version>9.1-901.jdbc4</version>
            </dependency>
        </dependencies>
    </profile>
    <profile>
        <id>h2</id>
        <activation>
            <property>
                <name>spring.profiles.active</name>
                <value>h2</value>
            </property>
        </activation>           
        <dependencies>
            <dependency>
                <groupId>com.h2database</groupId>
                <artifactId>h2</artifactId>
                <version>1.4.191</version>
            </dependency>
        </dependencies>
    </profile>
</profiles>
Run Code Online (Sandbox Code Playgroud)

其次,为spring设置默认配置文件(对于已在POM中设置的maven).对于Web应用程序,我将以下行插入web.xml:

<context-param>
   <param-name>spring.profiles.default</param-name>
   <param-value>postgres</param-value>
</context-param>
Run Code Online (Sandbox Code Playgroud)

第三,将配置文件相关的bean添加到您的配置中.在我的情况下(XML配置),它是:

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="mainDataSource" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
    </property>
    <property name="jpaProperties" ref="hibProps"/>
    <property name="packagesToScan">
        <list>
            <value>my.test.model</value>
        </list>
    </property>
</bean>
...
<beans profile="postgres">
    <bean name="mainDataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.postgresql.Driver" />
        <property name="url" value="jdbc:postgresql://127.0.0.1:5432/webchat" />
        <property name="username" value="postgres" />
        <property name="password" value="postgres" />
    </bean>
</beans>

<beans profile="h2">
    <bean name="mainDataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.h2.Driver" />
        <property name="url" value="jdbc:h2:file:./newsdb;INIT=RUNSCRIPT FROM 'classpath:init.sql';TRACE_LEVEL_FILE=0" />
        <property name="username" value="sa" />
        <property name="password" value="" />
    </bean>
</beans>
Run Code Online (Sandbox Code Playgroud)

现在有可能:

  • 使用mvn jetty:runmvn jetty:run -Dspring.profiles.active=postgres命令在Postgres DB上运行我的web-app
  • 在H2 DB上运行我的网络应用程序 mvn clean jetty:run -Dspring.profiles.active=h2


Ser*_*hez 23

您需要的第一件事是两个属性文件,用于保持您的配置.文件名应与模式application- {custom_suffix} .properties匹配.在Maven项目的src/main/resources目录中创建它们,在主application.properties文件旁边,稍后您将使用它来激活其中一个并保存两个配置文件共享的值.

然后是时候修改你的pom.xml了.您需要在每个Maven配置文件中定义自定义属性,并将其值设置为与要使用特定配置文件加载的相应属性文件的后缀相匹配.以下示例还标记了默认情况下要运行的第一个配置文件,但它不是必需的.

<profile>
    <id>dev</id>
    <properties>
        <activatedProperties>dev</activatedProperties>
    </properties>
    <activation>
        <activeByDefault>true</activeByDefault>
    </activation>
</profile>
<profile>
    <id>release</id>
    <properties>
        <activatedProperties>release</activatedProperties>
    </properties>
</profile>
Run Code Online (Sandbox Code Playgroud)

接下来,在同一文件的build部分中,为Resources Plugin配置过滤.这将允许您将上一步中定义的属性插入资源目录中的任何文件,这是后续步骤.

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
    …
</build>
Run Code Online (Sandbox Code Playgroud)

最后,将以下行添加到application.properties中.

spring.profiles.active=@activatedProperties@
Run Code Online (Sandbox Code Playgroud)

运行构建时,Resources Plugin将使用活动Maven概要文件中定义的属性值替换占位符.启动应用程序后,Spring框架将根据活动Spring配置文件的名称加载相应的配置文件,该配置文件由spring.profiles.active属性的值描述.请注意,Spring Boot 1.3替换了筛选值的默认Resources Plugin语法,@activatedProperties@而是使用而不是${activatedProperties}表示法.

它努力完美.希望这可以帮到你.

  • 我只想补充一点,您也可以在生成war时选择配置文件:mvn软件包-P release(释放POM中定义的maven配置文件的ID) (2认同)

pou*_*sma 21

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

例如

spring.profile = ${mySpringProfile}
Run Code Online (Sandbox Code Playgroud)

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

在构建期间,将相应地过滤到当前活动的配置文件中定义的值.

然后在您的应用程序的引导程序中,您将根据此文件选择适当的配置文件(由于您没有向我们提供更多信息,因此无法帮助您,但这非常简单.

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

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

<context-param>
    <param-name>spring.profiles.active</param-name>
    <param-value>${mySpringProfile}</param-value>
</context-param>
Run Code Online (Sandbox Code Playgroud)

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

  • @Gleeb嗯,总的来说,这更好(因为更灵活)提供一个独特的版本,根据运行时配置的不同行为,而不是生成3个不同的二进制文件.但这仍然取决于您的业务.由你决定:p (4认同)
  • 为什么这是不好的做法?我想要做的是根据maven配置文件启用某些bean,以配合不同环境的配置.我没有使用Web应用程序.这是一个简单的可执行jar.使用资源解决方案,我需要首先提升我的上下文,然后设置活动配置文件,然后刷新可能会删除一些创建的bean并创建其他bean.我想要从交换机加载正确的bean (3认同)

Say*_*adi 9

对于 Spring Boot 应用程序,可以在 Maven 配置文件中添加一个属性pom.xml,然后在 中引用该属性application.properties

添加 Maven 配置文件pom.xml,例如,名为 的属性spring.profile.from.maven

<profiles>
    <profile>
        <id>postgres</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <spring.profile.from.maven>postgres</spring.profile.from.maven>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>
            <dependency>
                <groupId>org.postgresql</groupId>
                <artifactId>postgresql</artifactId>
                <scope>runtime</scope>
            </dependency>
        </dependencies>
    </profile>
    <profile>
        <id>noDb</id>
        <properties>
            <spring.profile.from.maven>noDb</spring.profile.from.maven>
        </properties>
    </profile>
</profiles>
Run Code Online (Sandbox Code Playgroud)

参考 Maven 属性application.properties

spring.profiles.include=@spring.profile.from.maven@
Run Code Online (Sandbox Code Playgroud)

通过此设置,使用 Maven 配置postgres文件或不使用配置文件运行 Maven 会将postgresSpring 配置文件添加到 Spring 的活动配置文件列表中,而使用noDbMaven 配置文件运行 Maven 会将 Spring 配置文件添加noDb到 Spring 的活动配置文件列表中。