filter打包区分环境
2024-09-05 21:36:29
项目目录结构:
父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就顺利通过,之后再子项目打包。
最新文章
- js基本类型和引用类型
- Mongodb集群搭建的三种方式
- ArcGIS 的 Oracle 数据库的要求
- SQL常用字段类型
- 建筑材料系统 ASP.NET MVC4.0 + WebAPI + EasyUI + Knockout 的架构设计开发
- Unity3D插件分享
- 循环多少次?[HDU1799]
- weblogic .NoClassDefFoundError: Could not initialize class sun.awt.X11Graphi
- HDU 4870Rating(推公式)
- iOS开发中@selector的理解
- perl 变量详解
- Final Exam Arrangement(ZOJ)
- PAT (Advanced Level) 1108. Finding Average (20)
- win7 安装 vagrant + centos + virtualbox
- PHP:基于百度大脑api实现OCR文字识别
- swift 实践- 01 -- UItableView的简单使用
- JS高级程序设计2
- Codeforces 895C - Square Subsets
- 基于Linux的Samba开源共享解决方案测试(四)
- 通过LDAP验证Active Directory服务