项目目录结构:

父pom:

<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>
<groupId>com.test.env</groupId>
<artifactId>test-environment</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>test-common</module>
<module>environment-demo</module>
</modules>
</project>

common pom:

<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>test-environment</artifactId>
<groupId>com.test.env</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>test-common</artifactId>
<version>1.0-SNAPSHOT</version>
<name>test-common</name>
</project>  

demo pom,filter区分环境:

<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>test-environment</artifactId>
<groupId>com.test.env</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<properties>
<java.version>1.8</java.version>
<project.version>1.0.0-RELEASE</project.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<springboot.version>1.5.20.RELEASE</springboot.version>
</properties> <artifactId>environment-demo</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${springboot.version}</version>
</dependency>
<dependency>
<groupId>com.test.env</groupId>
<artifactId>test-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies> <build>
<finalName>environment-demo</finalName>
<filters>
<filter>../filter/${env}/common.properties</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build> <profiles>
<profile>
<id>development</id>
<properties>
<env>dev</env>
<build.level>compile</build.level>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>beta</id>
<properties>
<env>beta</env>
<build.level>provided</build.level>
</properties>
</profile>
<profile>
<id>production</id>
<properties>
<env>production</env>
<build.level>provided</build.level>
</properties>
</profile>
</profiles> </project>

 spring-beans.xml:EnvironmentBean在common中

spring-confog.xml

springboot 通过BaseConfig导入spring配置

@SpringBootConfiguration
@ImportResource("classpath:spring/spring-config.xml")//多个bean的配置文件
public class BaseConfig {
}

springboot:

@SpringBootApplication
public class EnvironmentDemo implements CommandLineRunner{
@Autowired
EnvironmentBean config;
@Override
public void run(String... args) throws Exception {
System.out.println("config:"+config);
}
public static void main(String[] args) {
SpringApplication.run(EnvironmentDemo.class);
}
}

在demo 目录下区分打包beta,production环境:mvn clean package -P beta/production

spring-beans.xml 中env.name/desc的值会被替换为相应环境的值。

注意:如果是第一次打包可能会遇到Could not find artifact XXX,这是因为需要父工程打包到本地仓库,整个大项目一期build install就顺利通过,之后再子项目打包。

 

最新文章

  1. js基本类型和引用类型
  2. Mongodb集群搭建的三种方式
  3. ArcGIS 的 Oracle 数据库的要求
  4. SQL常用字段类型
  5. 建筑材料系统 ASP.NET MVC4.0 + WebAPI + EasyUI + Knockout 的架构设计开发
  6. Unity3D插件分享
  7. 循环多少次?[HDU1799]
  8. weblogic .NoClassDefFoundError: Could not initialize class sun.awt.X11Graphi
  9. HDU 4870Rating(推公式)
  10. iOS开发中@selector的理解
  11. perl 变量详解
  12. Final Exam Arrangement(ZOJ)
  13. PAT (Advanced Level) 1108. Finding Average (20)
  14. win7 安装 vagrant + centos + virtualbox
  15. PHP:基于百度大脑api实现OCR文字识别
  16. swift 实践- 01 -- UItableView的简单使用
  17. JS高级程序设计2
  18. Codeforces 895C - Square Subsets
  19. 基于Linux的Samba开源共享解决方案测试(四)
  20. 通过LDAP验证Active Directory服务

热门文章

  1. Java多线程系列-基本概念
  2. 微信小程序云开发-云存储-使用云开发控制台存储文件
  3. 微信小程序云开发-数据查询的两种写法
  4. jumpserver 用户,系统用户和管理用户 普通用户和特权用户 区别
  5. Bigdecimal用法
  6. 利用奇偶数来获取websocket推送时间间隔(或者比较前一个数和下一个数的变化)
  7. 大数据学习(20)—— Zookeeper介绍
  8. python实现常用五种排序算法
  9. Centos8 Tomcat 开机自启配置
  10. Linux 数据库操作(一)