Maven 不向 Eclipse 项目添加类路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7071470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 20:26:11  来源:igfitidea点击:

Maven does not add classpath to Eclipse project

eclipsemavenclasspath

提问by Andre Cardoso

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

我有一个 Maven 项目,我正在 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.

我使用 maven eclipse:eclipse 来生成类路径,但是......它从来没有在 eclipse 项目中添加类路径。我试过 maven-eclipse-plugin,我试过 M2Eclipse 插件,但不管我做什么,我无法让类路径条目开始工作。我有很多构建错误,甚至认为 Maven 完美地构建了耳朵。

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:

UPDATE2:这是我的 .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>

采纳答案by Nicola Musatti

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.

Maven Eclipse 插件生成的设置与 m2e/m2eclipse 不兼容。如果您想同时使用 Maven 和 Eclipse,最好的方法是删除由 Maven Eclipse 插件生成的对设置的任何修改,并使用 m2e 导入您的 Maven 项目。

回答by Alexander Pogrebnyak

回答by Stephen

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

您需要使用 build-helper-maven-plugin 将额外的源文件夹添加到 pom.xml。然后运行

mvn eclipse:clean eclipse:eclipse

mvn 日食:清洁日食:日食

For example this pom.xml ..

例如这个 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

<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.

确保文件夹存在,如果路径不正确,它们将不会被添加。在 Maven 日志中查找此内容。

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

[信息] 跳过不存在的资源目录 /tmp/artifact/src/main/resources

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

我的团队发现 eclipse maven 插件不稳定,选择通过 maven 创建我们的整个 eclipse 配置。

回答by Gagan M

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 中。因此,在从命令提示符运行 mvn install 之后。运行以下命令:

mvn eclipse:eclipse

mvn 日食:日食

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

这会将所有 jars 导入您的 eclipse 项目,并将更新类路径。

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.

您需要在第一次向 POM 添加新依赖项并执行 mvn 编译或安装时执行上述命令。

回答by Javahollic

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

除了投票最多的答案之外,您仍然可能会遇到一个损坏的项目,没有“Maven 依赖项”(Kepler),也没有类路径解析。

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.

在我的情况下,当第三方 sdk 更新自身时,我的本地 Maven 存储库 settings.xml 被删除,重启后 eclipse 似乎清除了对该 settings.xml 文件的引用。

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.

一旦我放回文件并在 Maven > Installations > Global Settings: /home/andy/.m2/settings.xml 中引用它,清理每个项目就会启动所有必要的类路径解析。只是分享。

For me, the resulting files looked like:

对我来说,生成的文件如下所示:

.classpath

.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>

回答by james_t

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.Eclipse Run Configuration classpath should show maven dependencies

我遇到的一个问题是在将项目转换为 maven 后,我需要清除运行配置,因为转换不会更新类路径以配置 maven 依赖类路径。Eclipse Run Configuration classpath should show maven dependencies

回答by ptyx

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

这是依靠m2eclipse。当您这样做时,您的所有依赖项都由 maven/m2eclipse 处理,因此您的 .classpath 变得非常小。

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

在项目架构方面,每个 Maven pom 需要一个 Eclipse 项目。每个项目 .classpath 只包含源代码、JRE 和 maven 注入:

<?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.

我怀疑您的许多错误来自依赖项中的重复条目,因为您手动(附加 .classpath 条目)和通过 maven 注入它们。

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

您还需要验证 m2eclipse 中的本地工作区分辨率是否已启用(默认情况下应启用)。