maven发布时在不同的环境使用不同的配置文件
来源:互联网 发布:php 7 加密 编辑:程序博客网 时间:2024/05/22 15:09
maven发布时在不同的环境使用不同的配置文件
在开发时,不同的环境总会使用到不同的配置。如本地,测试,预发布,发布等环境,像数据库这些都要使用到不同的配置。如果手动改的话肯定会十分的麻烦。
还好maven提供的功能能够帮我们解决这个问题。
我们通过不同环境使用不同数据库的配置来说明
直接上代码:
1.db.properties
jdbc.username=${jdbc.username}jdbc.password=${jdbc.password}jdbc.url=${jdbc.url}name=${myName}
2.dev.properties
jdbc.url=jdbc:mysql://127.0.0.1:3306/devdb?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNulljdbc.username=devuserjdbc.password=dev123456
3.product.properties
jdbc.url=jdbc:mysql://127.0.0.1:3306/productdb?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNulljdbc.username=productuserjdbc.password=product123456
4.test.properties
jdbc.url=jdbc:mysql://127.0.0.1:3306/testdb?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNulljdbc.username=testuserjdbc.password=test123456
5.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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>mavenImparityProfile</groupId> <artifactId>mavenImparityProfile</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>mavenImparityProfile Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <profiles> <profile> <id>test</id> <properties> <env>test</env><!--相当于定义一个变量 供下面使用--> <myName>张三</myName><!--使用一个properties文件中未定义,但是其他地方会取值的变量--> </properties> <activation><!--默认激活--> <activeByDefault>true</activeByDefault> </activation> </profile> <profile> <id>dev</id> <properties> <env>dev</env> <myName>李四</myName> </properties> <activation><!--默认激活--> <activeByDefault>false</activeByDefault> </activation> </profile> <profile> <id>product</id> <properties> <env>product</env> </properties> </profile> </profiles> <build> <finalName>mavenImparityProfile</finalName> <filters><!--获得过滤使用的源文件 即有实际数据的地反--> <filter>src/main/resources/properties/${env}.properties</filter> </filters> <!-- 指定 src/main/resources下所有文件及文件夹为资源文件 --> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <!--是否使用过滤器--> </resource> <!-- 第二中方式 设置对dev.properties,等进行过虑,即这些文件中的${key}会被替换掉为真正的值--> <!-- <resource> <directory>src/main/resources/properties/properties</directory><!–一定要指向上层目录–> <includes> <!–要遍历出来文件都必须写–> <include>product.properties</include> <include>test.properties</include> <include>dev.properties</include> <include>db.properties</include> </includes> <filtering>true</filtering> </resource>--> </resources> </build></project>
其实现主要是通过配置frofile来实现。上面配置了3个环境(test,dev,product)。test环境是默认激活的。
我们直接执行 deploy 则使用的是test的配置。
如果要使用product的配置,则使用maven的命令 mvn clean package -P product (注:-P要大写 -P后面的参数是我们前面定义不同环境的id。如果你使用的是idea工具,在配置run的时候不用写mvn这个参数)
附录:
如果要配置jenkins,其他的参数配置不变,只需要修改maven的命令
这里是发布product环境
</div> <div class="article_social"> <div class="article_like"><div class="circle circle-like" id="my_zan" data_id="Y3aquyE"> </div>
0 0
- maven发布时在不同的环境使用不同的配置文件
- 使用maven管理不同环境的配置文件
- maven,打包时不同环境不同的配置文件
- 使用 MAVEN 进行不同环境的服务配置文件管理
- Maven根据不同的环境打包配置文件
- maven根据不同的运行环境,打包不同的配置文件
- Maven根据不同个环境打包, 获取不同的配置文件
- Maven根据不同个环境打包, 获取不同的配置文件
- 利用maven的resources、filter和profile实现不同环境使用不同配置文件
- 利用maven的resources、filter和profile实现不同环境使用不同配置文件
- 利用maven的resources、filter和profile实现不同环境使用不同配置文件
- 利用maven的resources、filter和profile实现不同环境使用不同配置文件
- 利用maven的resources、filter和profile实现不同环境使用不同配置文件(POM中)
- 不同环境(配置文件)下的项目发布
- 用maven filter 管理不同环境的配置文件
- 用 maven filter 管理不同环境的配置文件
- maven项目用filter管理不同环境的配置文件
- 利用maven按环境打包SpringBoot的不同配置文件
- 线索化二叉树的构造及遍历
- 守护进程(精灵进程)&调用fork一次和两次的区别
- Convolutional Sequence to Sequence Learning
- Bitlocker 参数错误导致打不开移动硬盘的解决方法
- django用户注册
- maven发布时在不同的环境使用不同的配置文件
- CString,string,char*之间的转换(转)
- 欢迎使用CSDN-markdown编辑器
- Java 多线程
- java从入门到弃坑第十三天0A0
- 统计学习方法概论
- ES6 -- 新的数据结构:Set和Map
- Can OGG Version 11 Be Used to Pump/Move Trails Generated from Version 9 or 10? (文档 ID 1297283.1)
- SpringMVC预处理方法参数HandlerMethodArgumentResolver类