Maven War plugin提供了overlays功能,overlays提供了多個web應用程序共享資源的途徑。通過overlays,可以通過包依賴實現個性化定製開發,而不是直接在已有成熟軟件的源碼下進行個性化定製開發。
overlays 樣例
假設當前正在構建的項目documentedproject有如下結構
|-- pom.xml
`-- src
`-- main
|-- java
| `-- com
| `-- example
| `-- projects
| `-- SampleAction.java
|-- resources
| |-- images
| | `-- sampleimage.jpg
| `-- sampleresource
`-- webapp
|-- WEB-INF
| `-- web.xml
|-- index.jsp
`-- jsp
`-- websource.jsp
該項目依賴於另一個war包documentedprojectdependency-1.0-SNAPSHOT.war
...
<dependencies>
<dependency>
<groupId>com.example.projects</groupId>
<artifactId>documentedprojectdependency</artifactId>
<version>1.0-SNAPSHOT</version>
<type>war</type>
<scope>runtime</scope>
</dependency>
...
</dependencies>
...
該war包依賴有如下結構
documentedprojectdependency-1.0-SNAPSHOT.war
|-- META-INF
| |-- MANIFEST.MF
| `-- maven
| `-- com.example.projects
| `-- documentedprojectdependency
| |-- pom.properties
| `-- pom.xml
|-- WEB-INF
| |-- classes
| | |-- com
| | | `-- example
| | | `-- projects
| | | `-- SampleActionDependency.class
| | `-- images
| | `-- sampleimage-dependency.jpg
| `-- web.xml
`-- index-dependency.jsp
當前構建的項目documentedproject最後生成的war包將會有如下結構
|-- META-INF
| |-- MANIFEST.MF
| `-- maven
| `-- com.example.projects
| `-- documentedproject
| |-- pom.properties
| `-- pom.xml
|-- WEB-INF
| |-- classes
| | |-- com
| | | `-- example
| | | `-- projects
| | | |-- SampleAction.class
| | | `-- SampleActionDependency.class
| | `-- images
| | |-- sampleimage-dependency.jpg
| | `-- sampleimage.jpg
| `-- web.xml
|-- index-dependency.jsp
|-- index.jsp
`-- jsp
`-- websource.jsp
web.xml來源於當前構建項目documentedproject。
overlays 配置
war plugin提供了一些overlays的配置項用於自定義,常用的include
/exclude
選項可以用於配置吸納/排除包中指定的文件。
如下的配置從documentedprojectdependency.war overlay中排除了sampleimage-dependency.jpg
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<overlays>
<overlay>
<groupId>com.example.projects</groupId>
<artifactId>documentedprojectdependency</artifactId>
<excludes>
<exclude>WEB-INF/classes/images/sampleimage-dependency.jpg</exclude>
</excludes>
</overlay>
</overlays>
</configuration>
</plugin>
</plugins>
</build>
...
overlays 打包
overlays打包過程遵循first-win策略,如果一個文件已經從一個overlay中得到拷貝,接下來的overlay中的同名文件將不會再被拷貝。overlays按它們在<overlays>
中聲明的順序被應用,如果沒有該配置,則按照依賴聲明的順序被應用,該方法具有不可靠性,尤其是在有傳遞性依賴存在的情況。在混合有配置和無配置的情況下,無配置的overlays在有配置的overlays之後被應用。
默認情況下,當前構建的項目的資源在任何其他overlay之前被應用,當前構建項目被作爲一個特殊的overlay被對待,其沒有groudId和artifactId。如果需要改變這個默認,可以配置當前構建項目在其他overlay之後被應用。
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<overlays>
<overlay>
<groupId>com.example.projects</groupId>
<artifactId>my-webapp</artifactId>
</overlay>
<overlay>
<!-- empty groupId/artifactId represents the current build -->
</overlay>
</overlays>
</configuration>
</plugin>
</plugins>
</build>
...
overlays可以結合不同的include
/exclude
被配置多次以提供更加細粒度的控制。
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<overlays>
<overlay>
<id>my-webapp-index.jsp</id>
<groupId>com.example.projects</groupId>
<artifactId>my-webapp</artifactId>
<includes>
<include>index.jsp</include>
</includes>
</overlay>
<overlay>
<!-- empty groupId/artifactId represents the current build -->
</overlay>
<!-- Other overlays here if necessary -->
<overlay>
<id>my-webapp</id>
<groupId>com.example.projects</groupId>
<artifactId>my-webapp</artifactId>
</overlay>
</overlays>
</configuration>
</plugin>
</plugins>
</build>
...
my-webapp包中index.jsp
文件首先被配置優先於當前構建項目被使用,而同一個war包中的其他文件則在當前構建項目之後被應用。