如何将 Maven 存储库 jar 添加到 Eclipse 构建路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10972770/
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 to add maven repository jars to eclipse buildpath?
提问by Sara
From eclipse I can see all the necessary jars in maven repositories view. But I have around 100 errors for the missing jar files. So I have set M2_REPO environment variable. I have ran the mvn eclipse:eclipse command from command line. When I run this command an ear package is added to one of subpackages of my project. For example my project name is portal. And sub package is portal_ear. The ear after running mvn eclipse:eclipse command is added to target folder of portal_ear. So my question is as I have the jars already in the repository, how can I add those to the portal buildpath inside eclipse?
从 Eclipse 中,我可以在 Maven 存储库视图中看到所有必需的 jar。但是对于丢失的 jar 文件,我有大约 100 个错误。所以我设置了 M2_REPO 环境变量。我已经从命令行运行了 mvn eclipse:eclipse 命令。当我运行这个命令时,ear 包被添加到我的项目的一个子包中。例如我的项目名称是门户。子包是portal_ear。运行mvn eclipse:eclipse 命令后的ear 被添加到portal_ear 的目标文件夹中。所以我的问题是,因为我的 jar 已经在存储库中,我如何将它们添加到 eclipse 中的门户构建路径中?
Thanks.
谢谢。
采纳答案by Thorbj?rn Ravn Andersen
Install m2e from the eclipse marketplace. You can now import existing maven projects into your workspace or convert existing ones.
从 eclipse 市场安装 m2e。您现在可以将现有的 Maven 项目导入您的工作区或转换现有的项目。
Works very well for Eclipse 3.7
适用于 Eclipse 3.7
回答by mhaller
When you're using M2Eclipse, all you need to do is to make sure your Eclipse project is a Maven project. Select the project, click on "Convert > Convert to Maven project" or "Maven > Enable dependency management".
当您使用 M2Eclipse 时,您需要做的就是确保您的 Eclipse 项目是一个 Maven 项目。选择项目,点击“Convert > Convert to Maven project”或“Maven > Enable dependency management”。
Do not manually add libraries. Also, the M2_REPO library variable is no longer used with m2eclipse, neither is to run the "mvn eclipse:eclipse" command. That is legacy you don't need to do any more.
不要手动添加库。此外,M2_REPO 库变量不再与 m2eclipse 一起使用,也不再运行“mvn eclipse:eclipse”命令。这是您不需要再做的遗产。
You can also import your project by using "File > Import > Maven > Existing Maven project" and M2Eclipse will correctly set up the project's build path.
您还可以使用“文件 > 导入 > Maven > 现有 Maven 项目”导入您的项目,M2Eclipse 将正确设置项目的构建路径。
If you have custom Maven settings, such as an Enterprise Repository, you may want to tell M2Eclipse where to find your settings.xml file, if you do not have it in your user home but in the Maven installation (people do this sometimes). Go to "Window > Preferences > Maven > User Settings" and choose the right settings.xml.
如果您有自定义的 Maven 设置,例如企业存储库,您可能想要告诉 M2Eclipse 在哪里可以找到您的 settings.xml 文件,如果您的用户主页中没有它但在 Maven 安装中(人们有时会这样做)。转到“窗口 > 首选项 > Maven > 用户设置”并选择正确的 settings.xml。
回答by Gene Bo
In my case, I had an existing project that was deployed in Eclipse fine. Thank you Mkyong: http://www.mkyong.com/spring-security/spring-security-hello-world-example/
就我而言,我有一个在 Eclipse 中部署的现有项目。谢谢Mkyong:http://www.mkyong.com/spring-security/spring-security-hello-world-example/
But when I needed to add JSTL for Logout tag support, I didn't see how to add the the jstl.jar file that was just pulled into my repo as another M2_REPO/xyz configuration. Instead, I could only import it as you would a regular .jar file - and that didn't seem right to me.
但是当我需要为 Logout 标记支持添加 JSTL 时,我没有看到如何添加刚刚作为另一个 M2_REPO/xyz 配置拉入我的 repo 的 jstl.jar 文件。相反,我只能像导入常规 .jar 文件一样导入它 - 这对我来说似乎不正确。
After reading a bunch of posts without success, I decided to see if I could get around the Eclipse UI and update the project configuration manually. From the project I opened .classpath in a text editor and added a new entry for the jar file:
在阅读了一堆没有成功的帖子后,我决定看看我是否可以绕过 Eclipse UI 并手动更新项目配置。在项目中,我在文本编辑器中打开了 .classpath 并为 jar 文件添加了一个新条目:
<classpathentry kind="var" path="M2_REPO/javax/servlet/jstl/1.2/jstl-1.2.jar" sourcepath="M2_REPO/javax/servlet/jstl/1.2/jstl-1.2.jar">
<attributes>
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
</attributes>
</classpathentry>
Then I just refreshed my project in Eclipse (no restart required) and ...
然后我只是在 Eclipse 中刷新了我的项目(不需要重新启动),然后......
- the JSTL build error I was having in my JSP page went away
- the build path entry for JSTL was appearing with the same M2_REPO as the other entries
- the deployed code worked fine in Tomcat
- 我在 JSP 页面中遇到的 JSTL 构建错误消失了
- JSTL 的构建路径条目与其他条目显示相同的 M2_REPO
- 部署的代码在 Tomcat 中运行良好
Yes a bit of a hack, but still relatively clean I think - cheers
是的有点黑客,但我认为仍然相对干净 - 欢呼
回答by Marco Paroni
Right click on the project --> Maven --> Update Project...
For me it worked simply like that
右键单击项目 --> Maven --> 更新项目...
对我来说,它的工作原理就是这样