Maven War包 POM配置文件


如何為你的Web程序(war包設定配置文件)

約定

http://maven.apache.org/plugins/maven-war-plugin/examples/adding-filtering-webresources.html

上面鏈接說了:

The default resource directory for all Maven projects is src/main/resources which will end up in target/classes and in WEB-INF/classes in the WAR. The directory structure will be preserved in the process.

The WAR Plugin is also capable of including resources not found in the default resource directory through the webResources parameter.

默認是找 src/main/resource 目錄的配置作為War包的配置,如果沒找到,則可以使用用戶自定義的,如下面,用戶定義了目錄 resource2 作為自定義目錄。

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <webResources>
            <resource>
              <!-- this is relative to the pom.xml directory -->
              <directory>src/main/resource</directory>
            </resource>
          </webResources>
        </configuration>
      </plugin>
    </plugins>
  </build>
  ...
</project>

例如,如果你的項目目錄如下:

 .
 |-- pom.xml
 |-- resource2
 |   |-- external-resource.jpg
 |   `-- image2
 |       `-- external-resource2.jpg
 `-- src
     `-- main
         |-- java
         |   `-- com
         |       `-- example
         |           `-- projects
         |               `-- SampleAction.java
         |-- resources
         |   `-- images
         |       `-- sampleimage.jpg
         `-- webapp
             |-- WEB-INF
             |   `-- web.xml
             |-- index.jsp
             `-- jsp
                 `-- websource.jsp

那么,執行上面的POM之后,WAR包內容應該如下:

documentedproject-1.0-SNAPSHOT.war
 |-- META-INF
 |   |-- MANIFEST.MF
 |   `-- maven
 |       `-- com.example.projects
 |           `-- documentedproject
 |               |-- pom.properties
 |               `-- pom.xml
 |-- WEB-INF
 |   |-- classes
 |   |   |-- com
 |   |   |   `-- example
 |   |   |       `-- projects
 |   |   |           `-- SampleAction.class
 |   |   `-- images
 |   |       `-- sampleimage.jpg
 |   `-- web.xml
 |-- external-resource.jpg
 |-- image2
 |   `-- external-resource2.jpg
 |-- index.jsp
 `-- jsp
     `-- websource.jsp

大家可以看到,這個WAR包不僅有 src/main/resource 目錄下的配置,還有 resource2下的配置。

說明,WAR的配置,來自兩個地方,一個是默認的,一個是用戶添加的。

如何覆蓋?

我們最常見的用戶場景是:

src/main/resource 目錄下有開發的所有配置

src/main/online-resource 目錄下有線上配置。它不包含所有配置。它只包含 src/main/resource 目錄下那些需要被替換成線上配置的配置文件。也就是 開發和線上配置 的公同配置文件 是不會存在這個目錄下的。

那么,我們的想法就很簡單了,線上WAR的配置應該包括:

  • 首先,應該包括 src/main/resource 目錄下所有文件
  • 然后,使用 src/main/online-resource 覆蓋上面這個目錄,將線下配置覆蓋成線上配置,共同配置保留下來。

如何完成 以上操作呢?

https://github.com/knightliao/disconf/blob/master/disconf-web/pom.xml 這里提供了一個最佳實踐:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <configuration>
        <webResources>

            <resource>
                <directory>${project.build.online.sourceDir}</directory>
                <targetPath>WEB-INF/classes</targetPath>
                <includes>
                    <include>**/*.*</include>
                </includes>
            </resource>

        </webResources>
    </configuration>
</plugin>

只需要像上面這樣配置,Maven幫我們默認執行了第一步(首先,應該包括 src/main/resource 目錄下所有文件),我們自定義了第二步,Maven會幫我們覆蓋掉。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM