Maven does not add classpath to Eclipse project

2020-02-09 00:11发布

问题:

I have a maven project, that I'm working on Eclipse.

I use maven eclipse:eclipse to generate the classpath, but ... it NEVER adds the classpath on the eclipse project. I've tried the maven-eclipse-plugin, I've tried the M2Eclipse plugin, but it doesn't matter what I do, I can't get the classpath entries to start working. I have many build errors, even thought the maven builds the ear perfectly.

Any guidelines?

Thanks for any answer!

UPDATE: Here's my root classpath:

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" path="core/src/main/java"/>
    <classpathentry excluding="**" kind="src" output="target/classes" path="core/src/main/resources"/>
    <classpathentry kind="src" path="client/src/main/java"/>
    <classpathentry kind="src" path="client/src/main/resources"/>
    <classpathentry kind="src" path="junit_server/src/main/java"/>
    <classpathentry kind="src" path="initializer/src/main/java"/>
    <classpathentry kind="src" path="initializer/src/main/webapp"/>
    <classpathentry kind="src" path="site/src/main/webapp"/>
    <classpathentry kind="src" path="core/src/test/java"/>
    <classpathentry kind="src" path="core/src/test/resources"/>
    <classpathentry exported="true" kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
    <classpathentry exported="true" kind="var" path="M2_REPO"/>
    <classpathentry kind="output" path="target/classes"/>
</classpath>

UPDATE2: This is my .project:

<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
    <name>coreisp_back</name>
    <comment></comment>
    <projects>
    </projects>
    <buildSpec>
        <buildCommand>
            <name>org.eclipse.jdt.core.javabuilder</name>
            <arguments>
            </arguments>
        </buildCommand>
        <buildCommand>
            <name>org.eclipse.m2e.core.maven2Builder</name>
            <arguments>
            </arguments>
        </buildCommand>
    </buildSpec>
    <natures>
        <nature>org.eclipse.m2e.core.maven2Nature</nature>
        <nature>org.eclipse.jdt.core.javanature</nature>
    </natures>
</projectDescription>

回答1:

The setup generated by the Maven Eclipse plugin is not compatible with m2e/m2eclipse. If you want to use Maven and Eclipse together your best course is to remove any modification to your setup generated by the maven eclipse plugin and use m2e to import your Maven project.



回答2:

  • Upgrade to Eclipse Indigo ( 3.7 )
  • Install m2e plugin ( http://www.eclipse.org/m2e/download/ ).
  • Use File->Import->Existing Maven Projects


回答3:

You need to add the extra source folders to the pom.xml with the build-helper-maven-plugin. Then run

mvn eclipse:clean eclipse:eclipse

For example this pom.xml ..

<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>mygroup</groupId>
    <artifactId>artifact</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>artifact</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>1.7</version>
                <executions>
                    <execution>
                        <id>add-source</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>client/src/main/java</source>
                            </sources>
                        </configuration>
                    </execution>
                    <execution>
                        <id>add-resource</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>add-resource</goal>
                        </goals>
                        <configuration>
                            <resources>
                                <resource>
                                    <directory>client/src/main/resources</directory>
                                    <targetPath>resources</targetPath>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Generates this .classpath

<classpath>
  <classpathentry kind="src" path="src/test/java" output="target/test-classes" including="**/*.java"/>
  <classpathentry kind="src" path="src/main/java" including="**/*.java"/>
  <classpathentry kind="src" path="client/src/main/java" including="**/*.java"/>
  <classpathentry kind="src" path="client/src/main/resources" excluding="**/*.java"/>
  <classpathentry kind="output" path="target/classes"/>
  <classpathentry kind="var" path="M2_REPO/junit/junit/3.8.1/junit-3.8.1.jar"/>
  <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
</classpath>

Make sure the folders exist, if the path is incorrect they wont be added. Look for this in the maven log.

[INFO] skip non existing resourceDirectory /tmp/artifact/src/main/resources

My team found the eclipse maven plugins to be unstable, opting to create our entire eclipse config through maven.



回答4:

This is relying on m2eclipse. When you do that, all your dependencies are handled by maven/m2eclipse, so your .classpath becomes very minimal.

In term of project architecture, you want one eclipse project per maven pom. Each project .classpath contains only the sources, JRE and maven injection:

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" output="target/classes" path="src/main/java"/>
    <classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
    <classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
    <classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
    <classpathentry kind="output" path="target/classes"/>
</classpath>

(adjust depending where your sources are).

I suspect your many errors come from duplicate entries in the dependencies because you inject them both manually (additional .classpath entries) and through maven.

You'll also want to verify local workspace resolution in m2eclipse are enabled (should be on by default).



回答5:

By default the mvn installs are not imported into eclipse automatically. So, after you run mvn install from the command prompt. Run the following command:

mvn eclipse:eclipse

This will import all the jars into your eclipse project and will update the classpath as well.

You need to execute the above command the very first time you add a new dependency to your POM and do an mvn compile or install.



回答6:

In addition to the most voted answer, you can still be left with a broken project, no 'Maven Dependencies' (Kepler) and no classpath resolution.

In my case my local maven repository settings.xml got deleted when a third party sdk updated itself, after a restart eclipse seemed to clear out the reference to that settings.xml file.

Once I put the file back and referred to it in Maven > Installations > Global Settings: /home/andy/.m2/settings.xml , cleaning each project kick-started all the necessary classpath resolution. just sharing.

For me, the resulting files looked like:

.classpath

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" output="target/classes" path="src/main/java"/>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
    <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"/>
    <classpathentry kind="output" path="target/classes"/>
</classpath>

.project

<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
    <name>jemh-ao</name>
    <comment></comment>
    <projects>
    </projects>
    <buildSpec>
        <buildCommand>
            <name>org.eclipse.jdt.core.javabuilder</name>
            <arguments>
            </arguments>
        </buildCommand>
        <buildCommand>
            <name>org.eclipse.m2e.core.maven2Builder</name>
            <arguments>
            </arguments>
        </buildCommand>
    </buildSpec>
    <natures>
        <nature>org.eclipse.jdt.core.javanature</nature>
        <nature>org.eclipse.m2e.core.maven2Nature</nature>
    </natures>
</projectDescription>


回答7:

a problem I run into is after converting a project to maven I need to clear the Run Configuration as conversion does not update the classpath to configure maven-dependency classpaths.