2012-08-08 68 views
0

我最近升級了一個項目使用hibernate 3.6.10.Final。該項目使用常春藤檢索和依賴項。現在我收到一個slf4j中找不到方法的錯誤。我認爲這是由於hibernate-core(需要1.6.1)和hibernate-commons-annotations(需要1.5.8)之間的slf4j依賴衝突造成的。根據hibernate-core ivy.xml,hibernate-core依賴於hibernate-commons-annotations。休眠3.6.10依賴分辨率

這是我得到的錯誤:

Constructor threw exception; nested exception is java.lang.NoSuchMethodError: org.slf4j.helpers.MessageFormatter.format(Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/String;: 
java.lang.NoSuchMethodError: org.slf4j.helpers.MessageFormatter.format(Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/String; 
     at org.slf4j.impl.JDK14LoggerAdapter.info(JDK14LoggerAdapter.java:303) 
     at org.hibernate.annotations.common.Version.<clinit>(Version.java:37) 
     at org.hibernate.annotations.common.reflection.java.JavaReflectionManager.<clinit>(JavaReflectionManager.java:73) 
     at org.hibernate.cfg.Configuration.createReflectionManager(Configuration.java:2988) 
     at org.hibernate.cfg.Configuration.createReflectionManager(Configuration.java:2984) 
     at org.hibernate.cfg.Configuration.reset(Configuration.java:307) 
     at org.hibernate.cfg.Configuration.<init>(Configuration.java:298) 
     at org.hibernate.cfg.Configuration.<init>(Configuration.java:302) 
     at mb.common.hibernate.cfg.DefaultEnhancedConfiguration.<init>(DefaultEnhancedConfiguration.java:10) 
     at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
     at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) 
     at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 
     at java.lang.reflect.Constructor.newInstance(Constructor.java:525) 

我查了老SLF4J的javadoc果然看起來像SLF4J-1.6.1有一個返回FomattingTuple,而不是一個字符串格式的方法。

下面是從maven2的庫中檢索的hibernate-core-3.6.10.Final的ivy.xml文件:

<?xml version="1.0" encoding="UTF-8"?> 
<ivy-module version="2.0" xmlns:m="http://ant.apache.org/ivy/maven"> 
    <info organisation="org.hibernate" 
     module="hibernate-core" 
     revision="3.6.10.Final" 
     status="release" 
     publication="20120208201903" 
    > 
     <description homepage=""> 
     The core functionality of Hibernate 
     </description> 
     <m:dependency.management__javassist__javassist__version>3.12.0.GA</m:dependency.management__javassist__javassist__version> 
     <m:properties__bytemanVersion>1.5.1</m:properties__bytemanVersion> 
     <m:dependency.management__commons-collections__commons-collections__version>3.1</m:dependency.management__commons-collections__commons-collections__version> 
     <m:dependency.management__javax.validation__validation-api__version>1.0.0.GA</m:dependency.management__javax.validation__validation-api__version> 
     <m:dependency.management__dom4j__dom4j__version>1.6.1</m:dependency.management__dom4j__dom4j__version> 
     <m:dependency.management__org.hibernate__hibernate-validator__version>4.1.0.Final</m:dependency.management__org.hibernate__hibernate-validator__version> 
     <m:dependency.management__cglib__cglib__version>2.2</m:dependency.management__cglib__cglib__version> 
     <m:dependency.management__org.hibernate.javax.persistence__hibernate-jpa-2.0-api__version>1.0.1.Final</m:dependency.management__org.hibernate.javax.persistence__hibernate-jpa-2.0-api__version> 
     <m:dependency.management__com.h2database__h2__version>1.2.145</m:dependency.management__com.h2database__h2__version> 
     <m:properties__project.build.sourceEncoding>UTF-8</m:properties__project.build.sourceEncoding> 
     <m:properties__slf4jVersion>1.6.1</m:properties__slf4jVersion> 
     <m:dependency.management__org.slf4j__slf4j-api__version>1.6.1</m:dependency.management__org.slf4j__slf4j-api__version> 
     <m:dependency.management__antlr__antlr__version>2.7.6</m:dependency.management__antlr__antlr__version> 
     <m:dependency.management__org.hibernate__hibernate-commons-annotations__version>3.2.0.Final</m:dependency.management__org.hibernate__hibernate-commons-annotations__version> 
     <m:properties__antlrPluginVersion>2.1</m:properties__antlrPluginVersion> 
     <m:dependency.management__org.jboss.byteman__byteman__version>1.5.1</m:dependency.management__org.jboss.byteman__byteman__version> 
     <m:maven.plugins>org.jboss.maven.plugins__maven-jdocbook-style-plugin__null|org.codehaus.groovy.maven__gmaven-plugin__1.0|org.apache.maven.plugins__maven-enforcer-plugin__null|org.apache.maven.plugins__maven-compiler-plugin__null|org.apache.maven.plugins__maven-jar-plugin__null|org.apache.maven.plugins__maven-surefire-plugin__null|org.apache.maven.plugins__maven-source-plugin__null|org.jboss.maven.plugins__maven-jdocbook-style-plugin__null|org.codehaus.groovy.maven__gmaven-plugin__1.0|org.apache.maven.plugins__maven-enforcer-plugin__null|org.apache.maven.plugins__maven-compiler-plugin__null|org.apache.maven.plugins__maven-jar-plugin__null|org.apache.maven.plugins__maven-surefire-plugin__null|org.apache.maven.plugins__maven-source-plugin__null|org.codehaus.mojo__antlr-maven-plugin__2.1|org.jboss.maven.plugins__maven-injection-plugin__null|null__maven-jar-plugin__null</m:maven.plugins> 
    </info> 
    <configurations> 
     <conf name="default" visibility="public" description="runtime dependencies and master artifact can be used with this conf" extends="runtime,master"/> 
     <conf name="master" visibility="public" description="contains only the artifact published by this module itself, with no transitive dependencies"/> 
     <conf name="compile" visibility="public" description="this is the default scope, used if none is specified. Compile dependencies are available in all classpaths."/> 
     <conf name="provided" visibility="public" description="this is much like compile, but indicates you expect the JDK or a container to provide it. It is only available on the compilation classpath, and is not transitive."/> 
     <conf name="runtime" visibility="public" description="this scope indicates that the dependency is not required for compilation, but is for execution. It is in the runtime and test classpaths, but not the compile classpath." extends="compile"/> 
     <conf name="test" visibility="private" description="this scope indicates that the dependency is not required for normal use of the application, and is only available for the test compilation and execution phases." extends="runtime"/> 
     <conf name="system" visibility="public" description="this scope is similar to provided except that you have to provide the JAR which contains it explicitly. The artifact is always available and is not looked up in a repository."/> 
     <conf name="sources" visibility="public" description="this configuration contains the source artifact of this module, if any."/> 
     <conf name="javadoc" visibility="public" description="this configuration contains the javadoc artifact of this module, if any."/> 
     <conf name="optional" visibility="public" description="contains all optional dependencies"/> 
    </configurations> 
    <publications> 
     <artifact name="hibernate-core" type="jar" ext="jar" conf="master"/> 
     <artifact name="hibernate-core" type="source" ext="jar" conf="sources" m:classifier="sources"/> 
    </publications> 
    <dependencies> 
     <dependency org="antlr" name="antlr" rev="2.7.6" force="true" conf="compile->compile(*),master(*);runtime->runtime(*)"/> 
     <dependency org="commons-collections" name="commons-collections" rev="3.1" force="true" conf="compile->compile(*),master(*);runtime->runtime(*)"/> 
     <dependency org="dom4j" name="dom4j" rev="1.6.1" force="true" conf="compile->compile(*),master(*);runtime->runtime(*)"> 
      <exclude org="xml-apis" module="xml-apis" name="*" type="*" ext="*" conf="" matcher="exact"/> 
     </dependency> 
     <dependency org="org.hibernate" name="hibernate-commons-annotations" rev="3.2.0.Final" force="true" conf="compile->compile(*),master(*);runtime->runtime(*)"/> 
     <dependency org="org.hibernate.javax.persistence" name="hibernate-jpa-2.0-api" rev="1.0.1.Final" force="true" conf="compile->compile(*),master(*);runtime->runtime(*)"/> 
     <dependency org="javax.validation" name="validation-api" rev="1.0.0.GA" force="true" conf="provided->compile(*),provided(*),runtime(*),master(*)"/> 
     <dependency org="org.hibernate" name="hibernate-validator" rev="4.1.0.Final" force="true" conf="test->runtime(*),master(*)"/> 
     <dependency org="javax.xml.bind" name="jaxb-api" rev="2.2" force="true" conf="test->runtime(*),master(*)"> 
      <exclude org="javax.xml.stream" module="stax-api" name="*" type="*" ext="*" conf="" matcher="exact"/> 
      <exclude org="javax.activation" module="activation" name="*" type="*" ext="*" conf="" matcher="exact"/> 
     </dependency> 
     <dependency org="com.sun.xml.bind" name="jaxb-impl" rev="2.1.12" force="true" conf="test->runtime(*),master(*)"/> 
     <dependency org="javassist" name="javassist" rev="3.12.0.GA" force="true" conf="optional->compile(*),master(*)"/> 
     <dependency org="cglib" name="cglib" rev="2.2" force="true" conf="optional->compile(*),master(*)"/> 
     <dependency org="javax.transaction" name="jta" rev="1.1" force="true" conf="compile->compile(*),master(*);runtime->runtime(*)"/> 
     <dependency org="org.jboss.javaee" name="jboss-jacc-api_JDK4" rev="1.1.0" force="true" conf="provided->compile(*),provided(*),runtime(*),master(*)"> 
      <exclude org="org.jboss.javaee" module="jboss-servlet-api_3.0" name="*" type="*" ext="*" conf="" matcher="exact"/> 
      <exclude org="org.jboss.logging" module="jboss-logging-spi" name="*" type="*" ext="*" conf="" matcher="exact"/> 
      <exclude org="org.jboss" module="jboss-common-core" name="*" type="*" ext="*" conf="" matcher="exact"/> 
     </dependency> 
     <dependency org="ant" name="ant" rev="1.6.5" force="true" conf="provided->compile(*),provided(*),runtime(*),master(*)"/> 
     <dependency org="com.h2database" name="h2" rev="1.2.145" force="true" conf="test->runtime(*),master(*)"/> 
     <dependency org="org.slf4j" name="slf4j-api" rev="1.6.1" force="true" conf="compile->compile(*),master(*);runtime->runtime(*)"/> 
     <dependency org="junit" name="junit" rev="3.8.1" force="true" conf="test->runtime(*),master(*)"/> 
     <dependency org="org.slf4j" name="jcl-over-slf4j" rev="1.6.1" force="true" conf="test->runtime(*),master(*)"/> 
     <dependency org="org.slf4j" name="slf4j-log4j12" rev="1.6.1" force="true" conf="test->runtime(*),master(*)"/> 
     <dependency org="commons-logging" name="commons-logging" rev="99.0-does-not-exist" force="true" conf="test->runtime(*),master(*)"/> 
     <dependency org="commons-logging" name="commons-logging-api" rev="99.0-does-not-exist" force="true" conf="test->runtime(*),master(*)"/> 
     <override org="commons-collections" module="commons-collections" matcher="exact" rev="3.1"/> 
     <override org="org.hibernate" module="hibernate-validator" matcher="exact" rev="4.1.0.Final"/> 
     <override org="javassist" module="javassist" matcher="exact" rev="3.12.0.GA"/> 
     <override org="org.jboss.byteman" module="byteman" matcher="exact" rev="1.5.1"/> 
     <override org="cglib" module="cglib" matcher="exact" rev="2.2"/> 
     <override org="org.hibernate.javax.persistence" module="hibernate-jpa-2.0-api" matcher="exact" rev="1.0.1.Final"/> 
     <override org="com.h2database" module="h2" matcher="exact" rev="1.2.145"/> 
     <override org="org.hibernate" module="hibernate-commons-annotations" matcher="exact" rev="3.2.0.Final"/> 
     <override org="org.slf4j" module="slf4j-api" matcher="exact" rev="1.6.1"/> 
     <override org="antlr" module="antlr" matcher="exact" rev="2.7.6"/> 
     <override org="javax.validation" module="validation-api" matcher="exact" rev="1.0.0.GA"/> 
     <override org="dom4j" module="dom4j" matcher="exact" rev="1.6.1"/> 
    </dependencies> 
</ivy-module> 

這裏的hibernate-commons-annotations-3.2.0.Final是的ivy.xml從maven2的存儲庫中檢索文件:

<?xml version="1.0" encoding="UTF-8"?> 
<ivy-module version="2.0" xmlns:m="http://ant.apache.org/ivy/maven"> 
    <info organisation="org.hibernate" 
     module="hibernate-commons-annotations" 
     revision="3.2.0.Final" 
     status="release" 
     publication="20100309114153" 
    > 
     <license name="GNU LESSER GENERAL PUBLIC LICENSE" url="http://www.gnu.org/licenses/lgpl.txt" /> 
     <description homepage="http://hibernate.org"> 
     Common reflection code used in support of annotation processing 
     </description> 
     <m:dependency.management__org.slf4j__slf4j-api__version>1.5.8</m:dependency.management__org.slf4j__slf4j-api__version> 
     <m:properties__slf4jVersion>1.5.8</m:properties__slf4jVersion> 
     <m:maven.plugins>org.apache.maven.plugins__maven-compiler-plugin__null</m:maven.plugins> 
    </info> 
    <configurations> 
     <conf name="default" visibility="public" description="runtime dependencies and master artifact can be used with this conf" extends="runtime,master"/> 
     <conf name="master" visibility="public" description="contains only the artifact published by this module itself, with no transitive dependencies"/> 
     <conf name="compile" visibility="public" description="this is the default scope, used if none is specified. Compile dependencies are available in all classpaths."/> 
     <conf name="provided" visibility="public" description="this is much like compile, but indicates you expect the JDK or a container to provide it. It is only available on the compilation classpath, and is not transitive."/> 
     <conf name="runtime" visibility="public" description="this scope indicates that the dependency is not required for compilation, but is for execution. It is in the runtime and test classpaths, but not the compile classpath." extends="compile"/> 
     <conf name="test" visibility="private" description="this scope indicates that the dependency is not required for normal use of the application, and is only available for the test compilation and execution phases." extends="runtime"/> 
     <conf name="system" visibility="public" description="this scope is similar to provided except that you have to provide the JAR which contains it explicitly. The artifact is always available and is not looked up in a repository."/> 
     <conf name="sources" visibility="public" description="this configuration contains the source artifact of this module, if any."/> 
     <conf name="javadoc" visibility="public" description="this configuration contains the javadoc artifact of this module, if any."/> 
     <conf name="optional" visibility="public" description="contains all optional dependencies"/> 
    </configurations> 
    <publications> 
     <artifact name="hibernate-commons-annotations" type="jar" ext="jar" conf="master"/> 
     <artifact name="hibernate-commons-annotations" type="source" ext="jar" conf="sources" m:classifier="sources"/> 
     <artifact name="hibernate-commons-annotations" type="javadoc" ext="jar" conf="javadoc" m:classifier="javadoc"/> 
    </publications> 
    <dependencies> 
     <dependency org="org.slf4j" name="slf4j-api" rev="1.5.8" force="true" conf="compile->compile(*),master(*);runtime->runtime(*)"/> 
     <dependency org="junit" name="junit" rev="3.8.1" force="true" conf="test->runtime(*),master(*)"/> 
     <dependency org="org.slf4j" name="jcl-over-slf4j" rev="1.5.8" force="true" conf="test->runtime(*),master(*)"/> 
     <dependency org="org.slf4j" name="slf4j-log4j12" rev="1.5.8" force="true" conf="test->runtime(*),master(*)"/> 
     <dependency org="commons-logging" name="commons-logging" rev="99.0-does-not-exist" force="true" conf="test->runtime(*),master(*)"/> 
     <dependency org="commons-logging" name="commons-logging-api" rev="99.0-does-not-exist" force="true" conf="test->runtime(*),master(*)"/> 
     <override org="org.slf4j" module="slf4j-api" matcher="exact" rev="1.5.8"/> 
    </dependencies> 
</ivy-module> 

我應該怎麼解決這個矛盾的依賴?

回答

2

你的問題是在類路徑中只能使用slf4j jar的一個副本,所以你需要決定保留哪一個。選擇較舊的版本可能會修復一個編譯錯誤,但會導致另一個錯誤:-)

默認情況下,ivy會在假設它將向後兼容的情況下選擇最新的一個。您可以通過使用conflict指示器覆蓋此行爲,如下所示:

<dependencies> 
    <dependency org="org.hibernate" name="hibernate-core" rev="3.6.10.Final" conf="compile->default"/> 
    <dependency org="org.slf4j" name="slf4j-api" rev="1.5.8" conf="compile->default"/> 

    <conflict org="org.slf4j" module="slf4j-api" rev="1.5.8"/> 
</dependencies> 
+0

感謝馬克。我結束了使用舊版本的hibernate,我沒有再發生衝突。主要問題是在不同版本的slf4j之間有一些改變了我的構建的API變化。 – algernon 2012-10-17 17:20:52