在配置文件中直接指定java
spring.profiles.active=test
这种方式很是不灵活,在实际开发部不太会使用到web
使用占位符,在打包时替换,以mavne为例:spring
首先在配置文件中增长:tomcat
spring.profiles.active=@package.target@
在pom.xml中增长不一样环境打包的配置:app
<profiles> <profile> <id>dev</id><!-- 开发环境 --> <properties> <package.target>dev</package.target> </properties> <activation> <activeByDefault>true</activeByDefault> </activation> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.6.1</version> </dependency> </dependencies> </profile> <profile> <id>prod</id><!-- 生产环境 --> <properties> <package.target>prod</package.target> </properties> </profile> <profile> <id>test</id><!-- 测试环境 --> <properties> <package.target>test</package.target> </properties> <dependencies> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.6.1</version> </dependency> </dependencies> </profile> </profiles> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering><!-- 使用package.target值,替换配置文件中 @package.target@ --> </resource> </resources> </build>
执行打包命令:eclipse
mvn package -Ptest
缺点:每次打包都要指定profilemaven
JVM参数方式:spring-boot
java命令行:单元测试
java -jar app.jar --spring.profiles.active=dev
tomcat 中 catalina.bat(.sh中不用“set”) 添加JAVA_OPS。经过设置active选择不一样配置文件:测试
set JAVA_OPTS="-Dspring.profiles.active=test"
eclipse 中启动tomcat。项目右键 run as –> run configuration–>Arguments–> VM arguments中添加。
-Dspring.profiles.active="dev"
web.xml方式
<init-param> <param-name>spring.profiles.active</param-name> <param-value>production</param-value> </init-param>
标注方式(junit单元测试很是实用)
@ActiveProfiles({"unittest","productprofile"})
ENV方式(建议使用此方式)
系统环境变量SPRING_PROFILES_ACTIVE(注意:是大写)
优势: