【问题标题】:How to modify the contents or replace a file which resides in webapp directory of a Maven webapp project如何修改内容或替换驻留在 Maven webapp 项目的 webapp 目录中的文件
【发布时间】: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-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>

【问题讨论】:

  • 为什么要将web-prod.xml重命名为web.xml
  • web-prod.xml 用于生产部署。因此,我希望在战争包装之前将其作为实际的web.xml 提供。
  • 它们之间的区别到底在哪里?除了具有环境依赖项之外,还应该在您的工件(war 文件)中......应该从外部应用......Tomcat?
  • 它有一些 CORS 策略配置,这些配置因环境而异。此外,我们使用maven-cargo-pluginCargo Daemon,它会自动下载并启动Tomcat 服务器并将工件部署到其中。所以我们不能手动配置Tomcat在外部拥有web.xml
  • 我重复我的问题:你的产品和通常的 web.xml 有什么区别......?什么样的信息有区别?在生产或开发中使用 cargo 插件?

标签: maven maven-war-plugin maven-resources-plugin


【解决方案1】:

感谢 eis 将我指向filtering feature
早些时候,我维护了两个不同的 web.xml 文件,并试图用另一个替换一个,因为这两个文件之间存在大约 40 行块的差异。 我可以通过修改相同的结果来实现最终结果,而 NOT 通过将其替换为另一个来实现。这是逐步解释的解决方案:

  1. 拥有一个带有占位符的 web.xml 文件以替换为块。
  2. maven-war-plugin 启用资源过滤并使用配置文件。
  3. 有两个不同的属性文件来存储占位符的目标特定值。

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>
        @web.xml.cors.config@
    </filter>
    ...
</web-app>

pom.xml

<build>
    ...
    <filters>
        <filter>src/main/resources/place-holders-${targetenv}.properties</filter>
    </filters>
    <plugins>
        
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
                ...
                <webResources>
                    ...
                    <resource>
                        <directory>${basedir}/src/main/webapp/WEB-INF</directory>
                        <filtering>true</filtering>
                        <targetPath>WEB-INF</targetPath>
                        <includes>
                            <include>**/web.xml</include>
                        </includes>
                    </resource>
                </webResources>
            </configuration>
        </plugin>
    </plugins>
</build>
<profiles>
    <profile>
        <id>dev</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <targetenv>dev</targetenv>
        </properties>
    </profile>
    <profile>
        <id>prod</id>
        <properties>
            <targetenv>prod</targetenv>
        </properties>
    </profile>
</profiles>

place-holders-dev.properties [只是一个空白/空白空间用于开发]

web.xml.cors.config= 

place-holders-prod.properties

web.xml.cors.config=<init-param>\
\n\t\t\t<description>A comma separated list of allowed origins. Note An '*' cannot be used for an allowed origin when using credentials.</description>\
\n\t\t\t<param-name>cors.enabled</param-name>\
\n\t\t\t<param-value>true</param-value>\
\n\t\t</init-param>\
\n\t\t<init-param>\
        ...
\n\t\t</init-param>\
\n\t\t<init-param>\
        ...
\n\t\t</init-param>\
        ...
        ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-26
    • 2016-04-11
    • 1970-01-01
    • 2013-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多