Maven 阴影插件的变压器来处理 java 反射
Maven 阴影插件会忽略字符串中的类/包名称,这对于 Java 反射来说是典型的。 如果您尝试“隐藏”的库使用 Class.forName("originalpackagename...."),则隐藏的依赖项将不起作用,因为原始包名称将保持不变。 同样的故事,代码如下
servlet.addServletWithMapping("org.mortbay.jetty.servlet.DefaultServlet",
URIUtil.SLASH);
,让事情变得更糟:(
private String[] _serverClasses = {
"-org.mortbay.jetty.plus.annotation.", // don't hide
"-org.mortbay.jetty.plus.jaas.", // don't hide
"-org.mortbay.jetty.plus.naming.", // don't hide
"-org.mortbay.jetty.plus.jaas.", // don't hide
"-org.mortbay.jetty.servlet.DefaultServlet", // don't hide
"org.mortbay.jetty.",
"org.slf4j."
};
从 Jetty 服务器源代码复制的示例)
也许有一些 ResourceTransformer 已经可用于 Maven Shade,它替换了字符串源代码中的原始包/类名称?..
Maven shade plugin ignores class/package names in Strings, which is typical for Java reflection.
If the library you're trying to "shade" uses Class.forName("originalpackagename...."), the shaded dependency won't work because the original package name will be left unchanged.
same story with code like
servlet.addServletWithMapping("org.mortbay.jetty.servlet.DefaultServlet",
URIUtil.SLASH);
and to make things worse:
private String[] _serverClasses = {
"-org.mortbay.jetty.plus.annotation.", // don't hide
"-org.mortbay.jetty.plus.jaas.", // don't hide
"-org.mortbay.jetty.plus.naming.", // don't hide
"-org.mortbay.jetty.plus.jaas.", // don't hide
"-org.mortbay.jetty.servlet.DefaultServlet", // don't hide
"org.mortbay.jetty.",
"org.slf4j."
};
(examples copied from Jetty server source code)
maybe there is some ResourceTransformer already available for Maven Shade, which replaces the original package/class names in the strings source code?..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这很可能是自您提出问题以来的新行为(我使用的是 2.3 版),但阴影插件似乎会自动处理 Java 源文件中的包名称文字。
但是,对于资源文件来说,则不然。例如,如果您尝试遮蔽 log4j 包,然后从 log4j.properties 引用记录器实现类,则会在运行时出现异常。我没有看到任何专门针对阴影插件的内容,但我使用第三方 maven-replacer-plugin 解决了这个问题。例如:
Most likely this is new behavior since when you asked your question (I'm using version 2.3), but the shade plugin appears to automatically handle package name literals in the Java source files.
However, for resource files, it does not. For example, if you try to shade the log4j packages, and then reference a logger implementation class from log4j.properties, you will have an exception at runtime. I don't see anything specifically for the shade plugin, but I fixed this issue using the third-party maven-replacer-plugin. For example: