前几天项目需要用到分环境打包, 于是研究了下, 由于项目基于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. [翻译]Component Registration in Script System 在脚本系统中注册组件

    Component Registration in Script System 在脚本系统中注册组件   To refer to our component from a script, the cl ...

  2. Android应用安全解决方案

    Apk安全解决方案 背景 公司为政府做的App开发完了,需要上一些手段保证安全.这样客户才放心嘛. 防止第三方反编译篡改应用,防止数据隐私泄露,防止二次打包欺骗用户. 目录 Apk安全解决方案 背景 ...

  3. 简述System.Windows.Forms.Timer 与System.Timers.Timer用法区别

    System.Windows.Forms.Timer 基于窗体应用程序 阻塞同步 单线程 timer中处理时间较长则导致定时误差极大. System.Timers.Timer 基于服务 非阻塞异步 多 ...

  4. django系列4.1--模版系统,过滤器,标签,模版继承,组件

    django 模版系统 一. 语法 { { 变量 } } {% 表达式 %} 二. 变量 { {变量名} } 深度查询据点符( . )在模版语言中有特殊的含义. 当模版系统遇到点(.) 查询顺序如下: ...

  5. WEB新手之布尔盲注

    开始写SQL的题,单引号等特殊符号闭合漏洞比较简单,因此从Less-5开始写. 布尔盲注是多种盲注的一种,这种盲注方法也比较好理解.但使用这种盲注有一个前提,就是网页会有回显,透过这个回显,我们可以通 ...

  6. AVA + Spectron + JavaScript 对 JS 编写的客户端进行自动化测试

    什么是 AVA (类似于 unittest) AVA 是一种 JavaScript 单元测试框架,是一个简约的测试库.AVA 它的优势是 JavaScript 的异步特性和并发运行测试, 这反过来提高 ...

  7. scrapy 资料整合

    先看看scrapy的框架流程, 1,安装 scrapy 链接 查看即可. 2,新建scrapy项目 scrapy startproject 项目名 目录结构图 3,cd到项目名下,创建任务. scra ...

  8. Linux 服务器加入Windows AD

    背景信息: Windows AD Version: Windows Server 2012 R2 zh-cn 计算机全名:hlm12r2n1.hlm.com 域:hlm.com 域控管理员:stone ...

  9. mysql常用日期、时间查询

    好记性不如烂笔头 select curdate(); --获取当前日期 select last_day(curdate()); --获取本月最后一天. day); -- 获取本月第一天 ,interv ...

  10. 实现函数 ToLowerCase()

    /** * 实现函数 ToLowerCase(),该函数接收一个字符串参数 str, 并将该字符串中的大写字母转换成小写字母,之后返回新的字符串. * 输入: "Hello" * ...