Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven jar dependencies are displayed outside `Maven Dependencies` view

I recently started developing a Spring MVC maven project. After I run mvn eclipse:eclipse command from the run configurations all the maven dependencies are displayed outside the project and not under "Maven Dependencies" Hierarchy view. When I switch my project perspective to Java from Java EE then the dependencies are found under Referenced Libararies.

Screenshot

My .project and .classpath metadata file contents.

Anybody got idea on how to display all the jars under the Maven Dependencies view?

like image 348
Lucky Avatar asked Jun 24 '15 15:06

Lucky


Video Answer


1 Answers

I thought, giving an answer might be better than writing comments.

The Maven Eclipse plugin

Assume you have the following project directory:

/hello-world/
   +-- src/
   +-- pom.xml

You now run a mvn eclipse:eclipse inside that directory to eclipsify it. This will create some meta files:

/hello-world/
   +-- src/
   +-- pom.xml
   +-- .project
   +-- .classpath

The .project file will contain a comment line with the statement

NO_M2ECLIPSE_SUPPORT: Project files created with the maven-eclipse-plugin are not supported in M2Eclipse.

The .classpath will have entries for the dependencies like these:

<classpathentry kind="var" path="M2_REPO/com/google/guava/guava/18.0/guava-18.0.jar" sourcepath="M2_REPO/com/google/guava/guava/18.0/guava-18.0-sources.jar"/>
<classpathentry kind="var" path="M2_REPO/junit/junit/4.12/junit-4.12.jar" sourcepath="M2_REPO/junit/junit/4.12/junit-4.12-sources.jar"/>
<classpathentry kind="var" path="M2_REPO/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar" sourcepath="M2_REPO/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3-sources.jar"/>

Note that these entries are using an eclipse internal variable: M2_REPO, which points to your local maven repository. After all these entries are simple library entries. The same that you would get when adding a library to the build path.

If you now import this via "Existing Projects into Workspace", you will exactly get the project that you described. Libraries are simple classpath entries. And - more important - it doesn't have the maven nature (it is missing the little "M" icon). That was mentioned in the .project file. Without the maven nature you do not have some useful and important features for this project.

The Eclipse Maven plugin

So, let's repair that. Make a backup of the project directory first (in case of anything going wrong).

First step is to remove the project from eclipse, but not from the harddisk. So simply delete it in eclipse, but don't tick "Delete project contents on disk". You still want to have this project, don't you?

Step 2: Navigate to the project directory and delete any eclipse meta files (I already told you). Afterwards you have the same project directory as in the initial situation.

Step 3: Now you can import this project into eclipse with "Existing Maven Projects". Browse to the directory that contains your project (might be the workspace directory) and select your project.

You will notice afterwards that this project now has the maven nature. There is a little "M" icon at the project icon. Additionally, you will see all libraries under a "Maven Dependencies" node.

Why is that so? Let's have a look at the meta files again:

The .project file now also contains the following lines (among others):

<buildCommand><name>org.eclipse.m2e.core.maven2Builder</name></buildCommand>
<nature>org.eclipse.m2e.core.maven2Nature</nature>

The .classpath now also has the following entry:

<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
    <attributes>
        <attribute name="maven.pomderived" value="true"/>
    </attributes>
</classpathentry>

And the M2E plugin now is doing the rest. Easy, isn't it?

like image 195
Seelenvirtuose Avatar answered Oct 06 '22 01:10

Seelenvirtuose