前几天项目需要用到分环境打包, 于是研究了下, 由于项目基于springboot的, 所以分两个情况进行说明:

1), springboot的多环境配置

2), maven-springboot的多环境配置

项目gitHub地址: https://github.com/wenbronk/springboot-maven-profile

1, springboot的环境配置比较简单, yml和properties的配置相似, 不同的是, yml只需要在同一个文件中, properties需要3个不同的文件, 分别说明:

1) pom.xml 文件:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.</modelVersion>
<groupId>com.wenbronk</groupId>
<artifactId>springboot-profiles</artifactId>
<version>0.0.-SNAPSHOT</version> <!-- 添加父依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5..RELEASE</version>
</parent> <!-- 锁定jdk版本 -->
<properties>
<!-- 指定启动类(main方法的位置) -->
<start-class>com.wenbronk.App</start-class>
<java.version>1.8</java.version>
<!-- 构建编码 -->
<project.build.sourceEncoding>UTF-</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-</project.reporting.outputEncoding>
</properties> <dependencies> <!-- exclude掉spring-boot的默认log配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- spring-boot的web启动的jar包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 为了构建一个即是可执行的,又能部署到一个外部容器的war文件,你需要标记内嵌容器依赖为"provided" -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- 引入log4j2依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<!-- 加上这个才能辨认到log4j2.yml文件 -->
<dependency> <!-- 加上这个才能辨认到log4j2.yml文件 -->
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.</version>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies> <build>
<finalName>spring-boot-profiles</finalName>
</build> </project>

2, application.yml

spring:
profiles:
active: dev # 开发环境
---
spring:
profiles: dev server:
port: 8090

my:
  properties:
    abc: devdevdev


# 测试环境配置
---
spring:
profiles: qa server:
port:

my:
  properties:
    abc: devdevdev


# 生产环境配置
---
spring:
profiles: prod
server:
port: 8000

my:
  properties:
    abc: devdevdev

 
 

3, 编写一个类测试:

App.java

package com.wenbronk.maven;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer; @SpringBootApplication
public class App extends SpringBootServletInitializer { /**
* war使用
*/
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(App.class);
} /**
* jar使用
* @param args
* @throws Exception
* @time 2017年5月15日
*/
public static void main(String[] args) throws Exception {
SpringApplication.run(App.class, args);
} }

TestController.java

package com.wenbronk.maven.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import com.wenbronk.maven.config.TestConfig; /**
* 测试多环境部署
*
* @author wenbronk
* @time 2017年5月12日
*/
@RestController
public class TestController { @Autowired
private Environment env; @Autowired
private TestConfig testConfig; /**
*
* @return
* @time 2017年5月12日
*/
@RequestMapping(value="/test")
public String test() {
System.out.println("connect success");
String property = env.getProperty("profile");
System.out.println("property " + property); String abc = testConfig.getAbc();
System.out.println(abc); return "sucess";
} }

TestConfig.java

package com.wenbronk.maven.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import com.wenbronk.maven.config.TestConfig; /**
* 测试多环境部署
*
* @author wenbronk
* @time 2017年5月12日
*/
@RestController
public class TestController { @Autowired
private Environment env; @Autowired
private TestConfig testConfig; /**
*
* @return
* @time 2017年5月12日
*/
@RequestMapping(value="/test")
public String test() {
System.out.println("connect success");
String property = env.getProperty("profile");
System.out.println("property " + property); String abc = testConfig.getAbc();
System.out.println(abc); return "sucess";
} }

此时启动端口可以看到由yml衷配置文件所制定的配置生效

2 maven-springboot

上面的配置有时候并不能满足我们的需要, 比如 db.properites, 不通的环境需要打包不同文件夹下的配置文件, 这时候还需要maven的打包方式

mvn clean package -Dmaven.test.skip=true -P dev -e

