`

maven profile和spring profile选择及配置

阅读更多

工作中经常遇到开发、测试、生产等多个环境切换,profile可以解决,目前主流的是spring profile和maven profile两种。以我项目配置文件为例,结构如下,主要的改变是在properties里:



 

  • 一、spring profile

1、在spring的配置文件中配置profile,下面是我的app-context-profile.xml,把profile的配置独立出来,然后引用该配置文件

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:task="http://www.springframework.org/schema/task" xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
      http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd
       http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">

	<description>spring profile配置</description>

	<!-- 开发环境配置文件 -->
	<beans profile="development">
		<context:property-placeholder
			location="classpath*:properties/development/*.properties" />
	</beans>

	<!-- 测试环境配置文件 -->
	<beans profile="test">
		<context:property-placeholder
			location="classpath*:properties/test/*.properties" />
	</beans>

	<!-- 生产环境配置文件 -->
	<beans profile="production">
		<context:property-placeholder
			location="classpath*:properties/production/*.properties" />
	</beans>
</beans>

 

<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			classpath:spring/app-context.xml,
			classpath:spring/app-context-mybatis.xml,
			classpath:spring/app-context-service.xml,
			classpath:spring/app-context-profile.xml,
			classpath:spring/app-context-other.xml
		</param-value>
	</context-param>

  

 

2、在web.xml中配置调用

 

<!-- 在上下文context-param中设置profile.default的默认值 -->
    <context-param>
        <param-name>spring.profiles.default</param-name>
        <param-value>development</param-value>
    </context-param>

    <!-- 在上下文context-param中设置profile.active的默认值 -->
    <!-- 设置active后default失效,web启动时会加载对应的环境信息 -->
    <!-- <param-value>的值和前面配置的beans profile一致,
         需要调用哪种环境修改param-value即可 -->
    <context-param>
        <param-name>spring.profiles.active</param-name>
        <param-value>test</param-value>
    </context-param>

 spring profile适合所有工程,但是切换环境仍然需要修改web.xml。下面的maven profile可以实现零文件修改

 

 

  • 二、maven profile

1、pom.xml文件配置

<profiles>
		<profile>
			<!-- 本地开发环境 -->
			<id>development</id>
			<properties>
				<profiles.active>development</profiles.active>
			</properties>
			<activation>
				<activeByDefault>true</activeByDefault>
			</activation>
		</profile>
		<profile>
			<!-- 测试环境 -->
			<id>test</id>
			<properties>
				<profiles.active>test</profiles.active>
			</properties>
		</profile>
		<profile>
			<!-- 生产环境 -->
			<id>production</id>
			<properties>
				<profiles.active>production</profiles.active>
			</properties>
		</profile>
	</profiles>

	<build>
		<resources>
			<resource>
				<directory>src/main/resources</directory>
				<!-- 资源根目录排除各环境的配置,使用单独的资源目录来指定 -->
				<excludes>
					<exclude>properties/test/**</exclude>
					<exclude>properties/production/**</exclude>
					<exclude>properties/development/**</exclude>
				</excludes>
			</resource>
			<resource>
				<directory>src/main/resources/properties/${profiles.active}</directory>
				<!-- 由于上方没有把profile的文件打包,在此处单独指定profile需要的
                        properties打包到指定位置,如果没有该配置profile需要的文件会采用maven
               的默认配置打到resources目录下而不是resources/properties下 -->
				<targetPath>properties</targetPath>
			</resource>
		</resources>

		<plugins>
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>tomcat-maven-plugin</artifactId>
				<version>1.1</version>
			</plugin>

			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-war-plugin</artifactId>
				<configuration>
					<!-- 激活spring profile -->
					<webResources>
						<resource>
							<filtering>true</filtering>
							<directory>src/main/webapp</directory>
							<includes>
								<include>**/web.xml</include>
							</includes>
						</resource>
					</webResources>
					<warSourceDirectory>src/main/webapp</warSourceDirectory>
					<webXml>src/main/webapp/WEB-INF/web.xml</webXml>
				</configuration>
			</plugin>
		</plugins>
		<finalName>dataservice-gateway</finalName>
	</build>

 

此配置已经可以采用profile打包了,运行mvn clean install -Ptest就可以打出test需要的war包,但这样还不能和eclipse tomcat中发布的一致。

 

2、与eclipse tomcat集成(非maven tomcat插件)

右键选中程序-properties-maven-录入需要运行的profile



 CRTL+ALT+P重新把tomcat  publish一下即可

 

 

如果maven打包后遇到“Execution default-resources of goal org.apache.maven.plugins:maven-resources-plugin:2.6:resources failed”,maven-update projects一下

 

 

  • 大小: 15.6 KB
  • 大小: 25.9 KB
分享到:
评论

相关推荐

    maven+spring+mybatis配置

    1.非web环境下spring如何与mybatis集成 2.maven如何打可以直接运行的jar包 3.maven如何用profile动态打包jdbc.properties 4.maven如何把mybatis的mapper.xml一起打包到jar中

    springboot实现maven打包加载不同环境的方式二

    当前案例中包含一整套的代码和word文档,非常适合新手代码简单易懂; 主要是通过maven打包加载不同环境的properties文件 然后将对于的属性绑定到指定的实体对象中;然后通过调用接口可以看到加载不同环境控制台打印的...

    Maven权威指南 很精典的学习教程,比ANT更好用

    2. 安装和运行Maven 2.1. 验证你的Java安装 2.2. 下载Maven 2.3. 安装Maven 2.3.1. 在Mac OSX上安装Maven 2.3.2. 在Microsoft Windows上安装Maven 2.3.3. 在Linux上安装Maven 2.3.4. 在FreeBSD或OpenBSD上...

    Maven管理SpringBoot Profile详解

    主要介绍了Maven管理SpringBoot Profile详解,具有一定参考价值,需要的朋友可以了解下。

    springboot通过@Profile注解配置不同环境

    通过当前案例和之前的maven 打包加载不同环境的配置文件内容;配合spring的@Profile注解配置在对于的class或者@Bean注解的方法上;来通过觉得某个拦截或者Java代码类是否生效

    sonarqube-jacoco-maven-sample:简单的spring应用程序,配置用于报告单元和集成测试覆盖率,由jacoco提供给sonarqube

    SonarQube中的使用单位和集成测试覆盖率报告示例为了将jacoco报告发布到sonarqube,请使用您的凭据在本地〜/ .m2 / settings.xml文件中设置新的配置文件: &lt;profile&gt; &lt;id&gt;sonar&lt;/id&gt; &lt;activation&gt; &lt;activeByDefault&gt;...

    springboot学习思维笔记.xmind

    依据条件选择配置类 动态注册Bean 测试 Spring TestContext Framework集成测试 SpringMVC基础 Spring MVC概述 SpringMVC项目快速搭建 构建Maven项目 日志配置 演示页面 Spring MVC配置 ...

    JavaEE开发的颠覆者SpringBoot实战[完整版].part3

    3.6.2 第二类:依据条件选择配置类 64 3.6.3 第三类:动态注册Bean 65 3.7 测试 66 3.7.1 点睛 66 3.7.2 示例 67 第二部分 点睛Spring MVC 4.x 第4 章 Spring MVC 基础 72 4.1 Spring MVC 概述 73 4.2 Spring MVC ...

    JavaEE开发的颠覆者SpringBoot实战[完整版].part2

    3.6.2 第二类:依据条件选择配置类 64 3.6.3 第三类:动态注册Bean 65 3.7 测试 66 3.7.1 点睛 66 3.7.2 示例 67 第二部分 点睛Spring MVC 4.x 第4 章 Spring MVC 基础 72 4.1 Spring MVC 概述 73 4.2 Spring MVC ...

    force-maven-plugin:部署到Salesforce的Maven Mojo原型

    尝试一下: 克隆回购mvn install-这将把force-maven-plugin安装到您的本地.m2存储库中在〜/ .m2 / settings.xml中创建Maven配置文件 &lt;profile&gt; &lt;id&gt;my-project&lt;/id&gt; &lt;properties&gt; &lt;sf&gt;xxx&lt;/sf&gt; &lt;sf&gt;xxx&lt;/sf&gt; ...

    JavaEE开发的颠覆者SpringBoot实战[完整版].part1

    3.6.2 第二类:依据条件选择配置类 64 3.6.3 第三类:动态注册Bean 65 3.7 测试 66 3.7.1 点睛 66 3.7.2 示例 67 第二部分 点睛Spring MVC 4.x 第4 章 Spring MVC 基础 72 4.1 Spring MVC 概述 73 4.2 Spring MVC ...

    spring-boot-reference.pdf

    13.2.3. Using the Spring Boot Maven Plugin 13.3. Gradle 13.4. Ant 13.5. Starters 14. Structuring Your Code 14.1. Using the “default” Package 14.2. Locating the Main Application Class 15. ...

    mvn-examples-1.0&Maven;权威指南中文版

    &lt;module&gt;ch07-multi-spring &lt;module&gt;ch08-optimize &lt;module&gt;ch09-pom &lt;module&gt;ch10-lifecycle &lt;module&gt;ch11-profile &lt;module&gt;ch12-assembly &lt;module&gt;ch13-properties &lt;module&gt;ch15-sitegen &lt;module&gt;ch17-...

    Mastering.Spring.Cloud

    Native profile support Building a server-side application Building a client-side application Adding a Eureka Server Client-side bootstrap approaches Config Server discovery Repository backend types ...

    springboot参考指南

    使用Spring Boot Maven插件 ii. 13.2. Gradle iii. 13.3. Ant iv. 13.4. Starter POMs ii. 14. 组织你的代码 i. 14.1. 使用"default"包 ii. 14.2. 定位main应用类 iii. 15. 配置类 目錄 Spring Boot参考指南 2 i. ...

    java8集合源码分析-app-engine-maven:应用引擎Maven

    多Profile支持,Gradle、Spring、应用程序Profile整合 完善的系统监控 热部署 自动生成接口文档 docker支持(gradle创建image、docker-compose) 环境配置 区分有三种环境dev、test、prod,不同环境会加载不同的配置...

    thymeleaf-tests-3.0-spring5.zip

    Tests included cover expressions, standard dialect attributes, Spring integration and also most of the example applications, among other topics. Tests under the org.thymeleaf.engine package are ...

    api-master:api-master是springMVC项目,基于maven多模块和dubbo的rpc方式整合使用

    项目基于maven的多profile环境配置,打包时需要选择(test/pro/dev)打包运行的环境。 **项目特点** - 友好的代码结构及注释,便于阅读及二次开发 。 - 前端页面采用jsp+freemaker,多视图解析处理,优先jsp。 - 采用...

    SpringMVC+多数据源Mysql(Mybatis)+多数据源Mongo

    SpringMVC+Mybatis(多数据源)+Mongo(多数据源),框架适合同时访问多种DB,且支持每种DB配置多个数据源。正如我代码中的demo,配置了两个...除此之外还通过maven profile支持了多环境自动切换配置。欢迎下载,互相学习。

    y2p-maven-plugin

    y2p-maven-plugin说明该maven插件适用于spring boot微服务框架的目录结构,即YourProject src main java resources config application.yml application-dev.yml application-online.yml bootstrap.ymlMaven依赖...

Global site tag (gtag.js) - Google Analytics