信步漫谈之Maven——多环境部署切换配置


1 目标

打包时根据指定的环境切换所使用的项目配置文件。

2 程序结构

  • 项目结构
com.alfred.maven.multienvswitch-webapp
  ∟ config
      ∟ local
          ∟ config.properties
      ∟ dev
          ∟ config.properties
      ∟ release
          ∟ config.properties
  ∟ src
      ∟ main
          ∟ webapp
              ∟ WEB-INF
                  ∟ config
                      ∟ config.properties
                  ∟ web.xml
          ∟ index.jsp
  ∟ pom.xml

3 Maven 的 POM 脚本

<?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.alfred.maven</groupId>
    <artifactId>maven-multienv-switch-webapp</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <description>
        概述:【Maven技术研究】多环境切换配置
        目标:打包时根据指定的环境切换所使用的项目配置文件
        多环境目录说明:
        1)local:本地环境配置,用于本地运行开发
        2)dev:开发环境配置,用于内部测试打包
        3)release:发布环境配置,用于发布版本打包
    </description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <profiles>
        <profile>
            <id>local</id>
            <activation>
                <!-- 默认使用的环境 -->
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <package.env>local</package.env>
                <package.suffix></package.suffix>
            </properties>
        </profile>
        <profile>
            <id>dev</id>
            <properties>
                <package.env>dev</package.env>
                <!-- 定义的打包文件后缀 -->
                <package.suffix>_dev</package.suffix>
            </properties>
        </profile>
        <profile>
            <id>release</id>
            <properties>
                <package.env>release</package.env>
                <package.suffix></package.suffix>
            </properties>
        </profile>
    </profiles>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-clean-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <id>auto-clean</id>
                        <phase>initialize</phase>
                        <goals>
                            <goal>clean</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <webappDirectory>${project.build.directory}/${project.artifactId}</webappDirectory>
                    <warName>MultiEnvSwitch${package.suffix}</warName>
                    <includeEmptyDirectories>true</includeEmptyDirectories>
                    <webResources>
                        <resource>
                            <directory>config/${package.env}</directory>
                            <filtering>true</filtering>
                            <targetPath>WEB-INF/config</targetPath>
                        </resource>
                    </webResources>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

4 打包结构

MultiEnvSwitch.war
  ∟ META-INF
  ∟ WEB-INF
      ∟ classes
      ∟ config
          ∟ config.properties
      ∟ web.xml
  ∟ index.jsp

5 环境切换

根据不同的maven打包指令可切换不同的配置文件进行打包,带上 maven 参数 -P 加指定 profile 的id 即可。

打包开发环境命令:mvn package -Pdev
打包发布环境命令:mvn package -Prelease
原文地址:https://www.cnblogs.com/alfredinchange/p/14073285.html