写在前面

项目中往往有开发环境、测试环境、生产环境,这几个环境有些配置文件可能是不一样的,三个环境切换时以前基本是靠手动注释文件来打成不同的包。如果不同的配置文件过多,粗心打包后,就会导致配置文件出错,从而使项目部署出问题。例如Mysql配置文件在项目中有可能会使用如下情况,通过手动注释切换配置文件。

#本地数据库
#url:jdbc:mysql://***
#username:dev
#password:root

#线上数据库
url:jdbc:mysql://***
username:formal
password:admin

#测试数据库
#url:jdbc:mysql://**
#username:test
#password:test
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

为了解决这个问题,可以使用Maven不同的打包命令生成不同的Jar包

  • 开发环境打包命令
mvn clean install -P dev
  • 1.
  • 测试环境打包命令
mvn clean install -P test
  • 1.
  • 生产环境打包命令
mvn clean install -P formal
  • 1.
创建普通Maven项目

项目结构如下图所示

使用Maven自动打包配置文件生成开发环境,测试环境,生产环境_配置文件

System.properties 配置文件

app.mode=${app.mode}

app-develop.properties 开发配置文件

app.mode=develop

app-formal.properties 生产配置文件

app.mode=formal

app.test.properties 测试配置文件

app.mode=test

以上配置文件中,System.properties为项目打包后真正使用的文件,其它三个分别存放各自配置文件,当打包时,Maven会将对应的参数填充到System.properties中
注意事项
项目中使用的配置参数,必需在System.properties 中有,并通过${参数名}进行引用

POM 配置文件
<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>com.zhaochao</groupId>
    <artifactId>TestDemo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>TestDemo</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>


    <dependencies>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.21</version>
        </dependency>
    </dependencies>

    <profiles>
        <profile>
            <id>test</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <build.profile.id>test</build.profile.id>
            </properties>
        </profile>
        <profile>
            <id>develop</id>
            <properties>
                <build.profile.id>develop</build.profile.id>
            </properties>
        </profile>
        <profile>
            <id>formal</id>
            <properties>
                <build.profile.id>formal</build.profile.id>
            </properties>
        </profile>
    </profiles>

    <build>
        <filters>
            <filter>src/app-${build.profile.id}.properties</filter>
        </filters>
        <sourceDirectory>src/main/java</sourceDirectory>
        <!-- 定义打包资源文件-->
        <resources>
            <resource>
                <filtering>true</filtering>
                <!--加上filter会过滤该资源路径中的文件-->
                <directory>${project.basedir}/src/main/resources</directory>
            </resource>
        </resources>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.4.2</version>
                <configuration>
                    <createDependencyReducedPom>true</createDependencyReducedPom>
                    <filters>
                        <filter>
                            <artifact>*:*</artifact>
                            <excludes>
                                <exclude>META-INF/*.SF</exclude>
                                <exclude>META-INF/*.DSA</exclude>
                                <exclude>META-INF/*.RSA</exclude>
                            </excludes>
                        </filter>
                    </filters>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.zhaochao.Main</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>
</project>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
Main测试函数
public class Main {
    public Properties properties;

    public String loadProperties(String key) throws IOException {
        if (properties == null) {
            properties = new Properties();
            properties.load(this.getClass().getClassLoader().getResourceAsStream("System.properties"));
        }
        return properties.getProperty(key);
    }

    public static void main(String[] args) throws IOException {
        Main main = new Main();
        System.out.println(main.loadProperties("app.mode"));
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
测试结果

在Idea中,勾选Maven Projects中->Profiles->develop运行main则是使用开发环境的配置,如下图所示

使用Maven自动打包配置文件生成开发环境,测试环境,生产环境_maven_02

使用Maven自动打包配置文件生成开发环境,测试环境,生产环境_apache_03

使用Maven自动打包配置文件生成开发环境,测试环境,生产环境_配置文件_04

也可以通过以下命令生成对就的Jar包

mvn clean install -P dev 
mvn clean install -P test
mvn clean install -P formal
  • 1.
  • 2.
  • 3.

使用Maven自动打包配置文件生成开发环境,测试环境,生产环境_apache_05