---
title: "maven多环境配置打包"
author: "王亚龙"
date: "2017-06-08T20:42:51Z"
tags: []
categories: ["编程语言"]
slug: "maven"
draft: false
lastmod: "2017-06-08T20:43:38Z"
id: 2378634
url: "https://memoryforwyl.iteye.com/blog/2378634"
---

在开发过程中，我们的软件会面对不同的运行环境，比如开发环境、测试环境、生产环境，而我们的软件在不同的环境中，有的配置可能会不一样，比如数据源配置、日志文件配置、以及一些软件运行过程中的基本配置，那每次我们将软件部署到不同的环境时，都需要修改相应的配置文件，这样来回修改，是个很麻烦的事情。有没有一种方法能够让我们不用修改配置就能发布到不同的环境中呢？当然有，这就是接下来要做的事。

      当然，这里的前提是使用maven做为构建工具。

      使用maven来实现多环境的构建可移植性，需要借助maven提供的profile功能，通过不同的环境激活不同的profile来达到构建的可移植性。

 

1、在src/main/resources/filters，建三个文件：dev.properties、test.properties和prod.properties。 

分别放置开发环境、测试环境和生产环境的配置。（假设文件中都设置了jdbc.url属性） 

![](http://dl2.iteye.com/upload/attachment/0125/3482/1b736db5-b4f8-39b4-aa0e-a54dc60ad79d.png)  
   
2、新建src/main/resources/conf.properties文件。里面设置 

```
jdbc.url=${jdbc.url}
```

 

 

3、配置pom.xml。配置如下 

```
    <profiles>
        <profile>
            <!-- 开发环境 -->
            <id>dev</id>
            <properties>
                <env>dev</env>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>

        <profile>
            <!-- 测试环境 -->
            <id>test</id>
            <properties>
                <env>test</env>
            </properties>
        </profile>

        <profile>
            <!-- 生产环境 -->
            <id>prod</id>
            <properties>
                <env>prod</env>
            </properties>
        </profile>
    </profiles>

    <build>
        <filters>
            <filter>src/main/resources/filters/${env}.properties</filter>
        </filters>
        
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                 <filtering>true</filtering>

                 <!-- optional -->
                 <includes>
                     <include>**/*.*</include>
                 </includes>
                 <excludes>
                     <exclude>filters/</exclude>
                 </excludes>
            </resource>
        </resources>
        
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.0</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.0.2</version>
                <configuration>
                    <encoding>${java.encoding}</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
```

 

4、打包。

 

```
开发环境包
mvn clean install -Denv=dev
mvn clean install

测试环境包
mvn clean install -Denv=test
mvn clean install -Ptest

成产环境包
mvn clean install -Denv=prod
mvn clean install -Pprod
```