I am trying to wrok with JAX RS service. I have added few dependencies in my pom .xml. Here is the pom.
<properties>
<jersey.version>1.19</jersey.version>
</properties>
<dependencies>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.mvel</groupId>
<artifactId>mvel2</artifactId>
<version>2.2.1.Final</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.0</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>org.apache.drill.exec</groupId>
<artifactId>drill-jdbc-all</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>
when i add this dependency <dependency>
<groupId>org.apache.drill.exec</groupId>
<artifactId>drill-jdbc-all</artifactId>
<version>1.1.0</version>
</dependency>
it fails with elow mentioned exception. I need the above mentioned dependecy for apache drill. On removing and running it works smooth.
The excpetion i am getting is:
java.lang.NoSuchMethodError: org.codehaus.jackson.map.AnnotationIntrospector.pair(Lorg/codehaus/jackson/map/AnnotationIn
trospector;Lorg/codehaus/jackson/map/AnnotationIntrospector;)Lorg/codehaus/jackson/map/AnnotationIntrospector;
at org.codehaus.jackson.jaxrs.MapperConfigurator._resolveIntrospectors(MapperConfigurator.java:155)
at org.codehaus.jackson.jaxrs.MapperConfigurator._setAnnotations(MapperConfigurator.java:133)
at org.codehaus.jackson.jaxrs.MapperConfigurator.getDefaultMapper(MapperConfigurator.java:70)
at org.codehaus.jackson.jaxrs.JacksonJsonProvider.locateMapper(JacksonJsonProvider.java:648)
at org.codehaus.jackson.jaxrs.JacksonJsonProvider.writeTo(JacksonJsonProvider.java:500)
at com.sun.jersey.json.impl.provider.entity.JacksonProviderProxy.writeTo(JacksonProviderProxy.java:160)
at com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:302)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1510)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1419)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1409)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:409)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:558)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:733)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291).
Can some one please help as in why on adding the particular dependency it fails with the above exception.
tried running mvn dependency tree to check the conflict in jars.
Building JAXRS-JSON Maven Webapp 0.0.1-SNAPSHOT
------------------------------------------------------------------------
--- maven-dependency-plugin:2.8:tree (default-cli) # JAXRS-JSON ---
com.java.codegeeks.example:JAXRS-JSON:war:0.0.1-SNAPSHOT
+- com.sun.jersey:jersey-servlet:jar:1.19:compile
| \- com.sun.jersey:jersey-server:jar:1.19:compile
+- com.sun.jersey:jersey-json:jar:1.19:compile
| +- org.codehaus.jettison:jettison:jar:1.1:compile
| +- com.sun.xml.bind:jaxb-impl:jar:2.2.3-1:compile
| | \- javax.xml.bind:jaxb-api:jar:2.2.2:compile
| | +- javax.xml.stream:stax-api:jar:1.0-2:compile
| | \- javax.activation:activation:jar:1.1:compile
| +- org.codehaus.jackson:jackson-core-asl:jar:1.9.2:compile
| +- org.codehaus.jackson:jackson-mapper-asl:jar:1.9.2:compile
| +- org.codehaus.jackson:jackson-jaxrs:jar:1.9.2:compile
| +- org.codehaus.jackson:jackson-xc:jar:1.9.2:compile
| \- com.sun.jersey:jersey-core:jar:1.19:compile
| \- javax.ws.rs:jsr311-api:jar:1.1.1:compile
+- com.sun.jersey:jersey-client:jar:1.19:compile
+- junit:junit:jar:3.8.1:test
\- org.apache.drill.exec:drill-jdbc-all:jar:1.1.0:compile
------------------------------------------------------------------------
BUILD SUCCESS
drill contains too many packages inside it and it collides with other jars in app. I too encountered similar problem but with com.google.common.collect and org.apache.zookeeper packages of drill jar. In my case the version drill is using for these packages are older and so, I just removed those.
zip -d drill-jdbc-all-1.1.0.jar com/google/common/collect/*
zip -d drill-jdbc-all-1.1.0.jar org/apache/zookeeper/*
Now it is working fine. I am not sure how to handle all these using maven only and so opted to delete. It would be better if maven can handle that.
Related
I included my library (mylib) as a maven dependency and from then on, this is thrown at runtime:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/logging/log4j/LogManager
at ...
Caused by: java.lang.ClassNotFoundException: org.apache.logging.log4j.LogManager
at ...
... 6 more
I tried to exclude the log4j dependency from mylib import, but no change happened.
Excerpt from my pom.xml.
</properties>
<slf4j.version>1.7.7</slf4j.version>
<log4j.version>1.2.17</log4j.version>
<mylib.version>1.0.0</mylib.version>
</properties>
<dependencies>
<dependency>
<groupId>cz.mylib.name</groupId>
<artifactId>mylib</artifactId>
<version>${mylib.version}</version>
<exclusions>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4j.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
</dependencies>
Output from mvn dependency:tree
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) # name ---
[INFO] cz.project.name.name:name:jar:1.30.0
[INFO] +- com.github.cverges.expect4j:expect4j:jar:1.6:compile
[INFO] | +- org.apache.servicemix.bundles:org.apache.servicemix.bundles.oro:jar:2.0.8_6:compile
[INFO] | +- com.jcraft:jsch:jar:0.1.50:compile
[INFO] | +- commons-net:commons-net:jar:3.3:compile
[INFO] | \- net.sourceforge.tcljava:com.springsource.tcl.lang.jacl:jar:1.4.1:compile
[INFO] | \- net.sourceforge.tcljava:com.springsource.tcl.lang:jar:1.4.1:compile
[INFO] +- commons-cli:commons-cli:jar:1.2:compile
[INFO] +- org.apache.commons:commons-exec:jar:1.3:compile
[INFO] +- commons-io:commons-io:jar:2.4:compile
[INFO] +- commons-lang:commons-lang:jar:2.6:compile
[INFO] +- cz.mylib.name:mylib:jar:1.0.0:compile
[INFO] | +- org.apache.logging.log4j:log4j-api:jar:2.11.2:compile
[INFO] | +- javax.xml.bind:jaxb-api:jar:2.3.1:compile
[INFO] | | \- javax.activation:javax.activation-api:jar:1.2.0:compile
[INFO] | \- com.opencsv:opencsv:jar:4.6:compile
[INFO] | +- org.apache.commons:commons-lang3:jar:3.8.1:compile
[INFO] | +- org.apache.commons:commons-text:jar:1.3:compile
[INFO] | +- commons-beanutils:commons-beanutils:jar:1.9.3:compile
[INFO] | | +- commons-logging:commons-logging:jar:1.2:compile
[INFO] | | \- commons-collections:commons-collections:jar:3.2.2:compile
[INFO] | \- org.apache.commons:commons-collections4:jar:4.2:compile
[INFO] +- jlibdiff:jlibdiff:jar:1.01:compile
[INFO] +- org.slf4j:slf4j-api:jar:1.7.7:compile
[INFO] +- org.slf4j:jcl-over-slf4j:jar:1.7.7:runtime
[INFO] +- org.slf4j:slf4j-log4j12:jar:1.7.7:runtime
[INFO] +- log4j:log4j:jar:1.2.17:compile
[INFO] \- junit:junit:jar:4.11:test
[INFO] \- org.hamcrest:hamcrest-core:jar:1.3:test
mylib pom.xml
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.11.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.11.2</version>
</dependency>
</dependencies>
CLASSPATH=$APP_HOME/lib/myproject-1.30.0.jar:$APP_HOME/lib/ojdbc6.jar:$APP_HOME/lib/jlibdiff-1.01.jar:$APP_HOME/lib/commons-cli-1.2.jar:$APP_HOME/lib/commons-io-2.4.jar:$APP_HOME/lib/commons-exec-1.3.jar:$APP_HOME/lib/commons-lang-2.6.jar:$APP_HOME/lib/slf4j-api-1.7.7.jar:$APP_HOME/lib/jcl-over-slf4j-1.7.7.jar:$APP_HOME/lib/slf4j-log4j12-1.7.7.jar:$APP_HOME/lib/log4j-1.2.17.jar:$APP_HOME/lib/mylib-1.0.0.jar
The $APP_HOME/lib contains all slf4j and log4j libraries and mylib as well.
My quick quess is, that you have two different versions of log4j on classpath. In dependency tree I see org.apache.logging.log4j:log4j-api:jar:2.11.2, but also log4j:log4j:jar:1.2.17. You have excluded transitional dependency org.apache.logging.log4j:log4j-core from mylib, but log4j-api is still there.
Try to remove the exclusion of org.apache.logging.log4j:log4j-core and also remove log4j:log4j:jar:1.2.17 from project. This way there should be just one version of log4j 2.11.2.
I have the following test dependencies in a maven project where serenity.version is set to 2.0.30
<dependency>
<groupId>net.serenity-bdd</groupId>
<artifactId>serenity-junit</artifactId>
<version>${serenity.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.serenity-bdd</groupId>
<artifactId>serenity-screenplay</artifactId>
<version>${serenity.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.serenity-bdd</groupId>
<artifactId>serenity-screenplay-webdriver</artifactId>
<version>${serenity.version}</version>
<scope>test</scope>
</dependency>
As you can see here all the selenium dependencies of serenity-core-2.0.30 are set to 3.141.59
Running mvn -U dependency:tree -Dincludes=org.seleniumhq.selenium outputs the following
[INFO] --- maven-dependency-plugin:3.0.2:tree (default-cli) # core ---
[INFO] <snip>
[INFO] \- net.serenity-bdd:serenity-junit:jar:2.0.30:test
[INFO] \- net.serenity-bdd:serenity-core:jar:2.0.30:test
[INFO] +- org.seleniumhq.selenium:selenium-server:jar:3.141.59:test
[INFO] | +- org.seleniumhq.selenium:selenium-firefox-driver:jar:3.9.1:test
[INFO] | +- org.seleniumhq.selenium:selenium-ie-driver:jar:3.9.1:test
[INFO] | \- org.seleniumhq.selenium:jetty-repacked:jar:9.4.12.v20180830:test
[INFO] +- org.seleniumhq.selenium:selenium-java:jar:3.9.1:test
[INFO] +- org.seleniumhq.selenium:selenium-api:jar:3.9.1:test
[INFO] +- org.seleniumhq.selenium:selenium-chrome-driver:jar:3.9.1:test
[INFO] +- org.seleniumhq.selenium:selenium-edge-driver:jar:3.9.1:test
[INFO] +- org.seleniumhq.selenium:selenium-remote-driver:jar:3.9.1:test
[INFO] +- org.seleniumhq.selenium:selenium-safari-driver:jar:3.9.1:test
[INFO] +- org.seleniumhq.selenium:selenium-opera-driver:jar:3.9.1:test
[INFO] +- org.seleniumhq.selenium:selenium-support:jar:3.9.1:test
[INFO] \- org.seleniumhq.selenium:htmlunit-driver:jar:2.29.3:test
I have to add the following dependencies to avoid runtime errors - why is version 3.9.1 being used and how do I avoid this?
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-api</artifactId>
<version>3.141.59</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-support</artifactId>
<version>3.141.59</version>
<scope>test</scope>
</dependency>
It turns out that spring-boot-dependencies-2.0.3.RELEASE sets the property selenium.version to 3.9.1 and lists all but the selenium-server artifact in it's dependencyManagement section - setting selenium.version in my POM fixed the issue.
I am trying to compile a Spring application in which the ApplicationConfig file has #EnableAspectJAutoProxy anotation I am facing this error when trying to run the project:
Failed to instantiate [org.springframework.aop.aspectj.annotation.
AnnotationAwareAspectJAutoProxyCreator]:
Constructor threw exception; nested exception is
java.lang.NoClassDefFoundError: org/aspectj/lang/annotation/Around
I have to say when I remove the above annotation the project is running successfully.
Having read on Internet I thought maybe it's because of incompatible versions for of libraries and jar files.
When I run mvn dependency:tree the output is as below:
[INFO] task-segment: [dependency:tree]
[INFO] ------------------------------------------------------------------------
[INFO] [dependency:tree {execution: default-cli}]
[INFO] groupId:myProject:jar:1.0-SNAPSHOT
[INFO] +- junit:junit:jar:4.11:compile
[INFO] | \- org.hamcrest:hamcrest-core:jar:1.3:compile
[INFO] +- org.springframework:spring-webmvc:jar:4.3.0.RELEASE:compile
[INFO] | +- org.springframework:spring-aop:jar:4.2.6.RELEASE:compile (version m
anaged from 4.3.0.RELEASE)
[INFO] | | \- aopalliance:aopalliance:jar:1.0:compile
[INFO] | +- org.springframework:spring-beans:jar:4.2.6.RELEASE:compile
[INFO] | +- org.springframework:spring-context:jar:4.2.6.RELEASE:compile (versi
on managed from 4.3.0.RELEASE)
[INFO] | +- org.springframework:spring-core:jar:4.2.6.RELEASE:compile
[INFO] | +- org.springframework:spring-expression:jar:4.2.6.RELEASE:compile
[INFO] | \- org.springframework:spring-web:jar:4.2.6.RELEASE:compile (version m
anaged from 4.3.0.RELEASE)
[INFO] +- org.aspectj:aspectjweaver:jar:1.8.9:compile
[INFO] +- javax.servlet:javax.servlet-api:jar:3.1.0:compile
[INFO] \- org.aspectj:aspectjrt:jar:1.8.9:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3 seconds
and this is the content of the pom.xml file :
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.9</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.9</version>
</dependency>
</dependencies>
Question: I don't understand this output is saying that everything is fine or there might be some incompatibility in my project, and if there are how to solve them?
If you have worked on maven in your projects for dependency management, then you must have faced one problem at least once or may be more than that. And the problem is version mismatch. It generally happens when you got some dependencies which bring it’s related dependencies together with certain version. And if you have included those dependencies with different version numbers already, they can face undesired results in compile time as well as runtime also.
Ideally to avoid above issue you need to explicitly exclude the related dependency, but it is quite possible that you can forget to do so.
To solve version mismatch issue, you can use the concept of a “bill of materials” (BOM) dependency. A BOM dependency keep track of version numbers and ensure that all dependencies (both direct and transitive) are at the same version.
How to add BOM [Bill Of Materials] dependency
Maven provides a tag dependencyManagement for this purpose. You need to add the bom information in this tag as follows. I am taking the example of Spring bom file.
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>4.3.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
An added benefit of using the BOM is that you no longer need to specify the version attribute when depending on Spring Framework artifacts. So it will work perfectly fine.
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependencies>
Doing like above will resolve all version comparability issues and application works fine as we are not going to use unmatched versions
Following Spring docs, I've created a maven project with the following dependencies :
<dependencies>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.3-1102-jdbc41</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
I'd like to know if Repository data abstraction "goes through" hibernate or not.
From the reference documentation:
28.3 JPA and ‘Spring Data’
The Java Persistence API is a standard technology that allows you to ‘map’ objects to relational databases. The spring-boot-starter-data-jpa POM provides a quick way to get started. It provides the following key dependencies:
Hibernate — One of the most popular JPA implementations.
Spring Data JPA — Makes it easy to easily implement JPA-based repositories.
Spring ORMs — Core ORM support from the Spring Framework.
So the answer is "Yes".
BTW, Spring will create and bootstrap automatically an EntityManagerFactory and DataSource beans by declaring a spring-boot-starter-data-jpa dependency.
If you are unsure which dependencies your project have, you can use the maven-dependency-plugin. Specifically, you can use dependency:tree to get a hierarchal tree view of all dependencies and their transitive dependencies (or dependency:list to get a plain list), e.g.
$ mvn dependency:tree
And to answer your question: Yes, spring-boot-starter-data-jpa does depend on Hibernate:
[INFO] +- org.springframework.boot:spring-boot-starter-data-jpa:jar:1.2.0.RELEASE:compile
[INFO] | +-
[INFO] | +- org.hibernate:hibernate-entitymanager:jar:4.3.7.Final:compile
[INFO] | | +-
[INFO] | | +- org.hibernate:hibernate-core:jar:4.3.7.Final:compile
[INFO] | | | +-
[INFO] | | +-
[INFO] | | +- org.hibernate.common:hibernate-commons-annotations:jar:4.0.5.Final:compile
[INFO] | | +- org.hibernate.javax.persistence:hibernate-jpa-2.1-api:jar:1.0.0.Final:compile
[INFO] | | +-
I am using Spring 4.0 in my JavaEE application, and I tried to use the Spring-data-jpa.
However when I add the Spring-data-jpa dependency, I found that the Spring-data-jpa will depend on Spring-3.x.
Then I wonder this will cause any problem? Since my application will have Spring-4.x with Spring-3.x.
Anyone have the same experience?
Update:
I am using Spring-data-jpg-1.4.3:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.4.3.RELEASE</version>
</dependency>
But when I run mvn dependency:tree I got this:
+- org.springframework:spring-context:jar:4.0.0.RELEASE:compile
| +- org.springframework:spring-beans:jar:4.0.0.RELEASE:compile
| +- org.springframework:spring-core:jar:4.0.0.RELEASE:compile
| | \- commons-logging:commons-logging:jar:1.1.1:compile
| \- org.springframework:spring-expression:jar:4.0.0.RELEASE:compile
+- org.springframework:spring-aop:jar:4.0.0.RELEASE:compile
| \- aopalliance:aopalliance:jar:1.0:compile
+- org.springframework.data:spring-data-jpa:jar:1.4.3.RELEASE:compile
| +- org.springframework.data:spring-data-commons:jar:1.6.3.RELEASE:compile
| +- org.springframework:spring-orm:jar:3.1.4.RELEASE:compile
| | \- org.springframework:spring-jdbc:jar:3.1.4.RELEASE:compile
| +- org.springframework:spring-tx:jar:3.1.4.RELEASE:compile
| +- org.aspectj:aspectjrt:jar:1.7.2:compile
| +- org.slf4j:slf4j-api:jar:1.7.1:compile
| \- org.slf4j:jcl-over-slf4j:jar:1.7.1:runtime
+- org.aspectj:aspectjweaver:jar:1.7.4:compile
+- org.springframework:spring-test:jar:4.0.0.RELEASE:test
It seems that the spring 4.0.. is mixed with spring 3.1.4..
This is an error in the spring-data-jpa 1.4.3.RELEASE pom denepdency.
What actually happens is that it loads spring dependencies that exist in this maven pom instead of the ones you want to import.
The short answer is add this as a parent to your maven project:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.0.0.RC5</version>
</parent>
so that it can inherit the correct dependencies.
Another way to solve this is to use the <exclusions> tag to exclude them and then import the correct dependecies but this takes more time and is not as clean. If you don't want to add spring-boot-start-parent though, then this is how to solve this error.
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.4.1.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</exclusion>
<exclusion>
...
</exclusion>
</exclusions>
</dependency>
For more info see here