Spring and Hibernate dependencies are not being resolved

Viewed 82

I am having this error message for all the imported packages "The import org..... cannot be resolved". and my Spring and Hibernate dependencies are not being resolved. I have tried multiple solutions to fix this error but nothing seams to work, below are the solution I've tried.

  1. Maven => Update Project but same error. Also did => Force Update of Snapshots/Releases
  2. I have also tried to Right-click on the project and choose Properties, and then Maven. Uncheck the box labelled "Resolve dependencies from Workspace projects", hit Apply, and then OK
  3. Deleted my local Maven repo, the .m2 directory and Restarted Eclipse.

https://i.stack.imgur.com/LCUta.png

'''

<modelVersion>4.0.0</modelVersion>
<groupId>com.luv2code.springdemo</groupId>
<artifactId>spring-crm-rest</artifactId>
<version>1.0.0</version>
<packaging>war</packaging>

<properties>
    <springframework.version>5.0.6.RELEASE</springframework.version>
    <hibernate.version>5.4.1.Final</hibernate.version>
    <mysql.connector.version>5.1.45</mysql.connector.version>
    <c3po.version>0.9.5.2</c3po.version>

    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>

    <!-- Spring -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${springframework.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${springframework.version}</version>
    </dependency>
    
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${springframework.version}</version>
    </dependency>

    <!-- Add Jackson for JSON converters -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.9.5</version>
    </dependency>

    <!-- Hibernate -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>${hibernate.version}</version>
    </dependency>

    <!-- MySQL -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>${mysql.connector.version}</version>
    </dependency>

    <!-- C3PO -->
    <dependency>
        <groupId>com.mchange</groupId>
        <artifactId>c3p0</artifactId>
        <version>${c3po.version}</version>
    </dependency>

    <!-- Servlet+JSP+JSTL -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
    </dependency>

    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>javax.servlet.jsp-api</artifactId>
        <version>2.3.1</version>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>


    <!-- to compensate for java 9 not including jaxb -->
    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.3.0</version>
    </dependency>

</dependencies>

<build>

    <finalName>spring-crm-rest</finalName>

    <plugins>

        <!-- Builds a Web Application Archive (WAR) file from the project output 
            and its dependencies. -->
        <plugin>
            <!-- Add Maven coordinates (GAV) for: maven-war-plugin -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>3.2.0</version>
        </plugin>

    </plugins>
</build>
'''
2 Answers

You need as first step to run from terminal in your project root directory the mvn clean install command. In this way you will "install the package into the local repository, for use as a dependency". After that for sure you need to refresh your workspace and run a mvn update

A major problem is apparent from the picture you provided, but I'm not sure how you got it into this state.

By default, Maven projects will have a source directory at "src/main/java" and "src/test/java". Your source directory appears to be just "src", so it's finding classes in "main/java/com/..." so it wants the packages to start with "src.main.java.com...".

You appear to have overridden the settings in the .classpath file somehow. If you're using Maven in Eclipse, you should let the m2e plugin determine the classpath.

From what I can see, if you remove the "src" source directory and replace it with "src/main/java", it will more likely be able to compile your code.

Update:

To answer your question in the comment (this is too long for a comment):

Neither. I would suggest copying the project to a location outside of your workspace, if it isn't already outside of the workspace. If the project was already located outside of the workspace, now delete the project, but DO NOT delete the contents. If the project was located inside the workspace, as you've now copied it out, you can delete the project. At this point, go to the location where the project lies outside of the workspace, and delete the ".classpath" file from that location. Now, import the project back into Eclipse. If your project is conventional otherwise, this will "reset" it to a sane organization with respect to m2e.

Related