1, pom.xml的配置

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.</modelVersion>
<groupId>com.wenbronk</groupId>
<artifactId>springboot-profiles-maven</artifactId>
<version>0.0.-SNAPSHOT</version>
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>1.5..RELEASE</version>
</parent> <properties>
<start-class>com.wenbronk.profile.App</start-class>
<java.version>1.8</java.version>
</properties> <dependencies>
<!-- exclude掉spring-boot的默认log配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- spring-boot的web启动的jar包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 为了构建一个即是可执行的,又能部署到一个外部容器的war文件,你需要标记内嵌容器依赖为"provided" -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- 引入log4j2依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<!-- 加上这个才能辨认到log4j2.yml文件 -->
<dependency> <!-- 加上这个才能辨认到log4j2.yml文件 -->
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <!-- springboot 热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<scope>true</scope>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency> <dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
</dependencies> <profiles>
<profile>
<id>dev</id>
<properties>
<profileActive>dev</profileActive>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>qa</id>
<properties>
<profileActive>qa</profileActive>
</properties>
</profile>
</profiles> <build>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>dev_conf/*</exclude>
<exclude>qa_conf/*</exclude>
</excludes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources/${profileActive}_conf</directory>
</resource>
</resources> <plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- 如果没有这个, 不生效 -->
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build> </project>

2, src/main/resources/文件夹下放置不同环境的配置文件:

这儿使用application.properites, 使用yml就用上面那种方式

application.properties

spring.profiles.active=@profileActive@

applicatio-dev.properties

profile=dev_enviroment
server.port= my.properties.abc=devdevdev

application-prod.properties

profile=prod_enviroment
server.port=
my.properties.abc=prodprodprod

application-qa.properties

profile=qa_enviroment
server.port=
my.properties.abc=qaqaqaqa

3, 放置不同的 conf 下

---  dev_conf/db.properties

---  qa_conf/db.properties

qwer=

4, 接下来是程序编码

App.java, 在这儿为了区分, 在日志中将 使用的配置文件信息打印出来

package com.wenbronk.profile;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.ConfigurableApplicationContext; @SpringBootApplication
public class App extends SpringBootServletInitializer { /**
* war使用
*/
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(App.class);
} /**
* jar使用
* @param args
* @throws Exception
* @time 2017年5月15日
*/
public static void main(String[] args) throws Exception {
ConfigurableApplicationContext context = SpringApplication.run(App.class, args);
String[] activeProfiles = context.getEnvironment().getActiveProfiles();
for (String profile : activeProfiles) {
System.out.println("Spring Boot 使用profile为:" + profile);
}
}
}

TestConfig.java , 用于读取application-xxx.properties中的信息

package com.wenbronk.profile.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component; @Component
@ConfigurationProperties(prefix = "my.properties")
public class TestConfig {
private String abc; public String getAbc() {
return abc;
} public void setAbc(String abc) {
this.abc = abc;
}
}

ResourceLoader.java, 读取外部资源文件

package com.wenbronk.profile.resource;

import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component; /**
* 实现resourceLoaderAware , 加载外部资源文件
*
* @author wenbronk
* @time 2017年5月15日
*/
@Component
public class MyResourceLoader implements ResourceLoaderAware{ private ResourceLoader resourceLoader; public Resource getresourceLoader(String path) {
return resourceLoader.getResource("classpath:" + path);
} @Override
public void setResourceLoader(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
} }

controller.java测试

package com.wenbronk.profile.controller;

import java.io.IOException;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import com.wenbronk.profile.config.TestConfig;
import com.wenbronk.profile.resource.MyResourceLoader; /**
* 测试springboot-maven 分环境打包
*
* @author wenbronk
* @time 2017年5月15日
*/
@RestController
public class TestController { /**
* 可直接使用environment来配置
*/
@Autowired
private Environment env; @Autowired
private TestConfig testConfig; @Autowired
private MyResourceLoader resourceLoader; @RequestMapping("/test")
public String test() throws IOException {
String abc = testConfig.getAbc();
System.out.println(abc); String property = env.getProperty("profile");
System.out.println("property " + property); Resource resource = resourceLoader.getresourceLoader("db.properties");
String string = IOUtils.toString(resource.getInputStream(), "utf-8");
System.out.println(string);
return abc;
} }

运行后可以根据输入命令更改配置

为了进一步验证, 我们分别打 了war包和jar包, 可以看到根据输入的maven指令不同而替换不通的配置文件

在springboot测试中使用多环境:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@ActiveProfiles("prod")

原创地址: http://www.cnblogs.com/wenbronk/p/6855973.html, 转载请注明出处, 谢谢

