使用Maven Jetty插件时如何过滤资源?

发布于 2024-12-05 06:47:15 字数 1746 浏览 1 评论 0原文

我有一个 XML 文件 (urlrewrite.xml),需要解析属性占位符。我启用 Maven 过滤来实现此目的。这对于组装的 WAR 文件效果很好。

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>

问题是当尝试使用 maven-jetty-plugin (Maven Jetty 插件)在开发模式下运行应用程序时,如 maven jetty:run 。

有问题的文件 urlrewrite.xml 位于 src/main/resources 目录中,因此应该(并且确实)最终位于 /WEB-INF/classes (或 maven jetty:run 的 target/classes )中。

URLRewriteFilter 配置指定配置文件的位置,如下所示:

<filter>
    <filter-name>UrlRewriteFilter</filter-name>
    <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
    <init-param>
        <param-name>confPath</param-name>
        <param-value>/WEB-INF/classes/urlrewrite.xml</param-value>
    </init-param>
</filter>

这将在部署时起作用。但是,使用 jetty maven 插件,URLRewrite 将因 NullPointerException 而终止,因为它使用 context.getResourceAsString("/WEB-INF/classes/urlrewrite.xml") 来加载配置文件。 Jetty 为此返回 null,因为当从工作区运行应用程序时,它将 /WEB-INF/classes/... 解析为 src/main/webapp/WEB-INF/... 。该文件不存在,因为 WAR 尚未组装。相反,它应该从 target/classes/urlrewrite.xml 中提取资源。

如果这对您来说很模糊,那么您可能无法回答这个问题,因为我怀疑您需要成为 Jetty 专家才能找出解决方法(提示:这是一个挑战!)。

有谁知道解决这个问题的方法吗?我还尝试了以下解决方法来了解其有效性:

  1. 将 urlrewrite.xml 放在新目录 src/main/webResources 下并将其添加到 maven war 插件中并启用过滤。当 WAR 打包时,这会将其内容复制到适当的位置,但不会使其可用于 jetty:run
  2. 其他一些我什至不记得的黑客......(如果我记得的话会更新)

总之,maven-jetty -plugin 需要文件位于 src/main/resources/webapp/插入路径和文件名 下,以便可用于 maven jetty:run 命令...

感谢您的帮助...

真诚, 劳埃德·福斯

I have an XML file (urlrewrite.xml) that needs a property placeholder resolved. I enable Maven filtering to achieve this. This works fine for the assembled WAR file.

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>

The problem is when trying to run the application in development mode using the maven-jetty-plugin (Maven Jetty Plugin), as maven jetty:run .

The file in question, urlrewrite.xml, is located in the src/main/resources directory, and therefore should (and does) ends up in /WEB-INF/classes (or target/classes for maven jetty:run).

The URLRewriteFilter config specifies the location of the config file as follows:

<filter>
    <filter-name>UrlRewriteFilter</filter-name>
    <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
    <init-param>
        <param-name>confPath</param-name>
        <param-value>/WEB-INF/classes/urlrewrite.xml</param-value>
    </init-param>
</filter>

This will work at deployment time. However, Using the jetty maven plugin, URLRewrite will die with a NullPointerException because it uses context.getResourceAsString("/WEB-INF/classes/urlrewrite.xml") in order to load the config file. Jetty returns null for this because when running the application from workspace it resolves /WEB-INF/classes/... to src/main/webapp/WEB-INF/... . The file does not exist there because the WAR has not yet been assembled. It should instead pull the resource from target/classes/urlrewrite.xml.

If that is obscure to you, then you probably won't be able to answer this question because I suspect you will need to be a Jetty guru to figure out a workaround (hint: that's a challenge!).

Does anyone know a way around this? I have also tried the following workarounds to know avail:

  1. Put urlrewrite.xml under a new directory, src/main/webResources and add it to the maven war plugin <webReources> and enable filtering. That will copy it's contents in the appropriate location when the WAR is packaged, but will not make it available for jetty:run
  2. Some other hacks I can't even remember ... (will update if I do)

In summary, maven-jetty-plugin needs the file to be under src/main/resources/webapp/insert path and filename in order to be available for the maven jetty:run command ...

Thanks for you help ...

Sincerely,
Lloyd Force

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

雪若未夕 2024-12-12 06:47:15

回答了我自己的问题。

  1. 将 maven-jetty-plugin 至少升级到 6.1.12

  2. 请参阅此 wiki 页面上的“配置多个 WebApp 源目录”(自 jetty 起可用) -6.1.12.rc2 和 jetty-7.0.0pre3)

  3. 向 pom.xml 添加一些魔法:

首先,添加一个新目录 (src/main/webResources)为您过滤的网络资源添加一个; element:

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

这会将文件复制到 target/jettyFilteredResources (我们稍后将引用它)。该目录不会被复制到您打包的 WAR 文件中,它仅适用于 jetty!

将以下元素添加到您的 maven-war-plugin中element:

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

这将确保所有内容都打包为您真正的 WAR 文件。

最后,通过将以下代码片段添加到您的中,告诉 jetty 使用您专门为其复制的资源。 element:

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

现在一切都会正常了! (好吧,从技术上讲,我还没有测试过生产 WAR,但是……等等……它也应该可以工作)。

如果有人有更好的答案,只要在合理的时间内(比如 1 天)提供答案,我就会接受。

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).

萌︼了一个春 2024-12-12 06:47:15

我认为另一个问题的答案更好:

使用jetty时运行资源过滤器:run< /a>

基本上,您必须使用“mvn jetty:run-exploded”,而不是运行“mvn jetty:run”。

唯一的缺点是它需要构建 WAR 文件,这在某些情况下可能会很昂贵。如果这对你来说不是问题,那么我想这更好。

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.

望她远 2024-12-12 06:47:15

将其添加到 pom.xml:

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

这就是嵌入式 Jetty 服务器的样子:

        <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!感谢@les2 的灵感;-)

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 ;-)

陌若浮生 2024-12-12 06:47:15

我找到了另一种方式。

构建项目并将目标文件夹添加为额外的类路径。

<webAppConfig>
    ....
    <extraClasspath>${basedir}/target/mywebapp</extraClasspath>
    ....
</webAppConfig>

I found another way.

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

<webAppConfig>
    ....
    <extraClasspath>${basedir}/target/mywebapp</extraClasspath>
    ....
</webAppConfig>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文