Maven采用profile方式多环境配置打包实战(多个文件)

Maven采用profile方式多环境配置打包实战(多个文件)

1656发表于2017-09-16

上篇采用filter方式实现了多环境配置,这种情况一般针对一个文件,比如db.properties,但是如果存在多个配置文件都要通过指定profile来快速切换又怎么办呢?下面我就来把我的方案分享给大家。

首先我们还是来看一下resource文件结构。

其中上面有三个环境,local,test,product,分别表示开发,测试和线上,里面有三个配置文件。

接下来我们来修改pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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.lanhusoft</groupId>
    <artifactId>icharge</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>icharge Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.7</java.version>
        <org.slf4j-version>1.7.16</org.slf4j-version>
        <logback-version>1.1.5</logback-version>
        <!-- spring版本号 -->
        <spring.version>4.3.7.RELEASE</spring.version>
        <!-- mybatis版本号 -->
        <mybatis.version>3.3.0</mybatis.version>
        <poi.version>3.9</poi.version>
    </properties>
   
    <profiles>
        <profile>
            <id>local</id>
            <properties>
                <package.environment>local</package.environment>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>product</id>
            <properties>
                <package.environment>product</package.environment>
            </properties>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <package.environment>test</package.environment>
            </properties>
        </profile>
    </profiles>
	<!-- dependency包依赖省略 -->
	<!-- dependency包依赖省略 -->
    <build>
        <finalName>lanhusoft-maven-demo</finalName>
        <sourceDirectory>src/main/java</sourceDirectory>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <excludes>
                    <exclude>local/*</exclude>
                    <exclude>product/*</exclude>
                     <exclude>test/*</exclude>
                </excludes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.1.1</version>
                <configuration>
                    <archive>
                        <addMavenDescriptor>false</addMavenDescriptor>
                    </archive>
                    <warName>${project.artifactId}</warName>
                    <webXml>src/main/webapp/WEB-INF/web.xml</webXml>
                    <warSourceDirectory>src/main/webapp</warSourceDirectory>
                    <webResources>
                        <resource>
                            <directory>src/main/resources/${package.environment}</directory>
                            <targetPath>WEB-INF/classes</targetPath>
                            <filtering>true</filtering>
                        </resource>
                    </webResources>
                </configuration>
            </plugin>
            <!-- compiler插件, 设定JDK版本 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <showWarnings>true</showWarnings>
                    <encoding>${project.build.sourceEncoding}</encoding>
                    <compilerArguments>
                        <verbose />
                        <bootclasspath>${java.home}/lib/rt.jar;${java.home}/lib/jce.jar</bootclasspath>
                      <!--  <extdirs>webapp\WEB-INF\lib</extdirs>-->
                    </compilerArguments>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

打包:

mvn clean -Plocal package

mvn clean -Ptest package

mvn clean -Pproduct package


小编蓝狐