在Eclipse m2e中,如何引用工作区项目?

时间:2022-06-01 16:49:19

How can I reference another workspace project using Eclipse m2e?

如何使用Eclipse m2e引用另一个工作区项目?

Do I have to add a project dependency in the project setting? But in that case the dependency is not shown in the pom.

我是否必须在项目设置中添加项目依赖项?但在这种情况下,依赖关系不会显示在pom中。

If I set them in the pom, it will not reference the project in workspace but reference the jar in the local repository. Quite annoying, anyone can help?

如果我在pom中设置它们,它将不会在工作空间中引用项目,而是引用本地存储库中的jar。很烦人,有人可以帮忙吗?

4 个解决方案

#1


54  

The correct way to do this is the following:

正确的方法如下:

  • Use the dependencies section in the POM file exclusively, don't fiddle with the Eclipse project references. Right-click the project, then select Maven > Update Project Configuration to reset the project to the Maven default settings. This way, m2e has ownership of the dependencies.
  • 仅使用POM文件中的dependencies部分,不要使用Eclipse项目引用。右键单击该项目,然后选择Maven>更新项目配置以将项目重置为Maven默认设置。这样,m2e拥有依赖项的所有权。
  • Make sure all referenced projects are open in Eclipse and have the Maven nature enabled.
  • 确保所有引用的项目都在Eclipse中打开并启用了Maven特性。
  • Check the Maven settings for each project, make sure that groupId, artifactId and version match with the projects you have open in Eclipse. So if the project you depend on has version 1.0.0-SNAPSHOT in Eclipse, make sure that the depending project's POM file references version 1.0.0-SNAPSHOT in the dependencies section.
  • 检查每个项目的Maven设置,确保groupId,artifactId和version与您在Eclipse中打开的项目匹配。因此,如果您所依赖的项目在Eclipse中具有版本1.0.0-SNAPSHOT,请确保依赖项目的POM文件引用依赖项部分中的版本1.0.0-SNAPSHOT。
  • Enable Workspace Resolution for each of the projects. Right-click the project, then Maven > Enable Workspace Resolution.
  • 为每个项目启用工作空间分辨率。右键单击项目,然后单击Maven>启用工作空间分辨率。
  • Finally, if the projects are still not resolved, right-click the project again, then Maven > Update Project
  • 最后,如果项目仍未解决,请再次右键单击该项目,然后单击Maven> Update Project

This should solve your problem. If after this, your dependencies are still referenced from the file system, check the groupId, artifactId and especially version of each dependency again.

这应该可以解决您的问题。如果在此之后仍然从文件系统引用了依赖项,请再次检查groupId,artifactId以及每个依赖项的版本。

Also check if you don't have any errors in your project - try to run Maven install.

还要检查项目中是否有任何错误 - 尝试运行Maven安装。

#2


5  

I'd go even further than this. If you've ever run mvn eclipse:eclipse on your project then you're probably in trouble. I had a situation where I had both a "Referenced Libraries" section and a "Maven Dependencies" section in my eclipse project, with conflicting library versions, causing eclipse and myself inevitable confusion.

我会走得更远。如果你曾经在你的项目中运行过mvn eclipse:eclipse,那么你可能遇到了麻烦。我的情况是,我的eclipse项目中有一个“Referenced Libraries”部分和一个“Maven Dependencies”部分,库版本相互冲突,导致eclipse和我不可避免的混淆。

The safest thing I found was to run mvn eclipse:clean from the command line then go back in to eclipse, refresh the project, "OK" the resulting problem dialog, and then go Maven > Update Project. This sorted it all out for me.

我发现最安全的事情是运行mvn eclipse:从命令行清理然后返回到eclipse,刷新项目,“OK”生成的问题对话框,然后转到Maven> Update Project。这为我排序了全部。

#3


4  

When eclipse is messed up with importing and deleting several projects, you may need to rebuild index of maven repositories. Here is a way that I have done.

当eclipse与导入和删除多个项目混乱时,您可能需要重建maven存储库的索引。这是我做过的一种方式。

  1. Check if an referenced project is recognized as a maven project by eclipse properly.

    检查引用的项目是否被eclipse正确识别为maven项目。

    In menu bar, click Window -> Show View -> Other...
    When 'Show View' window pops up, select Maven -> Maven Repositories
    In Maven Repositories window, You should see your project as jar file in Local Repositories -> Workspace Projects
    If you can not find your project in Workspace Projects, right click on Workspace Projects and select Rebuild Index.

    在菜单栏中,单击窗口 - >显示视图 - >其他...当弹出'显示视图'窗口时,选择Maven - > Maven存储库在Maven存储库窗口中,您应该在本地存储库 - >工作区中看到您的项目为jar文件项目如果在Workspace Projects中找不到项目,请右键单击Workspace Projects并选择Rebuild Index。

  2. Update maven of an referencing project

    更新引用项目的maven

    Right click on the referencing project, Maven -> Update Project... -> OK

    右键单击引用项目,Maven - > Update Project ... - > OK

