使用Maven自动打包配置文件生成开发环境,测试环境,生产环境
来源:互联网 发布:linux服务器宕机原因 编辑:程序博客网 时间:2024/05/22 02:30
写在前面
项目中往往有开发环境、测试环境、生产环境,这几个环境有些配置文件可能是不一样的,三个环境切换时以前基本是靠手动注释文件来打成不同的包。如果不同的配置文件过多,粗心打包后,就会导致配置文件出错,从而使项目部署出问题。例如Mysql配置文件在项目中有可能会使用如下情况,通过手动注释切换配置文件。
#本地数据库#url:jdbc:mysql://***#username:dev#password:root#线上数据库url:jdbc:mysql://***username:formalpassword:admin#测试数据库#url:jdbc:mysql://**#username:test#password:test
为了解决这个问题,可以使用Maven不同的打包命令生成不同的Jar包
- 开发环境打包命令
mvn clean install -P dev
- 测试环境打包命令
mvn clean install -P test
- 生产环境打包命令
mvn clean install -P formal
创建普通Maven项目
项目结构如下图所示
System.properties 配置文件
app.mode=${app.mode}
app-develop.properties 开发配置文件
app.mode=develop
app-formal.properties 生产配置文件
app.mode=formal
app.test.properties 测试配置文件
app.mode=test
以上配置文件中,System.properties为项目打包后真正使用的文件,其它三个分别存放各自配置文件,当打包时,Maven会将对应的参数填充到System.properties中
注意事项
项目中使用的配置参数,必需在System.properties 中有,并通过${参数名}进行引用
POM 配置文件
<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.zhaochao</groupId> <artifactId>TestDemo</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>TestDemo</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.21</version> </dependency> </dependencies> <profiles> <profile> <id>test</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <build.profile.id>test</build.profile.id> </properties> </profile> <profile> <id>develop</id> <properties> <build.profile.id>develop</build.profile.id> </properties> </profile> <profile> <id>formal</id> <properties> <build.profile.id>formal</build.profile.id> </properties> </profile> </profiles> <build> <filters> <filter>src/app-${build.profile.id}.properties</filter> </filters> <sourceDirectory>src/main/java</sourceDirectory> <!-- 定义打包资源文件--> <resources> <resource> <filtering>true</filtering> <!--加上filter会过滤该资源路径中的文件--> <directory>${project.basedir}/src/main/resources</directory> </resource> </resources> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.4.2</version> <configuration> <createDependencyReducedPom>true</createDependencyReducedPom> <filters> <filter> <artifact>*:*</artifact> <excludes> <exclude>META-INF/*.SF</exclude> <exclude>META-INF/*.DSA</exclude> <exclude>META-INF/*.RSA</exclude> </excludes> </filter> </filters> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>com.zhaochao.Main</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build></project>
Main测试函数
public class Main { public Properties properties; public String loadProperties(String key) throws IOException { if (properties == null) { properties = new Properties(); properties.load(this.getClass().getClassLoader().getResourceAsStream("System.properties")); } return properties.getProperty(key); } public static void main(String[] args) throws IOException { Main main = new Main(); System.out.println(main.loadProperties("app.mode")); }}
测试结果
在Idea中,勾选Maven Projects中->Profiles->develop运行main则是使用开发环境的配置,如下图所示
也可以通过以下命令生成对就的Jar包
mvn clean install -P dev mvn clean install -P testmvn clean install -P formal
阅读全文
1 0
- 使用Maven自动打包配置文件生成开发环境,测试环境,生产环境
- Maven 生产环境 测试环境 开发环境 打包配置
- maven 开发环境 、测试环境、生产环境打包
- maven 开发环境 、测试环境、生产环境打包
- maven 开发环境 、测试环境、生产环境打包
- Maven 生产环境 测试环境 开发环境 打包配置
- maven 生产环境、开发环境替换配置文件打包
- Maven多环境配置:开发环境、测试环境、生产环境打包配置
- maven 为测试环境、生产环境等不同环境打包
- 使用Maven的profile对生产测试环境打包
- maven: 开发、测试和生产环境使用不同的配置文件例子
- 实现开发环境、测试环境、生产环境配置自动切换
- 实现开发环境、测试环境、生产环境配置自动切换
- Maven 如何为不同的环境打包 —— 开发、测试和生产环境
- maven如何为不同的环境打包-开发、测试、生产环境
- Maven 如何为不同的环境打包 —— 开发、测试和生产环境
- Maven 如何为不同的环境打包 —— 开发、测试和生产环境
- Maven 使用profile来区分开发、测试、生产环境
- IT 面试总结
- es6 iterator(十四)
- POJ
- Android 用ViewPager实现加载两个webview的可滑动Tab页
- 重构?先想好。
- 使用Maven自动打包配置文件生成开发环境,测试环境,生产环境
- java学习(11) 枚举enum
- Uvalive3353 Optimal Bus Route Design 带权二分图匹配
- MySQL主从配置详解
- Openstack之Nova创建虚机流程分析
- [leetcode]621. Task Scheduler
- 安装JDK1.8.0_73 配置JAVA的环境变量
- java android 占位符
- C#数组的使用