springboot-21-maven多环境打包的更多相关文章

  1. 用maven按环境打包SpringBoot的不同配置文件

    利用maven按环境打包SpringBoot的不同配置文件 application-dev.properties对应开发环境 application-test.properties对应测试环境 app ...

  2. springboot学习之maven多环境打包的几种方式

    在应用部署的时候,往往遇到需要发布到不同环境的情况,而每个环境的数据库信息.密钥信息等可能会存在差异. 1.在默认的application.properties或者yaml中设置profile spr ...

  3. 使用Maven分环境打包:dev sit uat prod

    使用Maven管理的项目,经常需要根据不同的环境打不同的包,因为环境不同,所需要的配置文件不同,比如database的连接信息,相关属性等等. 在Maven中,我们可以通过P参数和profiles元素 ...

  4. Maven多环境打包

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  5. Springboot与Maven多环境配置文件夹解决方案

    Profile用法 我们在application.yml中为jdbc.name赋予一个值,这个值为一个变量 jdbc: username: ${jdbc.username} Maven中的profil ...

  6. Maven 多环境 打包

    1.pom.xml文件添加profiles属性 <profiles> <profile> <id>dev</id> <activation> ...

  7. maven 不同环境打包命令

    mvn clean package mvn clean package -Pdev mvn clean package -Ptest mvn clean package -Pproduct

  8. springboot分环境打包(maven动态选择环境)

    分环境打包核心点:spring.profiles.active pom.xml中添加: <profiles> <profile> <id>dev</id> ...

  9. springboot不同环境打包

    1. 场景描述 springboot+maven打包,项目中经常用到不同的环境下打包不同的配置文件,比如连接的数据库.配置文件.日志文件级别等都不一样. 2. 解决方案 在pom.xml文件中定义 2 ...

  10. maven为不同环境打包(hibernate)-超越昨天的自己系列(6)

    超越昨天的自己系列(6) 使用ibatis开发中,耗在dao层的开发时间,调试时间,差错时间,以及适应修改需求的时间太长,导致项目看起来就添删改查,却特别费力.   在项目性能要求不高的情况下,开始寻 ...

随机推荐

  1. persona 典型用户

    1.姓名:王涛 2.年龄:22 3.收入:基本无收入 4.代表用户在市场上的比例和重要性:王涛为铁道学生.本软件的用户主要是学生和老师,尤其是广大的铁大学子,所以此典型用户的重要性不言而喻,而且比例相 ...

  2. delphi 拆分字符串

    最近在使用Delphi开发一种应用系统的集成开发环境.其中需要实现一个字符串拆分功能,方法基本原型应该是:procedure SplitString(src: string ; ch: Char; v ...

  3. Hibernate 零配置之Annotation注解

    JPA规范推荐使用Annotation来管理实体类与数据表之间的映射关系,从而避免同时维护两份文件(Java 实体类 和 XML 映射文件),将映射信息(写在Annotation中)与实体类集中在一起 ...

  4. Application可以被重用,从哪里看出来的?

    一开始Context是静态的,并且创建时赋值,然后校验用户访问权限的时候,出现了问题, 调试看到,每次请求的url都一样,我就发现了每次Contetx都是一样的, 说明每次请求的Application ...

  5. NPOI 设置excel 边框

    https://blog.csdn.net/xxs77ch/article/details/50232343

  6. VS2015 IIS Express 无法启动 解决办法(转)

    因为安装各种乱七八糟的软件,然后不小心把IIS Express卸载掉了,网上下载了一个IIS Express 7,安装之后本地使用VS 2015无法启动调试,F5 无法启动IIS, 再次F5调试,没有 ...

  7. sqlite 插入数据 too many variables

    相关文档:http://www.sqlite.org/limits.html#max_variable_number 一次插入条数限制500,参数最多999个.

  8. “全栈2019”Java第一章:安装JDK11(Mac)

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 文章原文链接 “全栈2019”Java第一章:安装JDK11(Mac) 下一章 “全栈2019”Java ...

  9. 【文文殿下】CF1029F Multicolored Markers

    这道题考场上卡了文文相当长的时间,所以写个题解泄泄愤QAQ 题意:给你$a$块红瓷砖,$b$块白瓷砖,在一个无限大的地上拼装,要求整体是一个矩形,并且至少有一种颜色是一个矩形,求最小周长. 题解: 首 ...

  10. 如何学习sql语言?

    如何学习 SQL 语言? https://www.zhihu.com/question/19552975 没有任何基础的人怎么学SQL? https://www.zhihu.com/question/ ...