简体   繁体   English

JDK9 自动模块和“拆分包”依赖项

[英]JDK9 Automatic Modules and "Split Packages" Dependencies

I am converting a java project to use modules.我正在转换一个 java 项目以使用模块。 One of my modules depends on hamcrest library, and it needs both of the jar files hamcrest.core and hamcrest.library .我的一个模块依赖于hamcrest库,它需要两个 jar 文件hamcrest.corehamcrest.library These two jar files both have org.hamcrest package.这两个 jar 文件都有org.hamcrest包。 So when my module wants to treat these two jars as automatic modules, it fails because two modules in a module-path cannot have packages with same name.因此,当我的模块想要将这两个 jar 视为自动模块时,它会失败,因为模块路径中的两个模块不能有同名的包。 I searched a lot and found some related stuff on stackoverflow.我搜索了很多,在stackoverflow上找到了一些相关的东西。 It seems that I have two reasonable options if I do not want to rebuild those dependencies:如果我不想重建这些依赖项,我似乎有两个合理的选择:

  1. Merge the two jars into one jar using my build automation tool (which is maven).使用我的构建自动化工具(maven)将两个 jar 合并为一个 jar。
  2. Somehow tell java compiler that these two jars should be treated as a single automatic module.以某种方式告诉 java 编译器,这两个 jar 应该被视为单个自动模块。

So here are my questions:所以这里是我的问题:

  • Is any of these two options possible?这两个选项中的任何一个都可能吗? If yes, how?如果是,如何?
  • Is there any better option?有没有更好的选择?

Thanks in advance提前致谢

Ok I finally managed to solve it like this:好的,我终于设法解决了这个问题:

  1. Create a new maven module called hamcrest-all and add dependencies on hamcrest-core and hamcrest-library .创建一个名为hamcrest-all的新 maven 模块,并添加对hamcrest-corehamcrest-library依赖。
  2. add maven-assembly-plugin to this module with appendAssemblyId set to false.maven-assembly-plugin添加到此模块, appendAssemblyId设置为 false。
  3. remove the dependency to hamcrest-core and hamcrest-library from other maven modules and instead add dependency to hamcrest-all .从其他 maven 模块中删除对hamcrest-corehamcrest-library依赖,而是添加对hamcrest-all依赖。
  4. exclude hamcrest-core and hamcrest-library when including dependency to hamcrest-all .当包含对hamcrest-all依赖时,排除hamcrest-corehamcrest-library

What it actually does is that it unpacks hamcrest-core and hamcrest-library in the jar file created for hamrest-all .它的实际作用是将hamcrest-corehamcrest-library解压到为hamrest-all创建的 jar 文件中。 And because each jar file is treated as one module by JMPS, the problem is gone :)并且因为每个 jar 文件都被 JMPS 视为一个模块,所以问题消失了:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM