Why does a dependency with scope “provided” hide transitive dependencies in Maven?

前端 未结 3 1093
礼貌的吻别
礼貌的吻别 2021-02-05 18:01

I have three modules in my Maven project (this is slightly simplified):

  • model contains JPA annotated entity classes
  • persistence instanti
相关标签:
3条回答
  • 2021-02-05 18:24

    The dependencyManagement section declares what dependencies will look like if you use them, not that you will use them. So you still need to declare a minimal dependency declaration to have the configuration applied in your child project. See the dependency management section of the Maven book for details.

    The minimum required is typically the groupId and the artifactId.

    If you want to inherit the configuration without declaring it at all, you should define it in the parent's dependencies section rather than dependencyManagement

    0 讨论(0)
  • 2021-02-05 18:31

    umm, because provided dependencies are not transitive? that's builtin behavior for maven.

    0 讨论(0)
  • 2021-02-05 18:47

    model and persistence obviously depend on javax.persistence, but application shouldn't, I think.

    That's true. But transitive dependencies resolution has nothing to do with your problem (and actually, javax.persistence is provided to model and persistence on which application depends with a compile scope so it's omitted as documented in 3.4.4. Transitive Dependencies).

    In my opinion, you are victim of this bug: http://bugs.sun.com/view_bug.do?bug_id=6550655

    I have the same issues with an EJB3 entity that uses the Inheritance annotation: @Inheritance(strategy=InheritanceType.SINGLE_TABLE)

    A client class using this entity won't compile when the ejb3 annatations are not on the classpath, but crash with the following message: com.sun.tools.javac.code.Symbol$CompletionFailure: class file for javax.persistence.InheritanceType not found

    [...]

    Note that is a special case of bug 6365854 (that is reported to be fixed); the problem here seems to be that the annotation is using an enum as its value.

    The current workaround is to add the missing enum to the CLASSPATH.

    In your case, the "less worse" way to do that would be to add javax.persistence as provided dependency to the application module. But that's a workaround to the JVM bug, application shouldn't need that dependency to compile.

    0 讨论(0)
提交回复
热议问题