为什么Maven希望使用javax.persistence而不是替代的Hibernate版本?

huangapple go评论77阅读模式
英文:

Why does Maven want to use javax.persistence instead of the alternative hibernate version

问题

我有一些使用以下代码的代码:

import javax.persistence.*;

我在pom.xml中有以下依赖项:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.4.15.Final</version>
</dependency>
<dependency>
    <groupId>org.hibernate.javax.persistence</groupId>
    <artifactId>hibernate-jpa-2.1-api</artifactId>
    <version>1.0.2.Final</version>
</dependency>

我可以使用“mvn compile”编译,但是当我尝试“mvn package”时,我会收到以下错误:

[WARNING] 发现未声明的依赖项:
[WARNING]    javax.persistence:javax.persistence-api:jar:2.2:compile
[WARNING] 发现未使用的声明依赖项:
[WARNING]    org.hibernate.javax.persistence:hibernate-jpa-2.1-api:jar:1.0.2.Final:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------

我检查了来自hibernate-jpa-2.1-api的jar文件,它确实包含javax.persistence.*类。

那么为什么Maven不接受这是要使用的jar文件,并坚持要使用javax.persistence-api呢?

如果我尝试切换到javax.persistence:javax.persistence-api:jar:2.2而不是hibernate的话,我会在“mvn package”中收到很多错误,看起来像是这样:

[WARNING] 在[jakarta.xml.bind:jakarta.xml.bind-api:2.3.3,javax.xml.bind:jaxb-api:2.3.1]中发现了重复(但相等)的类:
[WARNING]   javax.xml.bind.DatatypeConverterInterface
[WARNING]   javax.xml.bind.Element
[WARNING]   javax.xml.bind.JAXBContextFactory
[WARNING]   javax.xml.bind.Marshaller
[WARNING]   ...

有什么想法出了什么问题吗?

英文:

I have some code that uses:

import javax.persistence.*;

I have the following dependencies in pom.xml:

&lt;dependency&gt;
            &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
            &lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
            &lt;version&gt;5.4.15.Final&lt;/version&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.hibernate.javax.persistence&lt;/groupId&gt;
            &lt;artifactId&gt;hibernate-jpa-2.1-api&lt;/artifactId&gt;
            &lt;version&gt;1.0.2.Final&lt;/version&gt;
        &lt;/dependency&gt;

I can compile ok using "mvn compile" but when I try to "mvn package" I get the following error:

[WARNING] Used undeclared dependencies found:
[WARNING]    javax.persistence:javax.persistence-api:jar:2.2:compile
[WARNING] Unused declared dependencies found:
[WARNING]    org.hibernate.javax.persistence:hibernate-jpa-2.1-api:jar:1.0.2.Final:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------

I checked the jar from hibernate-jpa-2.1-api and it does contain the javax.persistence.* classes.

So why won't Maven accept that this is the jar to use and insists it wants the javax.persistence-api instead?

If I try to switch to the javax.persistence:javax.persistence-api:jar:2.2 instead of the hibernate one I get a lot of errors from 'mvn package' that look like this:

