Running resource filters when using jetty:run

2019-03-19 16:47发布

问题:

I'm using resource filtering on jsps, based on profiles. I'm also developing locally using mvn jetty:run, but the filtering phase does not run.

How can I perform filtering using the jetty plugin?


Configuration snippets:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.0.2</version>
<configuration>
    <webResources>
        <resource>
            <directory>src/main/webapp</directory>
            <includes>
                <include>error.jsp</include>
            </includes>
            <filtering>true</filtering>
            <targetPath>/</targetPath>
        </resource>
    </webResources>
</configuration>
</plugin>

<profile>
    <id>jci</id>
    <activation>
        <activeByDefault>true</activeByDefault>
        <property>
            <name>jci</name>
        </property>
    </activation>
    <properties>
        <error.title>Some value here</error.title>
    </properties>
</profile>  

回答1:

You may want to use the jetty:run-exploded goal rather than jetty:run. From the documentation:

This goal first assembles your webapp into an exploded war file and then deploys it to Jetty.

This may ensure that the appropriate war lifecycle phases are executed before the server is started.

Also are you sure the jci profile is being activated? if another profile is specified for the build, the <activeByDefault> property won't enable the profile, see this bug for details.

From John Casey's response:

The above example is working as designed. The <activeByDefault/> element is meant to specify that this profile will be activated if no other profiles are active in the build. Therefore, specific activation of any profile will cause this one to be deactivated.



回答2:

The filtered files usually end up in the build target-directory. If you run 'mvn jetty:run' it uses per default your unfiltered src/main/webapp directory. All you got to do is to add the build-target as additional resource directory. Done so, jetty will create an overlay and will also use the filtered files.

                <plugin>
                    <groupId>org.mortbay.jetty</groupId>
                    <artifactId>maven-jetty-plugin</artifactId>
                    <version>6.1.26</version>
                    <configuration>
                        <webAppConfig>
                            <contextPath>/${project.build.finalName}</contextPath>
                            <baseResource implementation="org.mortbay.resource.ResourceCollection">
                                <resourcesAsCSV>src/main/webapp,${project.build.directory}/${project.build.finalName}</resourcesAsCSV>
                            </baseResource>
                        </webAppConfig>
                        <scanIntervalSeconds>2</scanIntervalSeconds>
                    </configuration>
                </plugin>


回答3:

Thanks to @Randy I managed to get this working too. Heres an up to date example showing both the resources filtering and the jetty rebasing baseResource using org.eclipse.jetty rather than the older mortbay. Here we are filtering two jsp pages login.jsp and index.jsp and setting a variable "${login.resources}" in the jsp to "login.res.jsp" as per the properties section below. Note we filter and write these to "jetty.docroot", then we overlay jetty.docroot over src/main/webapps so our filtered jsps get used by jetty. The overlay is updated from @Randy to use the newer "org.eclipse.jetty.util.resource.ResourceCollection" implementation.

<profiles>
<profile>
    <id>jetty</id>

    <properties>
        <jetty.docroot>${project.build.directory}/jetty</jetty.docroot>
        <login.resources>login.res.jsp</login.resources>
    </properties>

    <build>

        <plugins>

            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.0.2</version>
                <executions>
                    <execution>
                        <id>jetty-docroot</id>
                        <!-- test-compile precedes jetty:run -->
                        <phase>test-compile</phase>

                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${jetty.docroot}</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>${basedir}/src/main/webapp</directory>
                                    <filtering>true</filtering>
                                    <includes>
                                        <include>**/login.jsp</include>
                                        <include>**/index.jsp</include>
                                    </includes>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>

                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>9.4.0.v20161208</version>
                <configuration>

                    <scanIntervalSeconds>2</scanIntervalSeconds>

                    <webApp>
                        <contextPath>/intamerge</contextPath>
                         <baseResource implementation="org.eclipse.jetty.util.resource.ResourceCollection">

                             <resourcesAsCSV>${jetty.docroot},${basedir}/src/main/webapp</resourcesAsCSV>
                            </baseResource>
                        <baseAppFirst>false</baseAppFirst>
                    </webApp>


                </configuration>
            </plugin>
        </plugins>
    </build>
</profile>



标签: maven-2 Jetty