Skip to main content
 首页 » 编程设计

jakarta-ee之使用 jetty-maven-plugin 时过滤 web.xml

2025年05月04日181Renyi-Fan

原帖:
我要过滤web.xml运行在 Jetty 8.1 servlet 容器上的 Java 6 Web 应用程序的部署描述符,但它目前不起作用。我想根据事件的 Maven 配置文件设置不同的 JSF 项目阶段:

<context-param> 
    <param-name>javax.faces.PROJECT_STAGE</param-name> 
    <param-value>${jsfProjectStage}</param-value> 
</context-param> 
pom.xml 中的配置文件部分看起来像这样:
<profiles> 
    <profile> 
        <id>development</id> 
        <activation> 
            <activeByDefault>true</activeByDefault> 
        </activation> 
        <properties> 
            <jsfProjectStage>Development</jsfProjectStage> 
        </properties> 
    </profile> 
    <profile> 
        <id>production</id> 
        <properties> 
            <jsfProjectStage>Production</jsfProjectStage> 
        </properties> 
    </profile> 
</profiles> 

在互联网上,您可以找到几种方法来实现这一点,例如使用替代 web.xml 文件。但对我来说,最明显的方法似乎是使用 maven-war-plugin:
<build> 
    <finalName>...</finalName> 
    <plugins> 
        <plugin> 
            <groupId>org.apache.maven.plugins</groupId> 
            <artifactId>maven-war-plugin</artifactId> 
            <version>2.3</version> 
            <configuration> 
                <webResources> 
                    <webResource> 
                        <directory>src/main/webapp/WEB-INF</directory> 
                        <filtering>true</filtering> 
                        <targetPath>WEB-INF</targetPath> 
                        <includes> 
                            <include>web.xml</include> 
                        </includes> 
                    </webResource> 
                </webResources> 
            </configuration> 
        </plugin> 
 
        ... 
 
    </plugins> 
</build> 

由于您在此代码段中找到了许多答案或文章,我想知道为什么它对我不起作用。我试图更换 <webResource><resource> (经常这样发现)我也尝试添加 <filteringDeploymentDescriptors>true</filteringDeploymentDescriptors>但没有任何效果。

有谁知道这里缺少什么才能正确过滤 web.xml?还是我必须明确指定 <filters> ?

谢谢
塞巴斯蒂安

更新:
感谢 user944849 我现在知道我没有过滤的原因 web.xml不是 maven-war-plugin 而是 jetty-maven-plugin,因为我使用 mvn jetty:run运行(未组装的)webapp。有谁知道怎么带jetty-maven-plugin来过滤 web.xml在运行未组装的 webapp 之前?

请您参考如下方法:

在确定问题不是 maven-war-plugin 而是 jetty-maven-plugin 之后,我只需要从 mvn clean jetty:run 更改 maven 命令。至 mvn clean jetty:run-war为了在部署描述符也被过滤的嵌入式码头上运行组装的 web 应用程序。

谢谢你们的帮助
塞巴斯蒂安