为什么具有范围的依赖性“提供”?隐藏Maven中的传递依赖? [英] Why does a dependency with scope "provided" hide transitive dependencies in Maven?
问题描述
我的Maven项目中有三个模块(稍微简化):
I have three modules in my Maven project (this is slightly simplified):
- model 包含JPA带注释的实体类
- 持久性实例化 EntityManager 并在其上调用方法
- application 在 model 中创建类的实例,设置一些值并将它们传递给 persistence
- model contains JPA annotated entity classes
- persistence instantiates an EntityManager and calls methods on it
- application creates instances of the classes in model, sets some values and passes them to persistence
模型和持久性显然取决于 javax.persistence
,但我认为应该不应用。
javax.persistence
依赖关系被移到顶部-level POM的 dependencyManagement
部分,因为它出现在许多子模块中,我只引用该条目。
The javax.persistence
dependency is moved to a top-level POM's dependencyManagement
section because it occurs in a number of submodules where I only reference that entry.
令人惊讶的是对我来说,当我将其范围设置为提供
时,我必须引用应用程序
中的依赖项,而我不知道当它的范围是 com时必须要这样做桩
。
What's surprising to me is that I have to reference the dependency in application
when I set its scope to provided
, whereas I don't have to when its scope is compile
.
提供的范围
,如果我没有列出它在应用程序的依赖项
中,构建失败并显示来自javac的错误消息:
With a scope of provided
, if I don't list it in the dependencies
for application, the build fails with an error message from javac:
com.sun.tools.javac.code.Symbol $ CompletionFailure:未找到javax.persistence.InheritanceType的类文件
com.sun.tools.javac.code.Symbol$CompletionFailure: class file for javax.persistence.InheritanceType not found
发生了什么?
推荐答案
模型和持久性显然取决于javax.persistence ,但我认为申请不应该。
model and persistence obviously depend on javax.persistence, but application shouldn't, I think.
这是真的。但是传递依赖性解析与您的问题无关(实际上, javax.persistence
提供
到 model
和持久性
应用程序
取决于编译
范围,因此它被省略,如 3.4.4。传递依赖)。
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).
在我看来,你是受害者这个错误: http://bugs.sun.com/view_bug.do?bug_id=6550655
In my opinion, you are victim of this bug: http://bugs.sun.com/view_bug.do?bug_id=6550655
我对使用继承注释的EJB3
实体有同样的问题:
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
使用此实体的客户端类不会在
编译时ejb3的宣传是
不在类路径上,但在
崩溃时出现以下消息:
com.sun.tools.javac.code.Symbol $ CompletionFailure:
class file for
javax.persistence.InheritanceType not
found
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
[...]
注意这是bug 6365854的特例(据报道是固定的);这里的问题似乎是注释使用枚举作为其值。
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.
当前的解决方法是将缺少的枚举添加到CLASSPATH。
The current workaround is to add the missing enum to the CLASSPATH.
在您的情况下,更糟糕的方法是将 javax.persistence
添加为提供
依赖于应用程序
模块。但这是JVM错误的解决方法,应用程序
不需要编译依赖项。
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.
这篇关于为什么具有范围的依赖性“提供”?隐藏Maven中的传递依赖?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!