Maven依赖特定类

发布于 2025-01-05 12:23:13 字数 1318 浏览 0 评论 0原文

在下面的场景中,

我将包装一个外部 jar 文件(读取我无法控制的依赖项)并将其包装在要通过 RMI 公开的服务中。

我希望我的服务接口也导出为 Maven 依赖项,但是因为它将返回依赖项中定义的类,这意味着依赖项本身将用作我的服务接口的依赖项。

不幸的是,原始 jar 文件包含许多与我公开的服务无关的类。

是否可以仅依赖 maven 中该 jar 文件中的几个类(可能通过提取并重新打包相关的几个类)?

  • uberbig_irrelevant.jar
    • com.uberbig.beans <-- 需要这个包或其中的一些类。
    • com.uberbig.everythingElse

服务项目包含所有 uberbig jar。但公开了一个服务 BeanService,该服务有一个返回 com.uberbig.beans.IntrestingLightWeightSerialiasbleBean 实例的调用。

服务接口项目需要有一个如下所示的 bean 定义

interface BeanFetcher {
   public IntrestingLightWeightSerialiasbleBean fetchBeanById(long beanId);
}

因此,理想情况下,我的 serviceInterface jar 文件将仅包含 BeanFetcher 接口。 IntrestingLightWeightSerialasbleBean 的定义以及 IntrestingLightWeightSerialasbleBean 的任何直接依赖项。

该项目供内部使用,不会公开暴露,因此只要重新打包的 bean 定义是二进制的并且与外部 jar 文件连续兼容,重新打包就不会有问题。

有什么建议吗?

可能相关的问题Maven依赖于项目 - 没有jar但类

也许我可以使用 maven-dependency-plugin 但我还没弄清楚如何做到这一点。

In the following senario

I'm wrapping an external jar file (read a dependency I've no control over) and wrapping this in a service to be exposed over RMI.

I'd like my service interface to also be exported as a maven dependency however as it will be returning classes defined in the dependency this means that the dependency itself will be used as a dependency of my service interface.

Unfortunatly the origional jar file contains many classes that are not relevant to my exposed service.

Is it possible to depend on just a few classes in that jar file in maven (possibly by extracting and repackaging the few classes that are relevant)?

  • uberbig_irrelevant.jar
    • com.uberbig.beans <-- Need this package or a few classes in it.
    • com.uberbig.everythingElse

Service project includes all of uberbig jar. But exposes a service BeanService which has a call which returns an insance of com.uberbig.beans.IntrestingLightWeightSerialiasbleBean.

Service interface project needs to have a bean definition that looks like

interface BeanFetcher {
   public IntrestingLightWeightSerialiasbleBean fetchBeanById(long beanId);
}

So ideally my serviceInterface jar file would only include the BeanFetcher interface. The definition of IntrestingLightWeightSerialiasbleBean and any direct dependencies of IntrestingLightWeightSerialiasbleBean.

The project is for use internally and won't be publically exposed so there should be no problems repackaging so long as the repackaged bean definitions are binary and searially compatable with the external jar file.

Any Suggestions?

Possibly related question Maven depend on project - no jar but classes

Maybe I could use something from the dependency:copy section of the maven-dependency-plugin but I haven't figured out how to do that.

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

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

发布评论

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

评论(2

忘东忘西忘不掉你 2025-01-12 12:23:13

我认为你的插件是对的,但不是目标。您应该使用 dependency:unpack 代替。

您应该能够使用包含过滤器仅提取您需要的类,然后将它们重新打包到您自己的 jar 中。 (如果您在服务接口项目中执行此操作,则为服务接口 jar,但您也可以设置一个单独的项目。)

I think you got the plugin right, but not the goal. You should use dependency:unpack instead.

You should be able to use an inclusion filter to extract only the classes you need, and then repack them into your own jar. (The service interface jar if you do it in the service interface project, but you can just as well set up a separate project.)

甜扑 2025-01-12 12:23:13

创建您自己的重新打包的 jar 并将其放入本地存储库中。并希望您实际上已经确定了所有依赖关系,考虑了反射等。IMO 并不值得。

您可以使用 ProGuard 等自动执行此操作(伴随相关的风险增加)。取出未使用的类等。这也可以在您自己的工件上完成,例如,通过 jarjar/onejar/etc 制作一个一体化的 jar。

Create your own repackaged jar and put it in your local repo. And hope you've actually identified all dependencies, accounting for reflection, etc. IMO not really worth it.

You may be able to do it automatically (with the associated increased risk) by using ProGuard/etc. to pull out unused classes etc. That could be done on your own artifact as well, for example, by making an all-in-one jar via jarjar/onejar/etc.

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