【转载: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来动态切换配置信息的更多相关文章

  1. Maven中的pom.xml配置文件详解

    原文:http://blog.csdn.net/u012152619/article/details/51485297 <project xmlns="http://maven.apa ...

  2. maven中的pom.xml解析

    pom.xml用于项目描述,组织管理,依赖管理和构件信息的管理. <project>是pom.xml的一些约束信息: <modelVersion>指定了当前pom的版本: 坐标 ...

  3. maven中的pom.xml中的scope的作用

    pom.xml配置文件中, <dependency>中的<scope>,它主要管理依赖的生效范围.目前<scope>可以使用5个值: * compile,缺省值,适 ...

  4. Maven快速入门(四)Maven中的pom.xml文件详解

    上一章,我们讲了Maven的坐标和仓库的概念,介绍了Maven是怎么通过坐标找到依赖的jar包的.同时也介绍了Maven的中央仓库.本地仓库.私服等概念及其作用.这些东西都是Maven最基本.最核心的 ...

  5. Maven中的pom.xml详解

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  6. hibernate4+spring4+struts2的Maven中的pom.xml文件的配置

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  7. 创建maven项目时pom.xml报错的解决方法

    创建maven项目时pom.xml时: 出现如下报错信息: Failure to transfer commons-lang:commons-lang:jar:2.1 from https://rep ...

  8. 【转】maven核心,pom.xml详解

    感谢如下博主: http://www.cnblogs.com/qq78292959/p/3711501.html maven核心,pom.xml详解 什么是pom?    pom作为项目对象模型.通过 ...

  9. Maven的配置文件pom.xml

    Maven的配置文件pom.xml 简介: 什么是POM? POM是项目对象模型(Project Object Model)的简称,它是Maven项目中的文件,使用XML表示,名称叫做pom.xml. ...

随机推荐

  1. maven 控制台 打包

    maven打包方法1.打开cmd,进入到项目的根目录2.执行命令:mvn clean package等待结束.结束后到目录的target子目录中找jar文件即可

  2. 记自己利用hexo和github搭建个人博客的过程

    --------------------------------------可能我书写的方式跟别人顺序不一样,但这是我的成功经验------------------------------------ ...

  3. U盘启动安装Centos 7

    ,1.先下载CentOS iso文件,http://centos.ustc.edu.cn/centos/7.6.1810/isos/x86_64/,注意大小是4G左右,之前不知道从哪里下载的iso文件 ...

  4. PWM_MOTOR_B

    port_cfg.h witti: #define PORT_CONFIG_PIN_E0_USAGE                        PORT_CONFIG_GPIO_OUT magna ...

  5. Exp4 恶意代码分析 20164303 景圣

    Exp4 恶意代码分析 实验内容 实验点一:系统运行监控 (1)使用如计划任务,每隔一分钟记录自己的电脑有哪些程序在联网,连接的外部IP是哪里.运行一段时间并分析该文件,综述一下分析结果.目标就是找出 ...

  6. ThreadLocal的意义和实现

    可以想像,如果一个对象的可变的变量被多个线程访问时,必然是不安全的. 在单线程应用可能会维持一个全局的数据库连接,并在程序启动时初始化这个连接对象,从而避免在调用每个方法时都传递一个Connectio ...

  7. Guitar Pro中如何添加与删除音轨

    Guitar Pro是一款专业的吉他打谱作曲软件,适合每一位热爱吉他并想进一步学习的大家.今天,我们一起来看看Guitar Pro软件写谱时音轨如何添加与删除. Guitar Pro能够同时支持虚拟音 ...

  8. Discuz3.2与Java 项目整合单点登陆

    JAVA WEB项目与Discuz 论坛整合的详细步骤完全版目前未有看到,最近遇到有人在问,想到这个整个不是一时半会也解释不清楚.便把整个整合过程以及后续碰到的问题解决方案写下,以供参考. 原理 Di ...

  9. 【HNOI 2016】网络

    Problem Description 一个简单的网络系统可以被描述成一棵无根树.每个节点为一个服务器.连接服务器与服务器的数据线则看做一条树边.两个服务器进行数据的交互时,数据会经过连接这两个服务器 ...

  10. Redhat终端中文乱码解决

    文件中的中文以及命令反馈的中文能够正常显示,但是在终端中用ls等命令查看文件时会出现乱码. 我在i18n文件中加了下面两行内容(本来只有第一行),后来就能正常显示了.