3

I have 2 libraries, which I have migrated to Java 10 and used module system.

First thing which makes me worry is that I had a lot of errors like: Error:java: the unnamed module reads package org.aspectj.internal.lang.reflect from both aspectjrt and org.aspectj.weaver. To fix that, I have added requires org.aspectj.weaver; to module info. Actually I had to put there lot of other things which I don't use, but e.g. Spring uses. Only then I was able to compile it.

So in the end, my module-info for the first library looks like this:

module my.lib1 {
    requires spring.context;
    requires spring.core;
    requires spring.context.support;
    requires spring.beans;
    requires org.aspectj.weaver;
    requires slf4j.api;
    requires metrics.core;
    requires com.fasterxml.jackson.databind;
    requires java.validation;
    requires org.hibernate.validator;
    requires javax.el;

    exports my.lib1;
}

For the second library I also had to add a lot of libs that are used by dependencies, not me:

module my.lib2 {
    requires org.hibernate.orm.core;
    requires java.sql;
    requires java.persistence;
    requires spring.context;
    requires spring.tx;
    requires spring.orm;
    requires spring.data.jpa;
    requires spring.beans;
    requires HikariCP;
    requires metrics.core;
    requires slf4j.api;
    requires spring.core;

    exports my.lib2;
}

Both libs are compiling now. I put them in my local mvn repo and started third project which depends on this two.

module my.project {
    requires my.lib1;
    requires my.lib2;
}

And now I got the same error... Error:java: the unnamed module reads package org.aspectj.internal.lang.reflect from both aspectjrt and org.aspectj.weaver, but this time, adding requires org.aspectj.weaver; doesn't help. I have noticed when I put only one of the libs in the module (not both together, but one lib1 or lib2) it works.

Is it normal that I have to put different libs in my module-info which are used not by my but by other dependencies? (e.g. shouldn't it be Spring's responsibility to require aspectj?).

And the most important thing: how do I fix the problem with my project which depends on my two libs?

Mariusz.v7
  • 2,322
  • 2
  • 16
  • 24

1 Answers1

2

I have noticed when I put only one of the libs in the module (not both together, but one lib1 or lib2) it works.

Using the module system, a package may only be offered by a single module in your current module graph. This is one of the main reasons for the introduction of the module system: avoidance of ambiguous dependencies (as they occur on the classic class path; called split packages). And that's why it works with only one of your modules.

In your case, both modules aspectjrt and org.aspectj.weaver are offering the same package org.aspectj.internal.lang.reflect and that's where the error message comes from (the unnamed module reads package org.aspectj.internal.lang.reflect from both aspectjrt and org.aspectj.weaver).

how do I fix the problem with my project which depends on my two libs?

like Andy Guibert wrote to this topic, you could:

  1. unsplit the package

  2. bundle them in a single package

  3. hope that third party modules become named modules in the future.

AspectJ has been ported to the module system. Check the newest version, if you don't use it (requires org.aspectj.runtime, requires org.aspectj.weaver)?

Additionally, here you can find some more information about the module system.

Rene Knop
  • 1,788
  • 3
  • 15
  • 27