使用Maven Jetty插件时如何过滤资源? [英] How to filter resources when using maven jetty plugin?
问题描述
我有一个需要解析属性占位符的XML文件(urlrewrite.xml).我启用了Maven过滤来实现此目的.对于已组装的WAR文件,此方法工作正常.
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>
问题是,当尝试使用maven-jetty-plugin(Maven Jetty插件)作为maven jetty:run在开发模式下运行应用程序时.
The problem is when trying to run the application in development mode using the maven-jetty-plugin (Maven Jetty Plugin), as maven jetty:run .
有问题的文件urlrewrite.xml位于src/main/resources目录中,因此应该(并且确实)以/WEB-INF/classes(或maven jetty:run的target/classes)结尾.
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).
URLRewriteFilter配置指定配置文件的位置,如下所示:
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>
这将在部署时起作用.但是,使用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中提取资源.
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.
如果这对您来说很晦涩,那么您可能将无法回答这个问题,因为我怀疑您需要成为Jetty专家才能找到解决方法(提示:这是一个挑战!).
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:
- 将urlrewrite.xml放在新目录src/main/webResources下,并将其添加到Maven War插件< webReources>中.并启用过滤.打包WAR时,会将其内容复制到适当的位置,但不会使其可用于码头:运行
- 我什至不记得的其他一些黑客……(如果我会更新)
总而言之,maven-jetty-plugin需要文件位于src/main/resources/webapp/插入路径和文件名下,以便可用于maven jetty:run命令..
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 ...
感谢您的帮助...
此致, 劳埃德部队
推荐答案
回答了我自己的问题.
-
将maven-jetty-plugin升级到至少6.1.12
Upgrade maven-jetty-plugin to at least 6.1.12
请参阅此Wiki页面上的配置多个Web应用程序源目录"(自jetty-6.1.12.rc2和jetty-7.0.0pre3起可用)
See this wiki page on 'Configuring Multiple WebApp Source Directory' (available since jetty-6.1.12.rc2 and jetty-7.0.0pre3)
向pom.xml添加一些魔术:
Add some magic to pom.xml:
首先,为过滤后的网络资源添加一个新目录(src/main/webResources),并添加一个< resource>元素:
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>
这会将文件复制到target/jettyFilteredResources(我们将在以后引用).此目录不会复制到打包的WAR文件中,仅用于码头!
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!
在您的maven-war-plugin< configuration>中添加以下元素元素:
Add the following element to your maven-war-plugin <configuration> element:
<webResources>
<resource>
<directory>src/main/webResources</directory>
<filtering>true</filtering>
</resource>
</webResources>
这将确保为您的真实WAR文件打包所有内容.
That will ensure everything is packaged up for your real WAR file.
最后,通过将以下代码段添加到您的< baseResource>中,告诉Jetty使用您专门为其复制的资源.元素:
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).
如果有人有更好的答案,只要在合理的时间内(例如1天)提供了答案,我就会接受.
If anyone has a better answer, I will accept it provided the answer is provided in a reasonable amount of time (say 1 day).
这篇关于使用Maven Jetty插件时如何过滤资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!