6

I am trying to integrate Maven - Spring MVC - Rest , but i am facing issue in which maven is not picking "spring-web/aop/beans/context/core"(version 4.3.12) dependency .

Instead of spring-web/aop/beans/context/core 4.3.12.RELEASE , maven is picking "spring-web/aop/beans/context/core" 2.5.6.SEC03 dependency. because of this "org.springframework.web.bind.annotation.RequestMapping;" is not getting resolve as it is not there in spring web 2.5.6.SEC03

Maven Version -> apache-maven-3.5.2

Maven Version

Maven env variables Maven env variables

Intellij Maven Dependency

Maven Respository Screenshot

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>org.portal</groupId>
    <artifactId>HumanResourcePortal</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.1.1</version>
                <configuration>
                    <warName>${project.artifactId}-${project.version}</warName>
                    <outputDirectory>${project.basedir}/target</outputDirectory>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-clean-plugin</artifactId>
                <version>2.6.1</version>
                <configuration>
                    <filesets>
                        <fileset>
                            <directory>
                                /home/shivam/apache-tomcat-7.0.81/webapps/${project.artifactId}-${project.version}
                            </directory>
                        </fileset>
                        <fileset>
                            <directory>
                                /home/shivam/apache-tomcat-7.0.81/webapps
                            </directory>
                            <includes>
                                <include>
                                    ${project.artifactId}-${project.version}.{packaging}
                                </include>
                            </includes>
                       </fileset>
                    </filesets>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.0.2</version>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>/home/shivam/apache-tomcat-7.0.81/webapps</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>${project.basedir}/target</directory>
                                    <includes>
                                        <include>${project.artifactId}-${project.version}.${packaging}</include>
                                        <include>${project.artifactId}-${project.version}</include>
                                    </includes>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.3.12.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.12.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.3.12.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.12.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.3.12.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>4.3.12.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.thoughtworks.xstream</groupId>
            <artifactId>xstream</artifactId>
            <version>1.4.9</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-bundle</artifactId>
            <version>1.8</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-core</artifactId>
            <version>1.8</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-server</artifactId>
            <version>1.8</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-client</artifactId>
            <version>1.8</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey.contribs</groupId>
            <artifactId>jersey-spring</artifactId>
            <version>1.8</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.3.11.Final</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.12</version>
        </dependency>
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.1.0.7.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.3.12.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.3.12.RELEASE</version>
        </dependency>
    </dependencies>
</project>

after Running maven dependency tree

after Running maven dependency tree

after running "mvn help:effectivePom" mvn help:effectivePom

shivam
  • 489
  • 2
  • 8
  • 22

2 Answers2

5

Most probably, that outdated Spring version comes from jersey-spring transient dependencies. Try to use one of the latest versions.

Better options?

Well, first of all, you can use <dependencyManagement> section. It is used to lock the versions of the dependencies across the project. <dependencyManagement> will work with any artifacts.

Second, starting somewhere from 3.x releases Spring provides a Bill of Materials. BOM is a special concept in Maven, like a <dependencyManagement>, but it allows to lock multiple dependencies with one BOM import. BOMs are prepared and published for some popular libraries, like Spring or Camel.

madhead
  • 31,729
  • 16
  • 153
  • 201
  • yeah that is because of jersey spring 1.8 . now i removed that dependency . – shivam Nov 04 '17 at 11:08
  • can you please suggest me which version of jersey spring should i use. as on "https://mvnrepository.com/artifact/com.sun.jersey.contribs/jersey-spring" latest version is 1.9 and if i uses that then it again start picking spring jars with version 3.0.0.RC3 – shivam Nov 04 '17 at 11:24
  • Latest version of Jersey is actually `1.19.4`: http://repo1.maven.org/maven2/com/sun/jersey/jersey-core/1.19.4. I am not sure which Spring version it depends on. Just force a version you need with `dependencyManagement` and pray that Jersey will work with it :) – madhead Nov 04 '17 at 11:38
  • ohh sorry.didn't notice 1.19.4 ..i was thinking it 1.1.9.4 – shivam Nov 04 '17 at 11:39
  • on using spring-jersey 1.19 jar again it is picking spring -jar 3.0.0.RC3. – shivam Nov 04 '17 at 15:00
  • which is giving this error "nested exception is java.lang.NoClassDefFoundError: org/springframework/context/event/EventListenerFactory" – shivam Nov 04 '17 at 15:00
  • as this class is not present spring-context 3.0.0.RC3 but is it there in spring-context latest 4.3.12 – shivam Nov 04 '17 at 15:01
  • Use `dependencyManagement` and force versions of Spring Framework! – madhead Nov 04 '17 at 16:27
4

Choosing dependency version that is closer to project root a known behavior of Maven dependency lookup. Let's imagine that you have the following dependency tree:

Your Project
- library-A-1.0.jar
-- library-B-1.5.jar (dependency of library-A)
- library-B-1.2.jar

The library set that maven will pick for you will be library-A-1.0.jar and library-B-1.2.jar (as this version is closer to project root at distance 1 compared to library-B-1.5.jar at disatnce 2).

The usual approach to resolve this kind of issues is to

  1. Analyze your dependency tree with Maven Dependency Plug-in
  2. Either define required library version closer to project root
  3. Or exclude unwanted dependency in included artifact
  4. Or move dependency higher in pom.xml file when at the same distance (first one will be used)
Aleh Maksimovich
  • 2,622
  • 8
  • 19
  • added the output of maven dependency tree plugin. but is is also failing – shivam Nov 04 '17 at 10:09
  • now i am able to run maven dependency tree successfully. that is because of jersey spring 1.8 . now i removed that dependency and now it is loading sping jar 4.3.12. "But it in unable to locate oracle ojdbc jars." can you please tell me which one i should use ? ojdbc6 or ojdbc14 ... and what is the difference among them – shivam Nov 04 '17 at 11:11
  • can you please suggest me which version of jersey spring should i use. as on "https://mvnrepository.com/artifact/com.sun.jersey.contribs/jersey-spring" latest version is 1.9 and if i uses that then it again start picking spring jars with version 3.0.0.RC3 – shivam Nov 04 '17 at 11:25
  • @shivam, it may look like `ojdbc14` is newer version of `ojdbc6` and you should use it, but it's not! Take a look at [this answer](https://stackoverflow.com/a/3212160/750510), which explains crazy Oracle namings. And use `ojdbc6`. – madhead Nov 04 '17 at 11:40