简体   繁体   中英

JDK9 Automatic Modules and "Split Packages" Dependencies

I am converting a java project to use modules. One of my modules depends on hamcrest library, and it needs both of the jar files hamcrest.core and hamcrest.library . These two jar files both have org.hamcrest package. 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. I searched a lot and found some related stuff on 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).
  2. Somehow tell java compiler that these two jars should be treated as a single automatic module.

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 .
  2. add maven-assembly-plugin to this module with appendAssemblyId set to false.
  3. remove the dependency to hamcrest-core and hamcrest-library from other maven modules and instead add dependency to hamcrest-all .
  4. exclude hamcrest-core and hamcrest-library when including dependency to hamcrest-all .

What it actually does is that it unpacks hamcrest-core and hamcrest-library in the jar file created for hamrest-all . And because each jar file is treated as one module by JMPS, the problem is gone :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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