当我更改 pom.xml 文件中的依赖项时,如何让我的 Eclipse-Maven 项目自动更新其类路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35511860/
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
How do I get my Eclipse-Maven project to automatically update its classpath when I change a dependency in my pom.xml file?
提问by Dave
I'm using Eclipse Mars with Maven (v 3.3). When I update a dependency in my pom (change the version), my Eclipse-Maven project doesn't pick it up, even when I right click my project, and select “Maven” -> “Update Project.” I know this because I do not see compilation errors in the Eclipse Java editor that I see when I build the project on the command line using
我将 Eclipse Mars 与 Maven (v 3.3) 一起使用。当我更新我的 pom 中的依赖项(更改版本)时,我的 Eclipse-Maven 项目没有选择它,即使我右键单击我的项目并选择“Maven”->“更新项目”。我知道这一点是因为我没有在 Eclipse Java 编辑器中看到编译错误,当我在命令行上使用
mvn clean install
When I remove the project from the workspace and re-import it, then things get back to normal. However this is a cumbersome process. How do I get my Maven-Eclipse project to automatically detect changes in my pom and update the project libraries appropriately?
当我从工作区中删除项目并重新导入它时,事情就会恢复正常。然而,这是一个繁琐的过程。如何让我的 Maven-Eclipse 项目自动检测我的 pom 中的更改并适当地更新项目库?
And yes, in the “Project” menu, “Build Automatically” is checked.
是的,在“项目”菜单中,选中了“自动构建”。
回答by E-Riz
When you import the project into Eclipse, use Eclipse's own built-in Maven support (aka, m2e). I recommend against using mvn eclipse:eclipse
as it doesn't give the best results (as you're seeing). Maven is a build and dependency management tool, not an IDE; expecting it to manage IDE-specific stuff is silly, in my opinion (I realize the Maven team thinks differently, that Maven should be responsible for managing your IDE, but that's nonsense).
将项目导入 Eclipse 时,请使用 Eclipse 自己的内置 Maven 支持(也称为 m2e)。我建议不要使用,mvn eclipse:eclipse
因为它不会提供最佳结果(如您所见)。Maven 是一个构建和依赖管理工具,而不是一个 IDE;在我看来,期望它管理特定于 IDE 的东西是愚蠢的(我意识到 Maven 团队的想法不同,Maven 应该负责管理您的 IDE,但这是无稽之谈)。
So if you have the project available on your system, delete any Eclipse-specific files (typically just .classpath
, .project
, and folder .settings
), they were generated by mvn eclipse:eclipse
and you don't want them interfering with the "proper" import process described here. Then inside Eclipse, use File > Import > Maven > Existing Maven Projects to import the project. That should result in better integration between Eclipse and maven, including automatically updating the Eclipse build path when the pom is changed.
因此,如果您的系统上有可用的项目,请删除任何特定于 Eclipse 的文件(通常只是.classpath
、.project
和 文件夹.settings
),它们是由生成的mvn eclipse:eclipse
并且您不希望它们干扰此处描述的“正确”导入过程。然后在 Eclipse 中,使用 File > Import > Maven > Existing Maven Projects 导入项目。这应该会导致 Eclipse 和 maven 之间更好的集成,包括在 pom 更改时自动更新 Eclipse 构建路径。
As a quick check, after doing the import that way, you should see a group called Maven Dependenciesin the Libraries tab of the project's Build Path (in Properties dialog). Like this:
作为快速检查,以这种方式进行导入后,您应该在项目构建路径(在属性对话框中)的库选项卡中看到一个名为Maven Dependencies的组。像这样:
If you want the Eclipse project configuration to be automatically updated every time the pom is changed, there's a (experimental) setting for that under Preferences> Maven. Be aware that doing so might not be desirable, though - as mentioned in this feature request, it's a somewhat lengthy process that touches a bunch of stuff in the Eclipse Project; doing that automatically on every pom.xml
change could end up being more trouble than it's worth.
如果您希望在每次更改 pom 时自动更新 Eclipse 项目配置,则在Preferences> Maven下有一个(实验性)设置。请注意,这样做可能是不可取的——正如在这个特性请求 中提到的,这是一个有点冗长的过程,涉及到 Eclipse 项目中的一堆东西;在每次pom.xml
更改时自动执行此操作最终可能会带来比其价值更多的麻烦。
回答by Praveen Kumar K S
Three Mandatory checks you should do for automatic update in your classpath
您应该为类路径中的自动更新进行三项强制性检查
- Your Repository is not in-sync with your Eclipse IDE, Please check the below settings in your IDE.
- Right Click your any POM.xml from your IDE and check for the Maven profile which should be auto-activated. Also offline and Force update check box shouldn't be enabled. Please refer the below image.
- Always check for your user settings which should reflect your local maven settings.xml, as shown in the below figure.
- After performing all these checks, refresh your Eclipse Work-space to get these changes reflected.
回答by Teemu Ilmonen
Eclipse should be updating your classpath. If it's not, that implies something is going wrong.
Eclipse 应该更新您的类路径。如果不是,则意味着出现问题。
It's hard to say what the problem could be exactly without knowing more about your project's pom.xml. More information might be necessary to solve the issue, but I'll just make a stab in the dark:
如果不了解项目的 pom.xml 的更多信息,就很难说到底是什么问题。解决问题可能需要更多信息,但我只会在黑暗中刺伤:
Open the .project file in your project's root folder and check the ordering of builders and natures there. It might be possible that some other nature on the project is also causing maven2Nature to fail. Move maven nature up and see if that helps any.
打开项目根文件夹中的 .project 文件,并检查那里的构建器和性质的顺序。项目中的某些其他性质也可能导致 maven2Nature 失败。将 Maven 自然向上移动,看看是否有帮助。
Alternatively you might be thinkingthat Eclipse does not update your dependencies because it does not add some some error indicators in the project that should be there with new dependencies. If that's the case try cleaning the current project (project>clean...). Maven in Eclipse does not necessarily trigger a full rebuild when dependencies are updated.
或者你可能会思考的是,因为它没有在这个项目应该与新的依赖是有添加一些一定的误差指标Eclipse不更新你的依赖。如果是这种情况,请尝试清理当前项目(project>clean...)。更新依赖项时,Eclipse 中的 Maven 不一定会触发完全重建。
If none of this works, closing/opening the project might solve the issue quicker than re-importing.
如果这些都不起作用,关闭/打开项目可能比重新导入更快地解决问题。
回答by iirekm
What you wrote, should work. Did you check this:
你写的,应该有效。你检查过这个吗:
does "pure"
mvn install
from terminal see your changes in POM?maybe some Maybe plugin is buggy, cached some dependencies in
target
, andmvn clean install
is neededyou can run Eclipse in a new workspace, and import your project there, sometimes it helps in case of such strange problems
instead of importing Maven project to Eclipse via
m2eclipse
, you can try to create Eclipse files via the oldmvn eclipse:eclipse
and see what happens thendoes it work well when you try to import your Maven project to other IDE, the free IntelliJ Community Edition for example?
mvn install
终端的“纯”是否看到您在 POM 中的变化?也许某些 Maybe 插件有问题,在 中缓存了一些依赖项
target
,并且mvn clean install
需要您可以在新的工作区中运行 Eclipse,并在那里导入您的项目,有时它有助于解决此类奇怪的问题
m2eclipse
您可以尝试通过旧的方式创建 Eclipse 文件,而不是通过 将 Maven 项目导入到 Eclipse 中,mvn eclipse:eclipse
然后看看会发生什么当您尝试将 Maven 项目导入其他 IDE(例如免费的 IntelliJ 社区版)时,它运行良好吗?
回答by Miguel Velez
As a last resort, you can delete your current Eclipse installation and install a new version. When you add several plugins, they might interfere with one another and create weird behavior. After you do that, do not import your Maven project into your workspace, but rather create a new one and copy and paste the files that you had.
作为最后的手段,您可以删除当前的 Eclipse 安装并安装新版本。当您添加多个插件时,它们可能会相互干扰并产生奇怪的行为。执行此操作后,不要将您的 Maven 项目导入到您的工作区,而是创建一个新项目并复制并粘贴您拥有的文件。