1. 程式人生 > >Maven War包 POM配置檔案 設定最佳實踐

Maven War包 POM配置檔案 設定最佳實踐

如何為你的Web程式(war包設定配置檔案)

約定

上面連結說了:

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>resource2</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 覆蓋上面這個目錄,將線下配置覆蓋成線上配置,共同配置保留下來。

如何完成 以上操作呢?

<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會幫我們覆蓋掉。