覆盖用于跨多个 Web 应用程序共享公共资源。WAR 项目的依赖项收集在WEB-INF/lib中,但覆盖在 WAR 项目本身上的 WAR 工件除外。
为了演示,给定项目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 ,它在项目的pom.xml中声明为依赖项:
...
<dependencies>
<dependency>
<groupId>com.example.projects</groupId>
<artifactId>documentedprojectdependency</artifactId>
<version>1.0-SNAPSHOT</version>
<type>war</type>
<scope>runtime</scope>
</dependency>
...
</dependencies>
...记录的projectdependency 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
由此产生的 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。
在以前版本的 WAR 插件中,不需要进行任何配置。如果您对默认设置感到满意,情况仍然如此。但是,如果您需要更多控制,请继续阅读!
< overlay >元素可以有以下子元素:
例如,要排除上面记录的项目依赖战争覆盖的sampleimage-dependency.jpg :
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</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>配置中定义的顺序应用。如果未提供配置,则使用在 POM 中定义依赖项的顺序(警告:这不是确定性的,尤其是当您将覆盖作为传递依赖项时)。在混合情况的情况下(例如配置的覆盖和未配置的覆盖),未配置的覆盖在配置的覆盖之后应用。
默认情况下,首先添加项目的源(也就是当前构建)(例如,在应用任何覆盖之前)。当前构建被定义为没有groupId和artifactId的特殊覆盖。如果需要首先应用覆盖,只需在这些覆盖之后配置当前构建。
例如,如果com.example.projects组中的my-webapp是项目的依赖项,需要在项目自己的源之前应用,请执行以下操作:
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</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>
...注意:在上述场景中,任何其他 WAR 依赖项都将在当前构建之后应用,因为它们尚未在<overlays>元素中配置。
为了执行更细粒度的覆盖策略,可以使用不同的包含/排除多次打包覆盖。例如,如果必须在 webapp 中设置叠加层my-webapp的index.jsp文件,但可以通过常规方式控制其他文件,则为my-webapp定义两个叠加层配置:
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</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>
...可以全局指定以下设置并修改所有覆盖的应用方式。
...
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<dependentWarIncludes>**/IncludeME,**/images</dependentWarIncludes>
</configuration>
</plugin>
</plugins>
... ...
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<dependentWarExcludes>WEB-INF/web.xml,index.*</dependentWarExcludes>
</configuration>
</plugin>
</plugins>
... ...
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<!-- default value is target/war/work -->
<workDirectory>/tmp/extract_here</workDirectory>
</configuration>
</plugin>
</plugins>
... ...
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<useCache>true</useCache>
</configuration>
</plugin>
</plugins>
...要将zip依赖项用作覆盖,您必须在插件的配置中显式配置它。例如,要在 webapp 的脚本目录中注入 zip 覆盖的内容,请执行以下操作:
...
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<overlays>
<overlay>
<groupId>zipGroupId</groupId>
<artifactId>zipArtifactId</artifactId>
<type>zip</type>
<targetPath>scripts</targetPath>
</overlay>
</overlays>
</configuration>
</plugin>
</plugins>
...