【发布时间】:2021-09-23 04:55:40
【问题描述】:
我有一个 Maven webapp 项目。它的默认目录结构如下 -
|-- pom.xml
`-- src
`-- main
|-- java
| `-- com
| `-- example
| `-- projects
| `-- SampleAction.java
|-- resources
| `-- images
| `-- sampleimage.jpg
`-- webapp
|-- WEB-INF
| `-- web.xml
|-- index.jsp
`-- jsp
`-- websource.jsp
它生成的 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
|-- index.jsp
`-- jsp
`-- websource.jsp
我想在打包到 WAR 之前将 web-prod.xml 重命名为 web.xml,它位于 webapp 目录下。
现在要注意的是 - 此目录的内容仅在打包阶段由 maven-war-plugin 复制到 target/<finalName> 中,并立即生成 WAR。
由于 webapp 内容默认被maven-war-plugin 复制,所以早期出现的其他插件如maven-compiler-plugin 和maven-resources-plugin 对修改上述文件没有任何作用。
pom.xml
<build>
...
<plugins>
...
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>replace-descriptor</id>
<phase>compile</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<delete file="${project.build.directory}/${project.finalName}/WEB-INF/web.xml" />
<move file="${project.build.directory}/${project.finalName}/WEB-INF/web-prod.xml" tofile="${project.build.directory}/${project.finalName}/WEB-INF/web.xml"/>
</target>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<archiveClasses>true</archiveClasses>
<attachClasses>true</attachClasses>
<warSourceIncludes>WEB-INF/**</warSourceIncludes>
<packagingExcludes>WEB-INF/classes</packagingExcludes>
<webResources>
<resource>
<directory>src/main/resources</directory>
<targetPath>WEB-INF/classes</targetPath>
<includes>
...
</includes>
</resource>
<resource>
<directory>${project.build.directory}/classes</directory>
<includes>
...
</includes>
<targetPath>WEB-INF/classes</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
web.xml
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" metadata-complete="true" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>NSWeb</display-name>
...
<filter>
<filter-name>corsFilter</filter-name>
<filter-class>com.example.core.security.filter.CORSFilter</filter-class>
</filter>
...
</web-app>
web-prod.xml
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" metadata-complete="true" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>NSWeb</display-name>
...
<filter>
<filter-name>corsFilter</filter-name>
<filter-class>com.example.core.security.filter.CORSFilter</filter-class>
<init-param>
<description>A comma separated list of allowed origins. Note: An '*' cannot be used for an allowed origin when using credentials.</description>
<param-name>cors.enabled</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
...
</init-param>
<init-param>
...
</init-param>
<init-param>
...
</init-param>
...
</filter>
...
</web-app>
【问题讨论】:
-
为什么要将
web-prod.xml重命名为web.xml? -
web-prod.xml用于生产部署。因此,我希望在战争包装之前将其作为实际的web.xml提供。 -
它们之间的区别到底在哪里?除了具有环境依赖项之外,还应该在您的工件(war 文件)中......应该从外部应用......Tomcat?
-
它有一些 CORS 策略配置,这些配置因环境而异。此外,我们使用
maven-cargo-plugin和Cargo Daemon,它会自动下载并启动Tomcat 服务器并将工件部署到其中。所以我们不能手动配置Tomcat在外部拥有web.xml。 -
我重复我的问题:你的产品和通常的 web.xml 有什么区别......?什么样的信息有区别?在生产或开发中使用 cargo 插件?
标签: maven maven-war-plugin maven-resources-plugin