Maven: hibernate-entitymanager together with javaee-api break my unit tests
I'm having the two dependencies javaee-api
and hibernate-entitymanag开发者_高级运维er
in my pom. But they don't work very well together: as soon as I add javaee-api, all my unit tests break 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 that?
(this question has been edited in order to fit the problem ;))
Maven Dependencies have no order , but the provide the concept of scopes.
So what you need to do is, use the scopes to build the right set of dependencies for:
- compile time
- runtime in the server: (use for example
provided
for dependencies that are needed at compiletime, but will be provided by the server, so your Application does/must not contain them - test time: use the
test
scope to add dependencies that are only needed for testing (junit for example)
In your special case it looks like that javax.validation interface libary is not aviable in the tests. May they are not incuded in javaee-api
. If this is the case, then 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 behaviour sounds strange to me. - I recommend to double check what the server provides and what javaee-api
relay includes. But may I am wrong, and javax.validation
is only needed for test explicit
The reason why the problem only occoures when javaee-api
is included, could be, that javax validation sometimes is only turned on, when a implementation is aviable in the classpath.
The order of dependency matter in some cases. The "problem" is that if a libary/dependency is referenced in two places (direct and inderect) and the version of both references to the same library differers, Maven has to decide which version to use.
The first and most important criterium is the depth of the reference in the dependency tree. If you reference the library directly in your project POM, then this will be dominate all other. If the library is refered directly by a libary that is refered direcly by you, then this will dominate all other where is one more indirection.
But in case where are two references (to the same library in different versions) in the same depth of the dependency tree, the first one will win. (more details).
First of all,
This is caused by the fact that the java-ee-api.jar contains crippled classes. There are alternative dependencies around which fix this problem. Changing the order in the pom.xml did the trick for me, too.
As far as I know, in Maven2 the order of the dependency is not kept and there is no guarantee on the order of how those dependencies appear in the final class path when you execute your code. In your case, I would "exclude" overlapping libraries in your dependency structure.
Look here for the Maven exclusion docs.
Given the complexity of library version matching I would suggest you start off with one of the Hibernate Maven examples, and morph it into your project.
I hope this helps.
精彩评论