[WARNING] Found duplicate (but equal) classes in [jakarta.xml.bind:jakarta.xml.bind-api:2.3.3, javax.xml.bind:jaxb-api:2.3.1]:
[WARNING]   javax.xml.bind.DatatypeConverterInterface
[WARNING]   javax.xml.bind.Element
[WARNING]   javax.xml.bind.JAXBContextFactory
[WARNING]   javax.xml.bind.Marshaller
[WARNING]   javax.xml.bind.NotIdentifiableEvent
[WARNING]   javax.xml.bind.ParseConversionEvent
[WARNING]   javax.xml.bind.PrintConversionEvent
[WARNING]   javax.xml.bind.Unmarshaller
[WARNING]   javax.xml.bind.UnmarshallerHandler
[WARNING]   javax.xml.bind.ValidationEvent
[WARNING]   javax.xml.bind.ValidationEventHandler
[WARNING]   javax.xml.bind.ValidationEventLocator
[WARNING]   javax.xml.bind.Validator
[WARNING]   javax.xml.bind.annotation.DomHandler
[WARNING]   javax.xml.bind.annotation.XmlAccessorOrder
[WARNING]   javax.xml.bind.annotation.XmlAccessorType
[WARNING]   javax.xml.bind.annotation.XmlAnyAttribute
[WARNING]   javax.xml.bind.annotation.XmlAnyElement
[WARNING]   javax.xml.bind.annotation.XmlAttachmentRef
[WARNING]   javax.xml.bind.annotation.XmlAttribute
[WARNING]   javax.xml.bind.annotation.XmlElement
[WARNING]   javax.xml.bind.annotation.XmlElementDecl
[WARNING]   javax.xml.bind.annotation.XmlElementRef
[WARNING]   javax.xml.bind.annotation.XmlElementRefs
[WARNING]   javax.xml.bind.annotation.XmlElementWrapper
&lt;...lots more lines like this&gt;

Any ideas what is going wrong?

答案1

得分: 1

很简单:org.hibernate:hibernate-core:5.4.15.Final 依赖于来自 javax.persistence:javax.persistence-api:2.2javax.persistence.* 类(这是在其自己的 POM 中声明的依赖关系),因此,通过包含 hibernate-core,您会额外获取一个传递性依赖 javax.persistence-api。这就是为什么您会收到“发现未声明的依赖项”的警告。

出于同样的原因,org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.2.Final 也没有被使用。请注意,Maven 无法知道 hibernate-jpa-2.1-apijavax.persistence-api 可以互换使用。

如果由于某种奇怪的原因,您希望强制使用 hibernate-jpa-2.1-api,您需要显式地排除 javax.persistence-api 作为传递性依赖项,如下所示:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.4.15.Final</version>
    <exclusions>
        <exclusion>
            <groupId>javax.persistence</groupId>
            <artifactId>javax.persistence-api</artifactId>
        </exclusion>
    </exclusions>
</dependency>
英文:

It's really quite simple: org.hibernate:hibernate-core:5.4.15.Final depends on javax.persistence.* classes from javax.persistence:javax.persistence-api:2.2 (that's the dependency declared in its own POM) and so, by including hibernate-core, you get an additional, transitive dependency javax.persistence-api pulled in. This is why you're getting the 'Used undeclared dependencies found' warning.

For the very same reason, org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.2.Final is not used. Note that Maven does not have a way of knowing hibernate-jpa-2.1-api and javax.persistence-api are supposed to be interchangeable.

If, for some strange reason, you want to force hibernate-jpa-2.1-api to be used instead, you need to explicitly exclude javax.persistence-api as a transitive dependency like so:

&lt;dependency&gt;
    &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
    &lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
    &lt;version&gt;5.4.15.Final&lt;/version&gt;
    &lt;exclusions&gt;
        &lt;exclusion&gt;
            &lt;groupId&gt;javax.persistence&lt;/groupId&gt;
            &lt;artifactId&gt;javax.persistence-api&lt;/artifactId&gt;
        &lt;/exclusion&gt;
    &lt;/exclusions&gt;
&lt;/dependency&gt;

答案2

