[英]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: 正确的方法如下:
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安装。
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.
这是我做过的一种方式。
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
。
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设置自定义配置文件,如下所示:
...
<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>
...
...
<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>
...
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.