如何修改内容或替换驻留在 Maven webapp 项目的 webapp 目录中的文件

问题描述

我有一个 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 目录下。 现在问题是 - 此目录的内容仅在打包阶段被 target/<finalName> 复制到 maven-war-plugin 中,并立即生成 WAR。
由于 webapp 的内容默认由 maven-war-plugin 复制,其他早期出现的插件如 maven-compiler-pluginmaven-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>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)