Maven利用profile打不同環境的包

解決方法1

  • profile配置:
    <profiles>
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <env>dev</env>
            </properties>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <env>test</env>
            </properties>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <env>prod</env>
            </properties>
        </profile>
    </profiles>
  • resource配置:
    <resources>
        <resource>
            <directory>src/main/resources</directory>
        </resource>
        <resource>
            <directory>src/main/resources.${env}</directory>
        </resource>
    </resources>
  • 工程目錄結構:
    工程目錄結構
  • war包目錄結構:
    war包目錄結構

解決方法2

  • profile配置:
    <profiles>
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <build>
                <filters>
                    <filter>src/main/resources/config/db-dev.properties</filter>
                </filters>
            </build>
        </profile>
        <profile>
            <id>test</id>
            <build>
                <filters>
                    <filter>src/main/resources/config/db-dev.properties</filter>
                </filters>
            </build>
        </profile>
        <profile>
            <id>prod</id>
            <build>
                <filters>
                    <filter>src/main/resources/config/db-prod.properties</filter>
                </filters>
            </build>
        </profile>
    </profiles>
  • 原理:
    filtering功能主要用來替換項目中的資源文件(.xml、.properties)當中的${...},比如${jdbc.username},那麼如果配置了jdbc.username=root,在項目編譯的時候,就會自動的把${jdbc.username}替換爲root。

解決方法3

  • profile配置:
    <profiles>
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
          	<properties>
                <packaging.excludes>**/logback.xml</packaging.excludes>
            </properties>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <packaging.excludes>**/logback-test.xml</packaging.excludes>
            </properties>
        </profile>
    </profiles>
  • maven-war-plugin配置:
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>${maven-war-plugin.version}</version>
        <configuration>
            <packagingExcludes>${packaging.excludes}</packagingExcludes>
        </configuration>
    </plugin>
  • 原理
    maven-war-plugin插件warSourceExcludespackagingExcludes參數:

warSourceExcludes:The comma separated list of tokens to exclude when copying the content of the warSourceDirectory.

解釋:warSourceDirectory是maven工程的src\main\webapp目錄,warSourceExcludes參數決定不將哪些src\main\webapp目錄下的文件copy到webappDirectory(一般是工程的target\${warName}目錄)。

packagingExcludes:The comma separated list of tokens to exclude from the WAR before packaging. This option may be used to implement the skinny WAR use case. Note that you can use the Java Regular Expressions engine to include and exclude specific pattern using the expression %regex[]. Hint: read the about (?!Pattern).

解釋:在生成webappDirectory(一般是工程的target\${warName}目錄)後,要將該目錄打包成**.war後綴的壓縮文件,packagingExcludes參數決定不將哪些webappDirectory**目錄中的文件打到war包中。

兩個參數是在不同階段起作用的,需要注意的是warSourceExcludes只能排除src\main\webapp目錄下的文件。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章