使用jetty-maven-plugin时过滤web.xml?

时间:2013-06-18 12:29:29

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

原帖:
我想过滤在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>

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

有没有人有想法,为了正确过滤web.xml,这里缺少什么?或者我是否必须明确指定<filters>

感谢
塞巴斯蒂安

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

3 个答案:

答案 0 :(得分:5)

在发现问题不是maven-war-plugin而是jetty-maven-plugin之后,我只需要将maven命令从mvn clean jetty:run更改为mvn clean jetty:run-war以便运行在嵌入式jetty上组装webapp,其中部署描述符也被过滤。

感谢您的帮助 塞巴斯蒂安

答案 1 :(得分:0)

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

例如,我的应用程序类似于:

<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}的引用,该值在2个不同的文件filter-dev.propertiesfilter-qa.properties中设置。我在父POM中有2个不同的maven配置文件(称为devqa),我在其中相应地定义了target.environment属性。

运行指定个人资料的mvn package时,bm.js中的宏将替换为正确的值。

我不是100%确定这必须与web.xml一起使用,但你可以尝试一下。

答案 2 :(得分:0)

我的2美分:

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

  1. 保留默认的webapp资源过滤,并添加我们自己的自定义过滤
  2. 将jetty配置为自定义docroot(和备用web.xml)
  3. 捆绑码头准备包装(maven 2.2.1+)阶段作为实际包装前的最后阶段
  4. 完成后,可以简单地运行maven并在命令行中提供属性,st。喜欢:mvn -Djetty=run -Djetty.webcontext=any-web-context -Djetty.port=8180 prepare-package
  5. 下面的配置是针对maven2的,但是修改了新的org.eclipse.jetty&amp; 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>