【问题标题】:Filtering web.xml when using the jetty-maven-plugin?使用 jetty-maven-plugin 时过滤 web.xml?
【发布时间】:2013-06-14 16:02:32
【问题描述】:

原帖:
我想过滤在 Jetty 8.1 servlet 容器上运行的 Java 6 Web 应用程序的 web.xml 部署描述符,但到目前为止它不起作用。我想根据活动的 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>

由于您找到了许多使用此代码 sn-p 的答案或文章,我想知道为什么它对我不起作用。我尝试将&lt;webResource&gt; 替换为&lt;resource&gt;(经常以这种方式找到),我还尝试添加&lt;filteringDeploymentDescriptors&gt;true&lt;/filteringDeploymentDescriptors&gt;,但没有任何效果。

有没有人知道,为了正确过滤 web.xml,这里缺少什么?还是我必须明确指定&lt;filters&gt;

谢谢
塞巴斯蒂安

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

【问题讨论】:

  • 你的maven命令是什么?特别是,您如何激活配置文件? &lt;activeByDefault&gt; 表示“只有在没有其他配置文件处于活动状态时才处于活动状态。”
  • 我在 Eclipse 中使用 Maven,并且我的 Run ConfigurationGoals 用于开发构建设置为 clean jetty:run,没有明确的 配置文件设置。生产版本的目标设置为clean installProfiles 设置为production
  • 生产版是否按照您的预期过滤 web.xml? jetty:run 不起作用,我一点也不惊讶,因为 jetty:run 执行 unassembled webapp。当 war 插件作为包阶段的一部分运行时会发生过滤,jetty:run 不使用 IIRC。您可能需要做一些研究来弄清楚如何让码头进行过滤。或者,您可能需要针对完全组装的 web 应用使用 jetty。
  • 是的,你是对的!在为生产构建时,组装的 webapp 包含一个过滤的web.xml。我只需要将 filteringDeploymentDescriptor 设置为 true 即可实现此目的。老实说,我之前没有尝试过为生产而构建。所以现在我只需要弄清楚如何为 jetty-maven-plugin 启用 web.xml 过滤。到目前为止感谢

标签: jakarta-ee maven web.xml maven-jetty-plugin


【解决方案1】:

在确定问题不是 maven-war-plugin 而是 jetty-maven-plugin 后,我只需将 maven 命令从 mvn clean jetty:run 更改为 mvn clean jetty:run-war 即可在部署描述符也被过滤的嵌入式码头。

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

【讨论】:

    【解决方案2】:

    您的配置似乎正确,但根据我的经验,您可能还需要 &lt;filters&gt; 部分。

    例如,我的应用程序有类似的东西:

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>${maven-war-plugin.version}</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                    <webResources>
                        <resource>
                            <directory>src/main/webapp</directory>
                            <filtering>true</filtering>
                            <includes>
                                <include>**/bm.js</include>
                            </includes>
                        </resource>
                    </webResources>
                    <warName>${war.name}</warName>
                </configuration>
            </plugin>
        </plugins>
        <filters>
            <filter>src/main/filters/filter-${target.environment}.properties</filter>
        </filters>
    </build>
    

    bm.js 文件包含对宏${my.property} 的引用,该值在两个不同的文件filter-dev.propertiesfilter-qa.properties 中设置。我在父 POM 中有 2 个不同的 Maven 配置文件(称为 devqa),我在其中相应地定义了 target.environment 属性。

    当运行mvn package 指定配置文件时,bm.js 中的宏将被替换为正确的值。

    我不能 100% 确定这对 web.xml 是否有效,但您可以试一试。

    【讨论】:

    • 使用额外的过滤器文件不知何故没有意义,因为新值(替换后)来自pom.xml 中的 Maven 配置文件属性变量。尽管如此,我试过了,它仍然没有过滤web.xml中的任何内容。
    【解决方案3】:

    只要我的 2 美分:

    使用 run-war 比使用 run 要差一些,因为 run-war 需要打包——不方便开发.对我有用的解决方案是遵循这些步骤(我可以想象一个更简单的步骤,但我们在 maven 和项目部署方面有几个 tweeks):

    1. 保留默认的 webapp 资源过滤并添加我们自己的自定义过滤
    2. 将码头配置指向自定义 docroot(和备用 web.xml)
    3. 将 jetty 绑定到 prepare-package (maven 2.2.1+) 阶段,作为实际打包之前的最后一个阶段
    4. 完成后,您可以简单地运行 maven 并在命令行 st 中提供属性。喜欢:mvn -Djetty=run -Djetty.webcontext=any-web-context -Djetty.port=8180 prepare-package

    下面的配置是针对 maven2 的,但是对于新的 org.eclipse.jetty & maven3 的修改应该很简单:

        <!-- jetty:run -->
        <profile>
            <id>jetty-run</id>
            <activation>
                <property>
                    <name>jetty</name>
                    <value>run</value>
                </property>
            </activation>
            <properties>
                <jetty.docroot>${project.build.directory}/jetty-docroot</jetty.docroot>
            </properties>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-resources-plugin</artifactId>
                        <version>2.4.3</version>
                        <executions>
                            <execution>
                                <id>jetty-docroot</id>
                                <phase>prepare-package</phase>
                                <goals>
                                    <goal>copy-resources</goal>
                                </goals>
                                <configuration>
                                    <outputDirectory>${jetty.docroot}</outputDirectory>
                                    <resources>
                                        <resource>
                                            <directory>${basedir}/src/main/webapp</directory>
                                            <filtering>true</filtering>
                                        </resource>
                                    </resources>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <!-- for maven2, use org.eclipse.jetty for maven3 + slightly different configuration  -->
                        <groupId>org.mortbay.jetty</groupId>
                        <artifactId>maven-jetty-plugin</artifactId>
                        <configuration>
                            <connectors>
                                <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
                                    <port>${jetty.port}</port>
                                    <maxIdleTime>60000</maxIdleTime>
                                </connector>
                            </connectors>
                            <scanIntervalSeconds>10</scanIntervalSeconds>
                            <contextPath>/${jetty.webcontext}</contextPath>
                            <webXml>${jetty.docroot}/WEB-INF/web.xml</webXml>
                            <webAppSourceDirectory>${jetty.docroot}</webAppSourceDirectory>
                            <!-- maven3
                            <webApp>
                                <contextPath>/${jetty.webcontext}</contextPath>
                                <descriptor>${jetty.docroot}/WEB-INF/web.xml</descriptor>
                                <baseResource>${jetty.docroot}</baseResource>
                            </webApp>
                            -->
                            <systemProperties>
                                <systemProperty>
                                    <name>java.awt.headless</name>
                                    <value>true</value>
                                </systemProperty>
                            </systemProperties>
                        </configuration>
                        <executions>
                            <execution>
                                <id>jetty-run</id>
                                <phase>prepare-package</phase>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    

    【讨论】:

      猜你喜欢
      • 2012-08-29
      • 1970-01-01
      • 1970-01-01
      • 2015-06-06
      • 1970-01-01
      • 2012-11-27
      • 2011-05-30
      • 1970-01-01
      • 2016-02-29
      相关资源
      最近更新 更多