java - Maven 项目编译但工作区中缺少导入

标签 java eclipse maven dependencies m2e

我正在使用带有 m2e 插件的 eclipse Kepler,我想修改一个 maven 项目并编译它。我首先使用 Configure -> Convert to maven project 将其转换为 maven 项目,但仍然缺少许多导入。当我使用 Run as... -> Maven install 时,项目编译。

如何修复丢失的导入?

最佳答案

告诉m2e更新项目:项目的上下文菜单/Maven/更新项目...

然后 m2e 将再次读取 POM 并更新类路径并再次构建项目。

确保你的 .classpath 文件中有这个:

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

注意:实际语法可能会略有不同,具体取决于您使用的 m2e 版本。

另一个常见的陷阱是提供的范围:

provided
This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive.

注意最后一部分:不可传递

这意味着您依赖于 POM A,它表示“我需要 X:Y:Z,范围已提供”。您的项目将看到此依赖项,但它根本不会在您的类路径中。

要解决此问题,请将依赖项复制到具有相同作用域的项目中(已提供)。

关于java - Maven 项目编译但工作区中缺少导入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18445625/

相关文章:

java - 为 JTable 中的每一行添加到数据库

java - 如何排除maven中的lib文件夹?

maven - Zeppelin 不加载 Maven jar

java - 保存的数据与数据库内容不对应SQLite litepal

Java 接口(interface)实现对

java - JFrame 在 Java Swing GUI 上显示为空

Eclipse 自动保存工作区

android - Eclipse 导出的 apk 崩溃

java - 如何通过maven在远程tomcat上自动部署?

java - 如何使用字符串或整数以外的数据类型启动 Intent?