R11G
R11G

Reputation: 1980

Why does organizing imports on my maven projects leads to lot of errors?

Removing unused import is one the best practices. While trying to follow it (Ctrl+Shift+O) I end up getting a lot of errors.

And then I need to resolve those via maven dependencies adding the jar or adding the dependency on the jar.

Eclipse gives me the option of using "Fix Project Setup". Clicking it prompts with a list of jars to add to the build path. How to know which one to use?

What things I can take care to avoid such things ?

Upvotes: 0

Views: 567

Answers (2)

Moritz Petersen
Moritz Petersen

Reputation: 13057

Let's recap what you should have done. Maybe you've done it already, but just to make sure, there is nothing left:

  1. This is how a project looks like, if the Maven support is not enabled (there is no "M" in the project's icon):

    Project without Maven support

  2. To enable Maven support, right-click on the project and enable Maven configuration:

    Enable Maven configuration

  3. Then you see the "M". Now dependencies managed by Maven are automatically recognized by the Eclipse project.

    Project with Maven support

  4. Adding dependencies is done in your pom.xml file, such as:

    <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>myproject</groupId>
      <artifactId>myproject</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
        </dependency>
      </dependencies>
    </project>
    

    Now you have the JUnit dependency in your project.

  5. The dependency also appears in the Package Explorer:

    Project incl. Maven dependeny

    You even see, that the project references one more library, this is because JUnit itself depends on this dependeny. It is recursive.

Upvotes: 2

Rajesh
Rajesh

Reputation: 3014

use maven command to resolve eclipse problems mvn clean install eclipse:clipse if command is suucessful(build success) refresh workspace

Upvotes: 0

Related Questions