Maven中基于POM.xml的Profile来动态切换配置信息
【转载:https://blog.csdn.net/blueheart20/article/details/52838093】
1. Maven中的profile设置
Maven是目前主流的项目代码结构管理工具和打包发布工具,在其中提供了profile方式,可以将不同的环境下的信息,基于profile来进行管理,所有的配置信息放入profile之内;
大家可以把profile当作一套环境下的独立一套配置来理解。
示例如下, pom.xml中的配置部分内容:
<project>
<!-- 省略其他部分内容 --->
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<ab.key>testkey</ab.key>
</properties> <build>
<filters>
<filter>src/main/resources/config/db.properties</filter>
<filter>src/main/resources/config/test.xml</filter>
</filters>
</build>
</profile> <profile>
<id>test</id>
<properties>
<ab.key>anothertestkey</ab.key>
</properties> <build>
<filters>
<filter>src/main/resources/config/db.properties</filter>
<filter>src/main/resources/config/test.xml</filter>
</filters>
</build>
</profile> </profiles>
</project>
这里使用了ab.key来表示不同环境下的配置信息,大家可以看到不同配置环境下的拥有相同key的值是各不相同的, testkey和anothertestkey.
<activation>标签下的内容标识为在基于mvn的命令操作情况下,默认的profile指定,在命令行如果没有指定profile,则默认使用activeByDefault中设定的profile值。
2. Profile实现配置信息动态过滤的依赖包
主要的plugins如下所示:
<project>
<!--这里省略其他部分的内容 -->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.1</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins> <resources>
<resource>
<directory>src/main/resources</directory>
<!-- <includes>
<include>**/*</include>
</includes> -->
<filtering>true</filtering>
</resource>
</resources>
</build>
maven-surefire-plugin, 可选组件, 插件用来在maven构建生命周期的test phase执行一个应用的单元测试。它会产生两种不同形式的测试结果报告。
使用方式: 使用该插件很简单,使用mvn surefire:test或者mvn test都可以运行工程下的单元测试。
结果信息: 在工程的${basedir}/target/surefire-reports,目录下(basedir指的是pom文件所在的目录)
主页地址: http://maven.apache.org/components/plugins/maven-surefire-plugin/
maven-resources-plugin: 必选组件, 基于profile指定动态过滤配置信息
使用方式: mvn package -P profileName (or mvn deploy etc)
结果信息: 将src/main/resources下的配置信息占位符替换为profile中指定的值
主页地址: http://maven.apache.org/components/plugins/maven-resources-plugin/
关于resources节点下的信息作用,这里是整个resources的总开关,filtering这里指定启用过滤功能,否则该功能将无法正常使用。 <directory>节点设定了扫描的目录,includes节点主要是从整体的角度设定具体的扫描目录或者文件;如果这里不指定的话,可以在各个profile之中指定具体需要扫描的配置文件,在build->filters->filter中指定特定的配置文件, 具体信息可以参照#1中的pom.xml示例。
#1中的resources filter设置如下:
............
<profile>
<id>test</id>
<properties>
<ab.key>anothertestkey</ab.key>
</properties> <build>
<filters>
<filter>src/main/resources/config/db.properties</filter>
<filter>src/main/resources/config/test.xml</filter>
</filters>
</build>
</profile>
...............
3. profile文件使用示例
3.1 创建maven项目
打开Eclipse, 打开File--> New--> Maven Project, 创建maven项目:
填写相应的项目groupId和artifactId信息:
点击Finish按钮之后,创建项目成功
3.2 项目结构以及相应的配置文件信息
这里我们将配置文件test.xml和db.properties放入了src/main/resources目录下的config中。
同时将maven的plugins按照#1的要求配置进入pom.xml文件中。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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>org.test</groupId>
<artifactId>mymaven</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>mymaven</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.1</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins> <resources>
<resource>
<directory>src/main/resources</directory>
<!-- <includes>
<include>**/*</include>
</includes> -->
<filtering>true</filtering>
</resource>
</resources>
</build> <profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<ab.key>testkey</ab.key>
</properties> <build>
<filters>
<filter>src/main/resources/config/db.properties</filter>
<filter>src/main/resources/config/test.xml</filter>
</filters>
</build>
</profile> <profile>
<id>test</id>
<properties>
<ab.key>anothertestkey</ab.key>
</properties> <build>
<filters>
<filter>src/main/resources/config/db.properties</filter>
<filter>src/main/resources/config/test.xml</filter>
</filters>
</build>
</profile>
</profiles>
</project>
配置文件中的信息如下, test.xml中的内容:
<?xml version="1.0" encoding="UTF-8"?>
<info>${ab.key}</info>
db.properties中的内容如下:
my.key2=${ab.key}
ab.key在pom.xml中的不同profile中设定的相应值。
在pom.xml中设定了2个profile, test和dev, dev做为缺省的profile。
4. 执行打包或者发布操作,查看打包结果
>> mvn clean # 清理上次打包的结果和临时文件
>> mvn package -P dev -Dmaven.test.skip=true # 打包,忽略测试部分
然后接入生成的target目录,查看classes目录下的config, 查看db.properties和test.xml中的内容:
maven的命令运行过程信息:
然后我们就可以在打包之后的结果中,看到过滤之后的信息了。
在Maven中-DskipTests和-Dmaven.test.skip=true的区别如下:
- -DskipTests,不执行测试用例,但编译测试用例类生成相应的class文件至target/test-classes下。
- -Dmaven.test.skip=true,不执行测试用例,也不编译测试用例类。
5. 在过程中碰到的问题以及解决办法
Q1: 在打包过程中,发现配置信息,并未被正确的profile下的信息替换掉
How to fix it?
a. 检查maven中的resources plugin是否被正确的引入, 在全局的filtering设置是否被打开,在build节点中的directory目录设置是否正确。另外,在特定的profile中设置的filter路径以及文件是否正确等
Q2: 在打包过程中,配置文件被正确过滤替换了,但是配置文件不是被复制到特定的目录,比如config下,而是被放入了classes下的根目录了,为什么?
How to fix it ?
此类情况应是build-->resources下设置了includes信息,include了所有的文件或者include文件路径为缺省值,比如下面的设置:
.....................
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
这种情况下,将includes节点去掉,在profile中进行配置filter即可。
Q3: 我的配置都是没有问题,但是依然发现配置文件中的占位符,没有被正确替换,问题在哪里?
How to fix it ?
请检查maven-resources-plugin是否被正确的引入到plugins的列表中。
Maven中基于POM.xml的Profile来动态切换配置信息的更多相关文章
- Maven中的pom.xml配置文件详解
原文:http://blog.csdn.net/u012152619/article/details/51485297 <project xmlns="http://maven.apa ...
- maven中的pom.xml解析
pom.xml用于项目描述,组织管理,依赖管理和构件信息的管理. <project>是pom.xml的一些约束信息: <modelVersion>指定了当前pom的版本: 坐标 ...
- maven中的pom.xml中的scope的作用
pom.xml配置文件中, <dependency>中的<scope>,它主要管理依赖的生效范围.目前<scope>可以使用5个值: * compile,缺省值,适 ...
- Maven快速入门(四)Maven中的pom.xml文件详解
上一章,我们讲了Maven的坐标和仓库的概念,介绍了Maven是怎么通过坐标找到依赖的jar包的.同时也介绍了Maven的中央仓库.本地仓库.私服等概念及其作用.这些东西都是Maven最基本.最核心的 ...
- Maven中的pom.xml详解
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- hibernate4+spring4+struts2的Maven中的pom.xml文件的配置
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- 创建maven项目时pom.xml报错的解决方法
创建maven项目时pom.xml时: 出现如下报错信息: Failure to transfer commons-lang:commons-lang:jar:2.1 from https://rep ...
- 【转】maven核心,pom.xml详解
感谢如下博主: http://www.cnblogs.com/qq78292959/p/3711501.html maven核心,pom.xml详解 什么是pom? pom作为项目对象模型.通过 ...
- Maven的配置文件pom.xml
Maven的配置文件pom.xml 简介: 什么是POM? POM是项目对象模型(Project Object Model)的简称,它是Maven项目中的文件,使用XML表示,名称叫做pom.xml. ...
随机推荐
- 7zip 18.3性能测试
配置 7zip 18.3 bate3 的性能大幅提升,进行一点简单测试 配置为 E3 1230v2 3.5G 4核心/8线程 12G内存 均为8线程测试 压缩解压一个1.5G的虚拟机磁盘镜像 测试 再 ...
- Flask-Migrate
终于到了Flask-Migrate,之前在看Flask-SQLAlchemy的时候, Flask支持 makemigration / migrate 吗? 答案在这里该诉你,如果你同时拥有两个三方组件 ...
- JavaScript 常用内置对象(字符串属性、Math对象、Array数组对象)
1.字符串属性 <script> var test_var = "I Iove you"; console.log(test_var.charAt(3)) // ...
- storm-sql-kafka问题情况
首先上官方文档:http://storm.apache.org/releases/1.2.2/storm-sql.html 解决的问题 1.kafka版本不对 开始测试时采用storm1.2.2+ka ...
- vue配置404页面
{ path:'*', name:"/404", component:cuowu } path星号表示没有这个路由 name表示去这个地址 component这个页面引入的时候叫的 ...
- 如何在Idea提交代码到Github上
一,配置账户 1. Setting >> Version Control >> git,配置git的安装目录(一般默认识别),其他参数不变 2.配置GitHub账户,输入Git ...
- 重写用户模型时报错AttributeError: type object ‘自定义类’ has no attribute ‘USERNAME_FIELD’
view中导入:from django.contrib.auth.models import AbstractBaseUser settings.py中设置了:AUTH_USER_MODEL='app ...
- linux查看与修改交换内存配置(解决zabbix-agent启动报错)
问题 zabbix-agent在一台centos6.5上启动报错: cannot allocate shared memory of size 949056: [28] No space left o ...
- [Python数据挖掘]第8章、中医证型关联规则挖掘
一.背景和挖掘目标 二.分析方法与过程 1.数据获取 2.数据预处理 1.筛选有效问卷(根据表8-6的标准) 共发放1253份问卷,其中有效问卷数为930 2.属性规约 3.数据变换 ''' 聚类 ...
- spring整合dubbo[单机版]
Spring整合Dubbo,这个是用xml配置的 (方式一) 来梳理下步骤: 1. 安装zookeeper,在进行简单配置[这里使用单机模式,不用集群] 2. 创建maven项目,构建项目结构 3. ...