1. 程式人生 > >通過maven-war-plugin插件對war包分環境打包

通過maven-war-plugin插件對war包分環境打包

hub env version 生產環境 classes 打包 技術 覆蓋 div

針對多環節,從源頭打包入手,當然這些都可以在運維階段用腳本進行替換來代替

resources/environment/下有四個環境,local本地、dev開發、test測試、pre預上線、prod生產,打包命令如下:

    # 本地
    mvn clean package -P local
    # 開發
    mvn clean package -P dev
    # 測試
    mvn clean package -P test
    # 預上線
    mvn clean package -P pre
    # 生產
    mvn clean package -p prod

說明:每個環境的文件夾下的配置文件可以全量放,也可以試增量,最終會覆蓋

項目目錄如下所示:

技術分享圖片

部分POM如下說是:

    <profiles>
        <!-- 本地環境 -->
        <profile>
            <id>local</id>
            <properties>
                <package.environment>local</package.environment>
            </properties>
            <!-- 是否默認 -->
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <!-- 開發環境 -->
        <profile>
            <id>dev</id>
            <properties>
                <package.environment>dev</package.environment>
            </properties>
        </profile>
        <profile>
            <!-- 測試環境 -->
            <id>test</id>
            <properties>
                <package.environment>test</package.environment>
            </properties>
        </profile>
        <profile>
            <!-- 預上線 -->
            <id>pre</id>
            <properties>
                <package.environment>pre</package.environment>
            </properties>
        </profile>
        <profile>
            <!-- 生產環境 -->
            <id>prod</id>
            <properties>
                <package.environment>prod</package.environment>
            </properties>
        </profile>
    </profiles>
<build> <finalName>ssm-framework</finalName> <plugins> <!-- war包打包組件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin
</artifactId> <version>${maven-war-plugin.version}</version> <configuration> <webResources> <resource> <!-- 元配置文件的目錄,相對於pom.xml文件的路徑 --> <directory>src/main/webapp/WEB-INF</directory> <!-- 是否過濾文件,也就是是否啟動auto-config的功能 --> <filtering>true</filtering> <!-- 目標路徑 --> <targetPath>WEB-INF</targetPath> </resource> <resource> <directory>src/main/resources/environment/${package.environment}</directory> <targetPath>WEB-INF/classes</targetPath> <filtering>true</filtering> </resource> </webResources> </configuration> </plugin>

說明:標紅部分

示例工程:https://github.com/easonjim/ssm-framework

通過maven-war-plugin插件對war包分環境打包