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

时间:2014-08-21 07:37:19

标签: java spring maven spring-profiles

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

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

我想要做的是maven中的所有活动配置文件也将被移植到spring active配置文件中,因此我可以在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作为活动配置文件。

6 个答案:

答案 0 :(得分:32)

有一种更优雅的方式可以同时在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:runmvn jetty:run -Dspring.profiles.active=postgres命令在Postgres数据库上运行我的网络应用
  • 使用mvn clean jetty:run -Dspring.profiles.active=h2
  • 在H2 DB上运行我的网络应用

答案 1 :(得分:19)

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

例如

spring.profile = ${mySpringProfile}

对于每个配置文件,请为此变量定义一个值(mySpringProfile)。

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

然后在您的应用程序的引导程序中,您将根据此文件选择适当的配置文件(无法帮助您,因为您没有提供更多信息,但这很容易。

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

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

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

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

答案 2 :(得分:18)

您需要的第一件事是保存配置的两个属性文件。文件名应与模式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}表示法。

它完美无缺。希望这可以帮到你。

答案 3 :(得分:2)

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

使用例如名为pom.xml的属性将Maven配置文件添加到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>

引用application.properties中的Maven属性:

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

通过此设置,使用postgres Maven配置文件运行Maven或不使用任何配置文件运行maven会将postgres Spring配置文件添加到Spring的活动配置文件列表中,同时使用noDb Maven运行maven配置文件将noDb Spring配置文件添加到Spring的活动配置文件列表中。

答案 4 :(得分:1)

我目前正在构建一个小型Web应用程序(由于我无法控制的原因)必须能够在仅支持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 {
    // ...
}

答案 5 :(得分:0)

在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>
相关问题