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:run或mvn jetty:run -Dspring.profiles.active=postgres命令在Postgres DB上运行我的web-appmvn clean jetty:run -Dspring.profiles.active=h2Ser*_*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}表示法.
它努力完美.希望这可以帮到你.
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:这实际上是一种不好的做法,您应该在运行时使用系统属性设置配置文件
对于 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 的活动配置文件列表中。
| 归档时间: |
|
| 查看次数: |
68336 次 |
| 最近记录: |