得分: 0

  1. <version>1.0.2.Final</version> - 此声明表示:允许任何版本,但首选 1.0.2.FINAL。如果要指定精确版本,请将其声明为 &lt;version&gt;[1.0.2.Final]&lt;/version&gt;

  2. 通常在解析传递依赖关系时(或者在冲突时,这在本例中不适用),Maven 将选择最近的依赖项。在这种情况下,javax.persistence:javax.persistence-api:jar:2.2:compile 更接近于 org.hibernate:hibernate-core:jar:5.4.15.Final:compile

    mvn dependency:tree
     [INFO] 正在扫描项目...
     [INFO] 
     [INFO] -----------------------&lt; org.example:so-answers &gt;-----------------------
     [INFO] 正在构建 so-answers 1.0-SNAPSHOT
     [INFO] --------------------------------[ jar ]---------------------------------
     [INFO] 
     [INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ so-answers ---
     [INFO] org.example:so-answers:jar:1.0-SNAPSHOT
     [INFO] +- org.hibernate:hibernate-core:jar:5.4.15.Final:compile
     [INFO] |  +- org.jboss.logging:jboss-logging:jar:3.3.2.Final:compile
     [INFO] |  +- javax.persistence:javax.persistence-api:jar:2.2:compile
     [INFO] |  +- org.javassist:javassist:jar:3.24.0-GA:compile
     [INFO] |  +- net.bytebuddy:byte-buddy:jar:1.10.10:compile
     [INFO] |  +- antlr:antlr:jar:2.7.7:compile
     [INFO] |  +- org.jboss.spec.javax.transaction:jboss-transaction-api_1.2_spec:jar:1.1.1.Final:compile
     [INFO] |  +- org.jboss:jandex:jar:2.1.3.Final:compile
     [INFO] |  +- com.fasterxml:classmate:jar:1.5.1:compile
     [INFO] |  +- javax.activation:javax.activation-api:jar:1.2.0:compile
     [INFO] |  +- org.dom4j:dom4j:jar:2.1.3:compile
     [INFO] |  +- org.hibernate.common:hibernate-commons-annotations:jar:5.1.0.Final:compile
     [INFO] |  +- javax.xml.bind:jaxb-api:jar:2.3.1:compile
     [INFO] |  \- org.glassfish.jaxb:jaxb-runtime:jar:2.3.1:compile
     [INFO] |     +- org.glassfish.jaxb:txw2:jar:2.3.1:compile
     [INFO] |     +- com.sun.istack:istack-commons-runtime:jar:3.0.7:compile
     [INFO] |     +- org.jvnet.staxex:stax-ex:jar:1.8:compile
     [INFO] |     \- com.sun.xml.fastinfoset:FastInfoset:jar:1.2.15:compile
     [INFO] \- org.hibernate.javax.persistence:hibernate-jpa-2.1-api:jar:1.0.2.Final:compile
     [INFO] -----------------------------------------------------------------------
     [INFO] 构建成功
     [INFO] -----------------------------------------------------------------------
     [INFO] 总时间: 0.790 秒
     [INFO] 完成于: 2020-09-26T10:02:50-04:00
     [INFO] -----------------------------------------------------------------------
    

让我们添加排除来影响解析。

&lt;dependencies&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.hibernate&lt;/groupId&gt;
			&lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
			&lt;version&gt;5.4.15.Final&lt;/version&gt;
			&lt;exclusions&gt;
				&lt;exclusion&gt;
					&lt;groupId&gt;javax.persistence&lt;/groupId&gt;
					&lt;artifactId&gt;javax.persistence-api&lt;/artifactId&gt;
				&lt;/exclusion&gt;
			&lt;/exclusions&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.hibernate.javax.persistence&lt;/groupId&gt;
			&lt;artifactId&gt;hibernate-jpa-2.1-api&lt;/artifactId&gt;
			&lt;version&gt;1.0.2.Final&lt;/version&gt;
		&lt;/dependency&gt;
	&lt;/dependencies&gt;

然后再次检查依赖树

  mvn dependency:tree
    [INFO] 正在扫描项目...
    [INFO] 
    [INFO] -----------------------&lt; org.example:so-answers &gt;-----------------------
    [INFO] 正在构建 so-answers 1.0-SNAPSHOT
    [INFO] --------------------------------[ jar ]---------------------------------
    [INFO] 
    [INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ so-answers ---
    [INFO] org.example:so-answers:jar:1.0-SNAPSHOT
    [INFO] +- org.hibernate:hibernate-core:jar:5.4.15.Final:compile
    [INFO] |  +- org.jboss.logging:jboss-logging:jar:3.3.2.Final:compile
    [INFO] |  +- org.javassist:javassist:jar:3.24.0-GA:compile
    [INFO] |  +- net.bytebuddy:byte-buddy:jar:1.10.10:compile
    [INFO] |  +- antlr:antlr:jar:2.7.7:compile
    [INFO] |  +- org.jboss.spec.javax.transaction:jboss-transaction-api_1.2_spec:jar:1.1.1.Final:compile
    [INFO] |  +- org.jboss:jandex:jar:2.1.3.Final:compile
    [INFO] |  +- com.fasterxml:classmate:jar:1.5.1:compile
    [INFO] |  +- javax.activation:javax.activation-api:jar:1.2.0:compile
    [INFO] |  +- org.dom4j:dom4j:jar:2.1.3:compile
    [INFO] |  +- org.hibernate.common:hibernate-commons
英文:
  1. <version>1.0.2.Final</version> - This declaration says : allow any version, but prefer 1.0.2.FINAL.
    If you want to specify exact version declare it as &lt;version&gt;[1.0.2.Final]&lt;/version&gt;

  2. Generally while resolving a transitive dependency (or on conflict, which is not the case in this case) Maven will choose the nearest dependency. In this case javax.persistence:javax.persistence-api:jar:2.2:compile is closer to org.hibernate:hibernate-core:jar:5.4.15.Final:compile.

    mvn dependency:tree
     [INFO] Scanning for projects...
     [INFO] 
     [INFO] -----------------------&lt; org.example:so-answers &gt;-----------------------
     [INFO] Building so-answers 1.0-SNAPSHOT
     [INFO] --------------------------------[ jar ]---------------------------------
     [INFO] 
     [INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ so-answers ---
     [INFO] org.example:so-answers:jar:1.0-SNAPSHOT
     [INFO] +- org.hibernate:hibernate-core:jar:5.4.15.Final:compile
     [INFO] |  +- org.jboss.logging:jboss-logging:jar:3.3.2.Final:compile
     [INFO] |  +- javax.persistence:javax.persistence-api:jar:2.2:compile
     [INFO] |  +- org.javassist:javassist:jar:3.24.0-GA:compile
     [INFO] |  +- net.bytebuddy:byte-buddy:jar:1.10.10:compile
     [INFO] |  +- antlr:antlr:jar:2.7.7:compile
     [INFO] |  +- org.jboss.spec.javax.transaction:jboss-transaction-api_1.2_spec:jar:1.1.1.Final:compile
     [INFO] |  +- org.jboss:jandex:jar:2.1.3.Final:compile
     [INFO] |  +- com.fasterxml:classmate:jar:1.5.1:compile
     [INFO] |  +- javax.activation:javax.activation-api:jar:1.2.0:compile
     [INFO] |  +- org.dom4j:dom4j:jar:2.1.3:compile
     [INFO] |  +- org.hibernate.common:hibernate-commons-annotations:jar:5.1.0.Final:compile
     [INFO] |  +- javax.xml.bind:jaxb-api:jar:2.3.1:compile
     [INFO] |  \- org.glassfish.jaxb:jaxb-runtime:jar:2.3.1:compile
     [INFO] |     +- org.glassfish.jaxb:txw2:jar:2.3.1:compile
     [INFO] |     +- com.sun.istack:istack-commons-runtime:jar:3.0.7:compile
     [INFO] |     +- org.jvnet.staxex:stax-ex:jar:1.8:compile
     [INFO] |     \- com.sun.xml.fastinfoset:FastInfoset:jar:1.2.15:compile
     [INFO] \- org.hibernate.javax.persistence:hibernate-jpa-2.1-api:jar:1.0.2.Final:compile
     [INFO] ------------------------------------------------------------------------
     [INFO] BUILD SUCCESS
     [INFO] ------------------------------------------------------------------------
     [INFO] Total time: 0.790 s
     [INFO] Finished at: 2020-09-26T10:02:50-04:00
     [INFO] ------------------------------------------------------------------------
    

Let's add exclusion to influence resolution.

&lt;dependencies&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.hibernate&lt;/groupId&gt;
			&lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
			&lt;version&gt;5.4.15.Final&lt;/version&gt;
			&lt;exclusions&gt;
				&lt;exclusion&gt;
					&lt;groupId&gt;javax.persistence&lt;/groupId&gt;
					&lt;artifactId&gt;javax.persistence-api&lt;/artifactId&gt;
				&lt;/exclusion&gt;
			&lt;/exclusions&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.hibernate.javax.persistence&lt;/groupId&gt;
			&lt;artifactId&gt;hibernate-jpa-2.1-api&lt;/artifactId&gt;
			&lt;version&gt;1.0.2.Final&lt;/version&gt;
		&lt;/dependency&gt;
	&lt;/dependencies&gt;

And then check dependency tree again

  mvn dependency:tree
    [INFO] Scanning for projects...
    [INFO] 
    [INFO] -----------------------&lt; org.example:so-answers &gt;-----------------------
    [INFO] Building so-answers 1.0-SNAPSHOT
    [INFO] --------------------------------[ jar ]---------------------------------
    [INFO] 
    [INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ so-answers ---
    [INFO] org.example:so-answers:jar:1.0-SNAPSHOT
    [INFO] +- org.hibernate:hibernate-core:jar:5.4.15.Final:compile
    [INFO] |  +- org.jboss.logging:jboss-logging:jar:3.3.2.Final:compile
    [INFO] |  +- org.javassist:javassist:jar:3.24.0-GA:compile
    [INFO] |  +- net.bytebuddy:byte-buddy:jar:1.10.10:compile
    [INFO] |  +- antlr:antlr:jar:2.7.7:compile
    [INFO] |  +- org.jboss.spec.javax.transaction:jboss-transaction-api_1.2_spec:jar:1.1.1.Final:compile
    [INFO] |  +- org.jboss:jandex:jar:2.1.3.Final:compile
    [INFO] |  +- com.fasterxml:classmate:jar:1.5.1:compile
    [INFO] |  +- javax.activation:javax.activation-api:jar:1.2.0:compile
    [INFO] |  +- org.dom4j:dom4j:jar:2.1.3:compile
    [INFO] |  +- org.hibernate.common:hibernate-commons-annotations:jar:5.1.0.Final:compile
    [INFO] |  +- javax.xml.bind:jaxb-api:jar:2.3.1:compile
    [INFO] |  \- org.glassfish.jaxb:jaxb-runtime:jar:2.3.1:compile
    [INFO] |     +- org.glassfish.jaxb:txw2:jar:2.3.1:compile
    [INFO] |     +- com.sun.istack:istack-commons-runtime:jar:3.0.7:compile
    [INFO] |     +- org.jvnet.staxex:stax-ex:jar:1.8:compile
    [INFO] |     \- com.sun.xml.fastinfoset:FastInfoset:jar:1.2.15:compile
    [INFO] \- org.hibernate.javax.persistence:hibernate-jpa-2.1-api:jar:1.0.2.Final:compile
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 0.927 s
    [INFO] Finished at: 2020-09-26T10:23:46-04:00
    [INFO] ------------------------------------------------------------------------

Now [INFO] | +- javax.persistence:javax.persistence-api:jar:2.2:compile line is absent - effectively we asked Maven to use org.hibernate.javax.persistence:hibernate-jpa-2.1-api:jar:1.0.2.Final:compile.

huangapple
  • 本文由 发表于 2020年9月26日 19:42:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/64077275.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定