I am creating a library "A" that imports the dependency "B". When I included library "A" in the project, I get a NoSuchMethodError if I also did not explicitly include the "B" dependency in the pom of the project.

When importing an “A” dependency, is there a way to tell Maven to automatically include all its dependencies, including “B” (without manually importing “B”)?

pom.xml

  <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>parking</groupId> <artifactId>parking</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>javax.validation</groupId> <artifactId>validation-api</artifactId> <version>1.1.0.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>5.1.0.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>5.1.0.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.1.0.Final</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> </dependencies> </project> 

With this pom.xml, I get Exception: enter image description here

But as soon as I add all the dependencies of Hibernate, everything goes like clockwork. Pom.xml after adding all dependencies to manual:

 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>parking</groupId> <artifactId>parking</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>com.fasterxml</groupId> <artifactId>classmate</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>xml-apis</groupId> <artifactId>xml-apis</artifactId> <version>1.0.b2</version> </dependency> <dependency> <groupId>org.jboss.logging</groupId> <artifactId>jboss-logging</artifactId> <version>3.3.0.Final</version> </dependency> <dependency> <groupId>javax.validation</groupId> <artifactId>validation-api</artifactId> <version>1.1.0.Final</version> </dependency> <dependency> <groupId>org.apache.geronimo.specs</groupId> <artifactId>geronimo-jta_1.1_spec</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>org.javassist</groupId> <artifactId>javassist</artifactId> <version>3.20.0-GA</version> </dependency> <dependency> <groupId>org.hibernate.javax.persistence</groupId> <artifactId>hibernate-jpa-2.1-api</artifactId> <version>1.0.0.Final</version> </dependency> <dependency> <groupId>org.hibernate.common</groupId> <artifactId>hibernate-commons-annotations</artifactId> <version>5.0.1.Final</version> </dependency> <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>org.jboss</groupId> <artifactId>jandex</artifactId> <version>2.0.0.Final</version> </dependency> <dependency> <groupId>antlr</groupId> <artifactId>antlr</artifactId> <version>2.7.7</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>5.1.0.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>5.1.0.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.1.0.Final</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> </dependencies> </project> 

Counts:

Option 1: With the prescribed dependencies in pom:

Count 1

Option 2: without any dependencies in pom:

Count 2

  • Maven automatically pulls all dependencies, you have a different problem. Please pom.xml in text form and tell me how you run the tests. - enzo
  • @enzo reformulated the question, changed the header, in fact, the problem is not with junit, but with Hibernate. I can not understand how Maven works. - Alexander Tsukanov
  • 2
    You have in the description of the problem ClassNotFoundException, in the screenshot NoSuchMethodError. Maven himself clings to related dependencies and, apparently, the different versions of Hibernate used by the different libraries used differ too much. When you explicitly specify the libraries you use, it is they who get into the classpath, and not dependencies. Do you have the ability to use the tools for building a graph of dependencies, as in the Enterprise version of IntelliJ Idea? - Mark
  • @Mark fixed, thanks. Attached graphs, if I understand you correctly. - Alexander Tsukanov
  • I can not help but note a comment (despite the fact that the author of the question has a problem obviously in another): the maven project (let's call it P) may mark the dependency as optional, and in this case it will not be pulled up automatically in projects dependent on P. - etki

1 answer 1

Generally the problem is:

  <dependency> <groupId>org.hibernate.common</groupId> <artifactId>hibernate-commons-annotations</artifactId> <version>5.0.1.Final</version> </dependency> 

Without it, Hibernate did not want to take off. But this library is present, in the Entity manager dependecies, then Maven would have to pull it up automatically:

  <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>5.1.0.Final</version> </dependency> 

Hence, as mentioned, comrade enzo, it is, navrenoe, Jar Hell.