简体   繁体   English

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

[英]In Eclipse m2e, how to reference workspace project?

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. 如果我在pom中设置它们,它将不会在工作空间中引用项目,而是引用本地存储库中的jar。 Quite annoying, anyone can help? 很烦人,有人可以帮忙吗?

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. 仅使用POM文件中的dependencies部分,不要弄乱Eclipse项目引用。 Right-click the project, then select Maven > Update Project Configuration to reset the project to the Maven default settings. 右键单击该项目,然后选择Maven>更新项目配置以将项目重置为Maven默认设置。 This way, m2e has ownership of the dependencies. 这样, 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. 检查每个项目的Maven设置,确保groupIdartifactId版本与您在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. 因此,如果您所依赖的项目在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>更新项目

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. 如果在此之后仍然从文件系统引用了依赖项,请再次检查groupIdartifactId以及每个依赖项的版本

Also check if you don't have any errors in your project - try to run Maven install. 还要检查项目中是否有任何错误 - 尝试运行Maven安装。

I'd go even further than this. 我走得更远。 If you've ever run mvn eclipse:eclipse on your project then you're probably in trouble. 如果你曾经在你的项目中运行过mvn eclipse:eclipse ,那么你可能遇到了麻烦。 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. 我的情况是,我的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 . 我发现最安全的事情是运行mvn eclipse:clean从命令行mvn eclipse:clean然后返回到eclipse,刷新项目,“OK”生成的问题对话框,然后转到Maven> Update Project This sorted it all out for me. 这为我排序了全部。

When eclipse is messed up with importing and deleting several projects, you may need to rebuild index of maven repositories. 当eclipse与导入和删除多个项目混淆时,您可能需要重建maven存储库的索引。 Here is a way that I have done. 这是我做过的一种方式。

  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... 在菜单栏中,单击Window -> Show View -> Other...
    When 'Show View' window pops up, select Maven -> Maven Repositories 弹出“显示视图”窗口时,选择Maven -> Maven Repositories
    In Maven Repositories window, You should see your project as jar file in Local Repositories -> Workspace Projects 在Maven Repositories窗口中,您应该在Local Repositories -> Workspace Projects中将项目视为jar文件
    If you can not find your project in Workspace Projects , right click on Workspace Projects and select Rebuild Index . 如果在Workspace Projects找不到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

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

With kudos to @nwinkler's response, the main problem is matching the version number. 感谢@nwinkler的回复,主要问题是匹配版本号。

A common scenario is that if you are developing a set of projects that are version lock-steped with each other - for example, a project and a set of library projects that are not very loosely coupled, such that a library API might change in a version to be consumed by the relevant app project version, but may change in a way that is incompatible with a past or future version of the app project. 一个常见的情况是,如果您正在开发一组彼此版本锁定的项目 - 例如,项目和一组不是非常松散耦合的库项目,那么库API可能会在相关应用程序项目版本要使用的版本,但可能会以与应用程序项目的过去或未来版本不兼容的方式更改。

The correct way to set Maven dependencies in such a configuration (and it is also the recommended practice) is to have the app consume specific versions of the libraries - so, for example, if you rebuild an old version of the app, it will use the library version that it previously compiled with. 在这样的配置中设置Maven依赖关系的正确方法(也是推荐的做法)是让应用程序使用特定版本的库 - 例如,如果您重建旧版本的应用程序,它将使用它之前编译的库版本。

With the app project's POM library dependency set to a release version (lets say 1.0.0 ), and while working on the next release with both the app and library projects set to a SNAPSHOT release (lets say 2.0.0-SNAPSHOT ), the m2e will not resolve the library version correctly, and will likely download an old version, so that trying to use Eclipse features like "Open Decleration" will target the download jar (sometimes without even a source attachment) which can be pretty annoying. 将app项目的POM库依赖关系设置为发布版本(比如说1.0.0 ),并在下一个版本中同时将应用程序和库项目设置为SNAPSHOT版本(比如说2.0.0-SNAPSHOT ), m2e将无法正确解析库版本,并且可能会下载旧版本,因此尝试使用“Open Decleration”等Eclipse功能将针对下载jar(有时甚至没有源代码附件),这可能非常烦人。

One way to work around that is to set the app POM dependency version to a range, so instead of depending on 1.0.0 , you'd depend on [1.0.0-) . 解决这个问题的一种方法是将应用程序POM依赖关系版本设置为一个范围,因此不依赖于1.0.0 ,而是依赖于[1.0.0-) With an open range like that, m2e will happily find your workspace library project. 有了这样的开放范围,m2e将很乐意找到您的工作区库项目。 But you'd want to set it back to the "correct" version before committing, building and publishing - and this can be very error prone. 但是,在提交,构建和发布之前,您需要将其设置回“正确”版本 - 这可能非常容易出错。

My solution is to use build profiles and set a custom profile for m2e, like this: 我的解决方案是使用构建配置文件并为m2e设置自定义配置文件,如下所示:

  • Set your dependency version with a property, to the version you want to publish against: 使用属性将依赖项版本设置为要发布的版本:
...

<properties>
<my.library.version>1.0.0</my.library.version>
</properties>

<dependencies>
  <dependency>
    <groupId>my.group</groupId>
    <artifactId>my.library</artifactId>
    <version>${my.library.version}</version>
  </dependency>
</dependencies>

...
  • Then add a profile section with an active by default profile that does nothing, and an Eclipse-specific profile that overrides the library version property with a range: 然后添加一个配置文件部分,其默认配置文件没有任何作用,以及一个特定于Eclipse的配置文件,它覆盖带有范围的库版本属性:
...

<profiles>
    <profile>
        <id>default</id>
        <activation><activeByDefault></activeByDefault></activation>
    </profile>

    <profile>
        <id>eclipse</id>
        <properties>
            <my.library.version>[1,)</my.library.version>
        </properties>
    </profile>
</profiles>

...
  • Finally go to your project properties, and under "Maven" type "eclipse" into "Active Maven Profiles": 最后转到你的项目属性,并在“Maven”下输入“eclipse”进入“Active Maven Profiles”:

在此输入图像描述

  • Then "Apply and close". 然后“应用并关闭”。

Eclipse m2e will then always see the version range and will resolve dependencies from the eclipse project (even if you have the library installed in the local Maven repo, as the Eclipse project will have a higher version number), but other builders will see the original, strict, version number. Eclipse m2e将始终看到版本范围,并将解决eclipse项目的依赖关系(即使您在本地Maven仓库中安装了库,因为Eclipse项目将具有更高的版本号),但其他构建器将看到原始版本,严格,版本号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM