从 META-INF 中过滤掉服务提供商文件
我的应用程序需要从命令行和 Web 应用程序运行。我选择实现这一点的方法是将整个应用程序放在一个 jar 文件中(即我的应用程序特定的类与我的应用程序使用的 jar 中的类共存)。这使得命令行用例变得简单,因为用户只需键入 java -jar JARNAME 即可。对于 Web 应用程序用例,我只需将 jar 包含在 WEB-INF/lib
中,一切就差不多了。
我遇到的问题是,我放入单个 jar 中的一些 jar 在 META-INF/services
中为同一服务定义了提供程序,因此单个 jar 最终在 < code>META-INF/services 具有相同的名称。 (出于好奇,这些是 Jersey jar,服务是 javax.ws.rs.ext.MessageBodyReader 和 javax.ws.rs.ext.MessageBodyWriter。)我试图防止 MessageBody* 服务文件被吞入我的 jar 文件中。以下是我尝试(但失败)实现这一目标的方法:
<jar destfile="build/jammies.jar">
<archives>
<zips>
<restrict>
<fileset dir="lib">
<include name="*.jar"/>
<exclude name="servlet-api.jar"/>
</fileset>
<rsel:not>
<rsel:name regex="META-INF/services/javax.ws.rs.ext.*"/>
</rsel:not>
</restrict>
</zips>
</archives>
</jar>
我确实在 build.xml
顶部定义了 rsel
命名空间。
<project basedir="." default="compile"
xmlns:rsel="antlib:org.apache.tools.ant.types.resources.selectors">
所以我不明白为什么 restrict
任务没有过滤掉那些特定的服务提供程序文件。
My application needs to run both from the command-line and a web app. The way I've chosen to implement this is to have the entire application in a single jar file (i.e. my application-specific classes coexist with the classes from the jars my app uses). This makes the command-line use case simple, as the user only has to type java -jar JARNAME
. For the web app use case, I simply include the jar in WEB-INF/lib
and all is well, almost.
The problem I have is that a few of the jars I'm slurping into the single jar define providers in META-INF/services
for the same service, so the single jar ends up with multiple entries in META-INF/services
with the same name. (For the curious, these are Jersey jars, and the services are javax.ws.rs.ext.MessageBodyReader
and javax.ws.rs.ext.MessageBodyWriter
.) So I'm trying to prevent the MessageBody* service files from being slurped into my jar file. Here's how I'm trying (and failing) to accomplish that:
<jar destfile="build/jammies.jar">
<archives>
<zips>
<restrict>
<fileset dir="lib">
<include name="*.jar"/>
<exclude name="servlet-api.jar"/>
</fileset>
<rsel:not>
<rsel:name regex="META-INF/services/javax.ws.rs.ext.*"/>
</rsel:not>
</restrict>
</zips>
</archives>
</jar>
I do have the rsel
namespace defined at the top of build.xml
.
<project basedir="." default="compile"
xmlns:rsel="antlib:org.apache.tools.ant.types.resources.selectors">
So I don't understand why the restrict
task isn't filtering out those particular service provider files.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这是因为正则表达式从一开始就匹配文件名字符串,但是没有文件以 META-INF 开头,它们将以 ${basedir}/WEB-INF/lib/META-INF 开头...
I think its because the regex starts matching the filename strings from the start however no file begins with META-INF, they would begin with ${basedir}/WEB-INF/lib/META-INF...