繁体   English   中英

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

[英]How to set spring active profiles with maven profiles

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

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

我想要做的是 maven 中的所有活动配置文件也将被移植到 spring 活动配置文件,以便我可以在 bean 签名( @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>

假设我在没有指定任何其他配置文件的情况下运行 maven,我希望 spring 将profile1development作为活动配置文件。

有一种更优雅的方式可以同时在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>

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

<context-param>
   <param-name>spring.profiles.default</param-name>
   <param-value>postgres</param-value>
</context-param>

第三 ,将配置文件相关的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>

现在有可能:

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

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

例如

spring.profile = ${mySpringProfile}

并为每个配置文件定义此变量的值( 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>

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

您需要的第一件事是两个属性文件,用于保持您的配置。 文件名应与模式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>

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

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
    …
</build>

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

spring.profiles.active=@activatedProperties@

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

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

我目前正在构建一个小型webapp(由于我无法控制的原因)必须能够在仅支持Servlet 2.5和Java 6的旧服务器/容器上运行。还需要将webapp配置完全自包含,因此甚至不能使用系统变量和/或JVM参数。 管理员只需要一个.war文件,用于可以放入容器进行部署的每个环境。

我在我的webapp中使用Spring 4.x. 这就是我配置应用程序的方式,以便使用活动的Maven配置文件来设置活动的Spring 4.x配置文件。


pom.xml文件更改

我将以下位添加到我的POM文件中。 我的POM正在使用模型版本4.0.0,当我进行构建时,我正在运行Maven 3.1.x.

<modelVersion>4.0.0</modelVersion>

...

<profiles>
    <profile>
        <id>dev</id>
        <activation>
            <!-- Default to dev so we avoid any accidents with prod! :) -->
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <!-- This can be a single value, or a comma-separated list -->
            <spring.profiles.to.activate>dev</spring.profiles.to.activate>
        </properties>
    </profile>
    <profile>
        <id>uat</id>
        <properties>
            <!-- This can be a single value, or a comma-separated list -->
            <spring.profiles.to.activate>uat</spring.profiles.to.activate>
        </properties>
    </profile>
    <profile>
        <id>prod</id>
        <properties>
            <!-- This can be a single value, or a comma-separated list -->
            <spring.profiles.to.activate>prod</spring.profiles.to.activate>
        </properties>
    </profile>
</profiles>

...

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <webResources>
                    <webResource>
                        <filtering>true</filtering>
                        <directory>src/main/webapp</directory>
                        <includes>
                            <include>**/web.xml</include>
                        </includes>
                    </webResource>
                </webResources>
                <failOnMissingWebXml>true</failOnMissingWebXml>
            </configuration>
        </plugin>
        ...
    </plugins>
</build>

web.xml文件更改

<!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Setup for root Spring context
-->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring-core-config.xml</param-value>
</context-param>
<!--
Jim Tough - 2016-11-30
Per Spring Framework guide: http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#beans-environment

...profiles may also be activated declaratively through the spring.profiles.active 
property which may be specified through system environment variables, JVM system 
properties, servlet context parameters in web.xml, or even as an entry in JNDI.
-->
<context-param>
    <param-name>spring.profiles.active</param-name>
    <param-value>${spring.profiles.to.activate}</param-value>
</context-param>
<!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->

现在我可以创建类似于下面的配置类,只有在特定的Spring配置文件处于活动状态时才会使用。

@Configuration
@Profile({"dev","default"})
@ComponentScan
@EnableTransactionManagement
@EnableSpringDataWebSupport
public class PersistenceContext {
    // ...
}

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

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

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

application.properties引用Maven属性:

spring.profiles.include=@spring.profile.from.maven@

使用此设置,使用postgres Maven配置文件或没有配置文件运行maven会将postgres Spring配置文件添加到Spring的活动配置文件列表中,而使用noDb Maven配置文件运行maven会将noDb Spring配置文件添加到Spring的活动配置文件列表中。

在web.xml中添加占位符${activeProfile}

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

在每个配置文件的pom.xml中设置属性:

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

添加maven-war-plugin并设置<filteringDeploymentDescriptors>true</filteringDeploymentDescriptors>以在运行mvn package -Pprofile1mvn package -Pprofile2时替换占位符:

<build>
  <plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>3.2.2</version>
    <configuration>
      <filteringDeploymentDescriptors>true</filteringDeploymentDescriptors>
    </configuration>
  </plugin>
</build>

spring boot插件本身可以帮助:

  <profiles>
    <profile>
      <id>postgres</id>
      <build>
        <plugins>
          <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
              <jmxPort>9001</jmxPort>
              <environmentVariables>
                <SPRING_PROFILES_ACTIVE>postgres</SPRING_PROFILES_ACTIVE>
              </environmentVariables>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
    <profile>
      <id>h2</id>
      <build>
        <plugins>
          <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
              <jmxPort>9002</jmxPort>
              <environmentVariables>
                <SPRING_PROFILES_ACTIVE>h2</SPRING_PROFILES_ACTIVE>
              </environmentVariables>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>

在 spring boot 应用程序中,有多种方法可以设置配置文件(dev、uat、prod 等)

例如:您可以将其添加到您的属性文件中:

spring.profiles.active=dev

以编程方式:

SpringApplication.setAdditionalProfiles("dev");

要指定哪些配置文件处于活动状态,请使用此行

@ActiveProfiles("dev")

在 Unix 环境中

export spring_profiles_active=dev

使用 dev 配置文件运行 jar 文件。

java -jar -Dspring.profiles.active=dev JARNAME.jar

这里JARNAME.jar意味着你的应用程序的 jar

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM