How to filter resources when using maven jetty plugin?

倖福魔咒の 提交于 2019-11-28 23:57:09

Answered my own question.

  1. Upgrade maven-jetty-plugin to at least 6.1.12

  2. See this wiki page on 'Configuring Multiple WebApp Source Directory' (available since jetty-6.1.12.rc2 and jetty-7.0.0pre3)

  3. Add some magic to pom.xml:

First, add a new directory (src/main/webResources) for your filtered web resources and add a <resource> element:

        <resource>
            <directory>src/main/webResources</directory>
            <filtering>true</filtering>
            <targetPath>../jettyFilteredResources</targetPath>
        </resource>

That will copy the files to target/jettyFilteredResources (we will reference this later). This directory will NOT get copied to your packaged WAR file, it is for jetty only!

Add the following element to your maven-war-plugin <configuration> element:

                <webResources>
                    <resource>
                        <directory>src/main/webResources</directory>
                        <filtering>true</filtering>
                    </resource>
                </webResources>

That will ensure everything is packaged up for your real WAR file.

Finally, tell jetty to use the resources your copied especially for it, by added the following snippet to your <baseResource> element:

<baseResource implementation="org.mortbay.resource.ResourceCollection">                        
    <resourcesAsCSV>src/main/webapp,target/jettyFilteredResources</resourcesAsCSV>
</baseResource>

Now everything will worketh! (Well, technically I haven't tested the production WAR yet, but ... blah ... it should work too).

If anyone has a better answer, I will accept it provided the answer is provided in a reasonable amount of time (say 1 day).

dgaviola

I think the answer in this other question is better:

Running resource filters when using jetty:run

Basically, instead of running 'mvn jetty:run' you have to use 'mvn jetty:run-exploded'.

The only drawback is that it needs to build the WAR file, which might be expensive in some cases. If that's not an issue for you, then I guess it's better.

add this to pom.xml:

    <resources>
        <resource>
            <directory>src/main/webapp/WEB-INF</directory>
            <filtering>true</filtering>
            <targetPath>../jettyFilteredResources</targetPath>
        </resource>
    </resources>

and this is how embedded Jetty server should look like:

        <plugin>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-maven-plugin</artifactId>
            <version>9.1.3.v20140225</version>
            <configuration>
                <webAppConfig>
                    <descriptor>target/jettyFilteredResources/web.xml</descriptor>
                </webAppConfig>
                <scanIntervalSeconds>3</scanIntervalSeconds>
            </configuration>
        </plugin>

woila! thanks @les2 for inspiration ;-)

I found another way.

build the project and add the target folder as extra classpath.

<webAppConfig>
    ....
    <extraClasspath>${basedir}/target/mywebapp</extraClasspath>
    ....
</webAppConfig>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!