Maven发布时在不同的环境使用不同的配置文件

在Maven开发时,不同的环境总会使用到不同的配置。如本地,测试,预发布,发布等环境,像数据库这些都要使用到不同的配置。如果手动改的话肯定会十分的麻烦。

还好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=convertToNull jdbc.username=devuser jdbc.password=dev123456

3.product.properties

jdbc.url=jdbc:mysql://127.0.0.1:3306/productdb?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull jdbc.username=productuser jdbc.password=product123456

4.test.properties

jdbc.url=jdbc:mysql://127.0.0.1:3306/testdb?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull jdbc.username=testuser jdbc.password=test123456

5.pom.xml

<project xmlns="" xmlns:xsi="" xsi:schemaLocation=" 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>&lt;!&ndash;一定要指向上层目录&ndash;&gt; <includes> &lt;!&ndash;要遍历出来文件都必须写&ndash;&gt; <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的命令

Maven发布时在不同的环境使用不同的配置文件

这里是发布product环境

Maven权威指南_中文完整版清晰PDF 

Maven 3.1.0 发布,项目构建工具

Linux 安装 Maven

Ubuntu 16.04 安装Maven3.3.9

Maven3.0 配置和简单使用

Ubuntu下搭建sun-jdk和Maven2

Maven使用入门

Ubuntu 下 搭建Nexus Maven私服中央仓库 

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/14209.html