---
title: "maven pom.xml加载不同properties配置 "
author: "kalogen"
date: "2015-10-30T11:52:22Z"
tags: []
categories: ["编程语言"]
slug: "maven-pom-xml-properties"
draft: false
lastmod: "2015-10-30T11:52:22Z"
id: 2253222
url: "https://kalogen.iteye.com/blog/2253222"
---

1.pom.xml

===========================

&lt;!-- 不同的打包环境配置: test=开发/测试测试环境,  product=生产环境; 命令行方式: mvn clean install -Dmaven.test.skip=true -Ptest 或 -Pproduct--&gt;    

    &lt;profiles&gt;  
       &lt;!-- 开发/测试环境，默认激活 --&gt;  
       &lt;profile&gt;  
           **&lt;id&gt;test&lt;/id&gt;**  
           &lt;properties&gt;  
              **&lt;env&gt;test&lt;/env&gt;**  
           &lt;/properties&gt;  
           &lt;activation&gt;  
              &lt;activeByDefault&gt;true&lt;/activeByDefault&gt;&lt;!--默认启用的是dev环境配置--&gt;  
           &lt;/activation&gt;  
       &lt;/profile&gt;  
       &lt;!-- 预发布环境 --&gt;  
       &lt;profile&gt;  
           **&lt;id&gt;preproduction&lt;/id&gt;**  
           &lt;properties&gt;  
              **&lt;env&gt;preproduction&lt;/env&gt;**  
           &lt;/properties&gt;  
       &lt;/profile&gt;  
       &lt;!-- 生产环境 --&gt;  
       &lt;profile&gt;  
           **&lt;id&gt;production&lt;/id&gt;**  
           &lt;properties&gt;  
              **&lt;env&gt;production&lt;/env&gt;**  
           &lt;/properties&gt;  
       &lt;/profile&gt;  
    &lt;/profiles&gt;    
   
      &lt;build&gt;  
        &lt;filters&gt; &lt;!-- 指定使用的 filter --&gt;  
          &lt;filter&gt;src/main/filters/filter-${env}-env.properties&lt;/filter&gt;  
        &lt;/filters&gt;  
        &lt;resources&gt;  
          &lt;resource&gt; &lt;!-- 配置需要被替换的资源文件路径, db.properties 应该在 src/main/resource 目录下 --&gt;  
            &lt;directory&gt;src/main/resources&lt;/directory&gt;  
            &lt;filtering&gt;true&lt;/filtering&gt; &lt;!-- 是否使用过滤器 --&gt;  
          &lt;/resource&gt;  
        &lt;/resources&gt;

      &lt;/build&gt;

 

2.src/main/filters/下不同环境的配置文件

src/main/filters/filter-preproduction-env.properties

src/main/filters/filter-production-env.properties

src/main/filters/filter-test-env.properties

======filter-test-env.properties 举例

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

 

3.src/main/resources下要应用处理的文件

src/main/resources/conf/db.properties

======db.properties

jdbc.datasource=com.mchange.v2.c3p0.ComboPooledDataSource  
jdbc.driverClass=com.mysql.jdbc.Driver  
jdbc.url=${jdbc.url}  
jdbc.username=${jdbc.username}  
jdbc.password=${jdbc.password}

jdbc.minPoolSize=10  
jdbc.maxPoolSize=50  
jdbc.autoCommit=false

 

 

 

 

 

￥￥￥￥￥￥￥￥

 **A.pom.xml属性介绍：**

                     project: pom的xml根元素。

                     parent:  声明继承。

                     modules:  声明聚合，该值是一个当前POM的相对目录。用户项目的聚合。

                     groupId:  定义当前Maven项目隶属的实际项目。

                     artifactId:  定义实际项目中的一个Maven项目模块，一般为实际项目名称。

                     version:  定义Maven项目当前版本。

                     groupId、artifactId、version三个属性构成项目的坐标，必须全部填写。

                     packaging: 坐标元素之一，可以不填，不填默认jar，表示项目构建成jar包；值为war表示可部署的web项目。

                     name: 名称。

                     description: 项目描述。

                     organization: 所属组织。

                     licenses: 许可证。

                     mailingLists: 邮件列表。  

                     developers: 开发者。

                     contributors: 贡献者。

                     issueManagement: 问题追踪系统。

                     ciManagement: 持续集成系统。

                     scm: 版本控制系统。

                     prerequisites--&gt;maven: 要求maven最低版本，默认值为2.0.

                     build--&gt;sourceDirectory: 主源码目录。

                     build--&gt;scriptSourceDirectory: 脚本源码目录。

                     build--&gt;testSourceDirectory: 测试源码目录。

                     build--&gt;outputDirectory: 主源码输出目录。

                     build--&gt;testOutputDirectory: 测试源码输出目录。

                     build--&gt;resources--&gt;resource:  主资源目录。

                     build--&gt;testResources--&gt;testResource: 测试资源目录。

                     build--&gt;finalName: 输出主构件的名称。

                     build--&gt;directory: 输出目录。

                     build--&gt;filters--&gt;filter: 通过properties文件定义资源过滤属性。

                     build--&gt;extensions--&gt;extension: 扩展Maven的核心。

                     build--&gt;pluginManagement:  插件管理。

                     build--&gt;plugins--&gt;plugin: 插件。

                     profiles--&gt;profile: POM Profile。

                     distributionManagement--&gt;repository: 发布版本部署仓库。

                     distributionManagement--&gt;snapshotRepository: 快照版本部署仓库。

                     distributionManagement--&gt;site: 站点部署。

                     repositories--&gt;repository: 仓库。

                     pluginRepositories--&gt;pluginRepository:  插件仓库。

                     dependencies--&gt;dependency: 依赖管理。

                     properties: Maven属性。

                     reporting--&gt;plugins: 报告插件。

                    a--&gt;b表示a元素嵌套着b元素。

 

         **B.settings.xml文件配置。** 

                    settings: settings.xml的根元素。

                    localRepository: 本地仓库。

                    interactiveMode: Maven是否与用户交互，默认值为true。

                    offline: 离线模式，默认false。

                    pluginGroups--&gt;pluginGroup: 插件组。

                    servers--&gt;server: 下载与部署仓库的认证信息。

                    mirrors--&gt;mirror: 仓库镜像。

                    proxies--&gt;proxy: 代理。

                    profiles--&gt;profile: Settings Profile。

                    activeProfiles--&gt;activeProfile: 激活Profile。

                    Maven目录下的conf文件夹下的settings.xml为全局的，影响所有用户的配置。一般建议修改c:\\Users\\用户名\\.m2\\目录下的settings.xml，属于局部的，只对当前目

         录下的用户有用。