我刚刚从subversion导入了一个项目到IntelliJ IDEA 11——这是一个maven项目。但是我在maven库依赖关系中有一个问题,所以我不能自动包括所有maven依赖关系- IDEA只在我打开该类时显示依赖错误/这就是我在这里得到的:

所以我想要自动添加所有依赖项-这是可能的还是我必须遍历所有类文件来识别和添加maven依赖项?!

更新:在做了一些修改之后,我发现了如何以某种方式解决我的问题。我就是这么做的:

但我认为从逻辑上讲,它不会包括和检查新的依赖关系?!在intelliJ - auto export dependencies to classpath中是否有设置区域?


当前回答

如果多次导入相同的路径,则导入Maven依赖项可能无法工作。这可能在导入现有maven项目后自动发生。我不知道为什么会发生这种情况,所以我倾向于认为这是一个bug。

例如,如果我的项目是

hibernate
  src/main
  src/test

这三条路径可以作为顶部节点导入:

hibernate
src/main
src/test

如果是这样的话,依赖关系看起来是正确的,但是除非删除多余的路径(在本例中,是src/main和src/test),否则不会使用它们。完成此操作后,刷新并单击Build > Rebuild Project。IDEA将拾取依赖项。

此问题的一个症状是,当您手动检查库时,IDEA会警告您有重复的路径(问题中的第二个屏幕截图)。

如果需要的话,有几种方法可以重新读取依赖项。如果您转到“Maven Projects”选项卡,在该选项卡的左上角有一个“Reimport All Maven Projects”图标。如果你怀疑IDEA变得困惑,你可以点击文件>无效缓存。

其他回答

如果其他方法都失败了,请检查本地.m2存储库中的jar文件是否确实有效且没有损坏。在我的例子中,文件还没有完全下载。

尝试从IntelliJ IDEA中的Maven Projects面板中重新导入项目。它应该自动下载并配置pom.xml中定义的所有依赖项。

如果由于某种原因,从IDEA下载不能工作,那么可以从命令行尝试mvn install,看看是否可以获取依赖项。

当然,所有必需的依赖项和任何自定义存储库都必须在pom.xml文件中直接定义。

我能够通过在pom.xml文件中的构建标记后添加这行代码来修复我的问题,我从我的运行项目中进行比较,发现这是不同的,现在我都很好。

<repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </pluginRepository>
    </pluginRepositories>

从项目目录中删除.idea文件夹,然后将项目作为Maven项目重新导入,这对我来说是可行的。

From maven tab click + and choose pom.xml From maven tab click download sources and documentation On project structure(where you can view project files/directories) right click the project you're trying to build and choose Build Module Project Name. From tab Run- Edit Configurations with + add Application and fill the below fields: i. Main Class- Manually choose from Project tab select the main class ii. Use classpath of module - choose the application name iii. Shorten command line - classpath file Now simply run the app.