#4


0  

You also need to make sure that you are running the correct goals.

您还需要确保运行正确的目标。

If you don't run the install goal then it won't be copied to your repository and won't compile.

如果您没有运行安装目标,那么它将不会被复制到您的存储库,也不会编译。

To learn more about goals have a look at https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html

要了解有关目标的更多信息,请访问https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html

#1


54  

The correct way to do this is the following:

正确的方法如下:

  • Use the dependencies section in the POM file exclusively, don't fiddle with the Eclipse project references. Right-click the project, then select Maven > Update Project Configuration to reset the project to the Maven default settings. This way, m2e has ownership of the dependencies.
  • 仅使用POM文件中的dependencies部分,不要使用Eclipse项目引用。右键单击该项目,然后选择Maven>更新项目配置以将项目重置为Maven默认设置。这样,m2e拥有依赖项的所有权。
  • Make sure all referenced projects are open in Eclipse and have the Maven nature enabled.
  • 确保所有引用的项目都在Eclipse中打开并启用了Maven特性。
  • Check the Maven settings for each project, make sure that groupId, artifactId and version match with the projects you have open in Eclipse. So if the project you depend on has version 1.0.0-SNAPSHOT in Eclipse, make sure that the depending project's POM file references version 1.0.0-SNAPSHOT in the dependencies section.
  • 检查每个项目的Maven设置,确保groupId,artifactId和version与您在Eclipse中打开的项目匹配。因此,如果您所依赖的项目在Eclipse中具有版本1.0.0-SNAPSHOT,请确保依赖项目的POM文件引用依赖项部分中的版本1.0.0-SNAPSHOT。
  • Enable Workspace Resolution for each of the projects. Right-click the project, then Maven > Enable Workspace Resolution.
  • 为每个项目启用工作空间分辨率。右键单击项目,然后单击Maven>启用工作空间分辨率。
  • Finally, if the projects are still not resolved, right-click the project again, then Maven > Update Project
  • 最后,如果项目仍未解决,请再次右键单击该项目,然后单击Maven> Update Project

This should solve your problem. If after this, your dependencies are still referenced from the file system, check the groupId, artifactId and especially version of each dependency again.

这应该可以解决您的问题。如果在此之后仍然从文件系统引用了依赖项,请再次检查groupId,artifactId以及每个依赖项的版本。

Also check if you don't have any errors in your project - try to run Maven install.

还要检查项目中是否有任何错误 - 尝试运行Maven安装。

#2


5  

I'd go even further than this. If you've ever run mvn eclipse:eclipse on your project then you're probably in trouble. I had a situation where I had both a "Referenced Libraries" section and a "Maven Dependencies" section in my eclipse project, with conflicting library versions, causing eclipse and myself inevitable confusion.

我会走得更远。如果你曾经在你的项目中运行过mvn eclipse:eclipse,那么你可能遇到了麻烦。我的情况是,我的eclipse项目中有一个“Referenced Libraries”部分和一个“Maven Dependencies”部分,库版本相互冲突,导致eclipse和我不可避免的混淆。

The safest thing I found was to run mvn eclipse:clean from the command line then go back in to eclipse, refresh the project, "OK" the resulting problem dialog, and then go Maven > Update Project. This sorted it all out for me.

我发现最安全的事情是运行mvn eclipse:从命令行清理然后返回到eclipse,刷新项目,“OK”生成的问题对话框,然后转到Maven> Update Project。这为我排序了全部。

#3


4  

When eclipse is messed up with importing and deleting several projects, you may need to rebuild index of maven repositories. Here is a way that I have done.

当eclipse与导入和删除多个项目混乱时,您可能需要重建maven存储库的索引。这是我做过的一种方式。

  1. Check if an referenced project is recognized as a maven project by eclipse properly.

    检查引用的项目是否被eclipse正确识别为maven项目。

    In menu bar, click Window -> Show View -> Other...
    When 'Show View' window pops up, select Maven -> Maven Repositories
    In Maven Repositories window, You should see your project as jar file in Local Repositories -> Workspace Projects
    If you can not find your project in Workspace Projects, right click on Workspace Projects and select Rebuild Index.

    在菜单栏中,单击窗口 - >显示视图 - >其他...当弹出'显示视图'窗口时,选择Maven - > Maven存储库在Maven存储库窗口中,您应该在本地存储库 - >工作区中看到您的项目为jar文件项目如果在Workspace Projects中找不到项目,请右键单击Workspace Projects并选择Rebuild Index。

  2. Update maven of an referencing project

    更新引用项目的maven

    Right click on the referencing project, Maven -> Update Project... -> OK

    右键单击引用项目,Maven - > Update Project ... - > OK

#4


0  

You also need to make sure that you are running the correct goals.

您还需要确保运行正确的目标。

If you don't run the install goal then it won't be copied to your repository and won't compile.

如果您没有运行安装目标,那么它将不会被复制到您的存储库,也不会编译。

To learn more about goals have a look at https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html

要了解有关目标的更多信息,请访问https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html