maven profile和spring profile选择及配置

4 篇文章 0 订阅

工作中经常遇到开发、测试、生产等多个环境切换,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>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>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一下

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Eclipse 中使用 Maven 启动 Spring Boot 有两种方式:通过 Maven 插件启动或者通过 Eclipse 插件启动。 1. 通过 Maven 插件启动: 步骤如下: 1. 在项目的 pom.xml 文件中添加 Spring Boot Maven 插件: ```xml <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> ``` 2. 在 MavenProfiles 中添加 Spring Boot 启动配置: ```xml <profiles> <profile> <id>spring-boot</id> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>run</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </profile> </profiles> ``` 3. 保存 pom.xml 文件并使用 Maven 命令启动 Spring Boot 应用程序: ```bash mvn spring-boot:run ``` 2. 通过 Eclipse 插件启动: 步骤如下: 1. 安装 Spring Tool Suite 插件。 2. 在项目的 pom.xml 文件中添加 Spring Boot Starter Parent 依赖。 3. 右键点击 pom.xml 文件,选择 Run As -> Maven build。 4. 在 Goals 中输入 `spring-boot:run`,然后点击 Run 按钮。 5. 等待应用程序启动完成后,使用浏览器访问 http://localhost:8080 即可访问应用程序。 注意:在使用 Eclipse 插件启动 Spring Boot 应用程序时,Eclipse 会自动检测项目中是否引入了 Spring Boot 相关依赖,如果没有则会提示你手动添加依赖。同时,Eclipse 也会自动配置应用程序的端口号和上下文路径等参数,无需手动配置

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值