Maven: hibernate-entitymanager along with javaee-api breaks my unit tests

I have two dependencies javaee-api and hibernate-entitymanager in my pom. But they don't work very well together: as soon as I add javaee-api all my unit tests are interrupted due to java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/validation/Validation . Without javaee-api everything works fine. Why is this?

(this question has been edited to fit the problem;))

+6
source share
3 answers

This is because java-ee-api.jar contains crippled classes . There are alternative dependencies that fix this problem. Reordering in pom.xml also helped me.

+4
source

Maven Dependencies are not ordered , but provide the concept of domains .

So what you need to do, use the scope to create the correct set of dependencies for:

  • compilation time
  • runtime on the server: (use, for example, provided for dependencies that are necessary in compiletime, but will be provided by the server, so your application should / should not contain them
  • testing time: use the test scope to add dependencies that are only needed for testing (e.g. junit)

In your special case, it looks like the interface is not available in javax.validation tests. Let them not go into javaee-api . If so, add:

  <dependency> <groupId>javax.validation</groupId> <artifactId>validation-api</artifactId> <version>1.0.0.GA</version> <scope>test</scope> </dependency> 

But be careful , your explanation about what is included in the server, and the described behavior sounds strange to me. - I recommend double checking what the server provides and that includes javaee-api . But can I be wrong, and javax.validation is only required to check for explicit

The reason that the problem only occurs when javaee-api may be because javax checking is sometimes only enabled when the implementation is available in the classpath.


The order of dependence depends in some cases. The “problem” is that if the link to libary / dependency is referenced in two places (direct and inderect), and the version of both links to the same libraries is different, Maven must decide which version to use.

The first and most important criterion is the link depth in the dependency tree. If you reference the library directly in your POM project, then this will dominate everything else. If the library refers directly to the library that you directly pointed out, it will dominate everything else, where there is another indirect appeal.

But in case two links (to the same library in different versions) at the same depth of the dependency tree, the first will win. ( more details ).

Primarily,

+5
source

As far as I know, in Maven2 the order of dependencies is not preserved and there is no guarantee how these dependencies appear in the final class path when your code is executed. In your case, I would “exclude” overlapping libraries in your dependency structure.

Look here for Maven exception documents.

Given the complexity of matching the version of the library, I suggest you start with one of the Habernate Maven examples and turn it into your project.

Hope this helps.

0
source

Source: https://habr.com/ru/post/885741/


All Articles