Java 将引用的 Eclipse 项目添加到 Maven 依赖项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/136362/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 08:43:50  来源:igfitidea点击:

Adding referenced Eclipse projects to Maven dependencies

javaeclipsemaven-2

提问by rgcb

Right now, I have two Eclipse projects - they both use Maven 2 for all their jar-dependency goodness.

现在,我有两个 Eclipse 项目——它们都使用 Maven 2 来实现它们所有的 jar 依赖项。

Inside Eclipse, I have project Foo included in project Bar's build path, so that I can use Foo's classes from project Bar. This works really well in Eclipse land, but when I try:

在 Eclipse 中,我将项目 Foo 包含在项目 Bar 的构建路径中,以便我可以使用项目 Bar 中的 Foo 类。这在 Eclipse 领域非常有效,但是当我尝试时:

mvn compile 

inside Bar's directory, it fails because Maven doesn't know about the project-to-project relationship in Eclipse's build path.

在 Bar 的目录中,它失败了,因为 Maven 不知道 Eclipse 构建路径中的项目到项目关系。

If I were using Ant, I would just use it to do something silly like copy foo.jar into project Bar's classpath, but as far as I can tell, things are done a lot less hackishly in Maven-land.

如果我使用 Ant,我只会用它来做一些愚蠢的事情,比如将 foo.jar 复制到项目 Bar 的类路径中,但据我所知,在 Maven-land 中做的事情要少得多。

I'm wondering if there's a standard workaround for this type of problem - it seems like it would be fairly common, and I'm just missing something basic about how Maven works.

我想知道是否有针对此类问题的标准解决方法 - 看起来它相当普遍,而且我只是缺少有关 Maven 工作原理的一些基本知识。

采纳答案by Pablo Fernandez

Maybe you are referencing the other project via Eclipse configure-> build path only. This works as long as you use Eclipse to build your project.

也许您仅通过 Eclipse configure-> build path 引用另一个项目。只要您使用 Eclipse 来构建您的项目,这就会起作用。

Try running first mvn installin project Bar (in order to put Bar in your Maven repository), and then add the dependency to Foo's pom.xml.

尝试先mvn install在项目 Bar 中运行(为了将 Bar 放在您的 Maven 存储库中),然后将依赖项添加到 Foo 的 pom.xml。

That should work!.

那应该工作!

回答by Chris Vest

I think the best way to handle it is to make Bar a Maven project just like Foo, and then mvn installit so it is available in your local Maven repository. The drawback is that you have to install that project every time you want Maven to see the changes you make to Bar.

我认为处理它的最好方法是让 Bar 成为一个 Maven 项目,就像 Foo 一样,然后mvn install它就可以在您本地的 Maven 存储库中使用。缺点是每次您希望 Maven 看到您对 Bar 所做的更改时都必须安装该项目。

回答by Stephen Denne

Not a complete answer:
Bar's pom needs to include Foo in order to use maven to compile it.
I'm interested in this question too, but from the perspective of how to get eclipse to recognise a maven-added dependency is actually another project in the same workspace. I currently alter the build path after performing mvn eclipse:eclipse

不是一个完整的答案:
Bar 的 pom 需要包含 Foo 才能使用 maven 编译它。
我也对这个问题感兴趣,但从如何让 eclipse 识别 maven 添加的依赖项的角度来看,实际上是同一工作区中的另一个项目。我目前在执行后更改构建路径mvn eclipse:eclipse

回答by Micke

Check out the m2eclipseplugin. It will automatically and dynamically update the project build path when you change the pom. There is no need for running mvn eclipse:eclipse.

查看m2eclipse插件。当您更改 pom.xml 时,它将自动动态更新项目构建路径。没有必要运行mvn eclipse:eclipse.

The plugin will also detect if any dependency is in the same workspace and add that project to the build path.

该插件还将检测是否有任何依赖项在同一工作区中,并将该项目添加到构建路径中。

Ideally, if you use m2eclipse, you would never change the project build path manually. You would always edit pom.xml instead, which is the proper way to do it.

理想情况下,如果您使用 m2eclipse,您永远不会手动更改项目构建路径。您将始终改为编辑 pom.xml,这是正确的做法。

As has been previously stated, Maven will not know about the Eclipse project build path. You do need to add all dependencies to the pom, and you need to make sure all dependencies are built and installed first by running mvn install.

如前所述,Maven 不会知道 Eclipse 项目的构建路径。您确实需要将所有依赖项添加到 pom,并且您需要确保首先通过运行mvn install.

If you want to build both projects with a single command then you might find project aggregationinteresting.

如果您想使用单个命令构建两个项目,那么您可能会发现项目聚合很有趣。

回答by alexguev

You might want to try an alternative approach, where you have a parent maven project and two children project. let's say:

您可能想尝试另一种方法,即您有一个父 Maven 项目和两个子项目。让我们说:

Parent (pom.xml has references to both children projects/modules) --> A (depends on B) --> B

父(pom.xml 引用了两个子项目/模块)--> A(取决于 B)--> B

then when you run mvn eclipse:eclipse from the root of Parent, maven will generate eclipse projects for A and B, and it will have B as a required project in the classpath of A.

那么当你从 Parent 的根目录运行 mvn eclipse:eclipse 时,maven 会为 A 和 B 生成 eclipse 项目,并且它会在 A 的 classpath 中将 B 作为必需的项目。

You can run mvn install from the root of Parent to get both projects to compile.

您可以从 Parent 的根目录运行 mvn install 来编译两个项目。

To complete your setup, you'll have to import both A and B into Eclipse, making sure you don't check "Copy projects into workspace".

要完成您的设置,您必须将 A 和 B 都导入 Eclipse,确保您没有选中“将项目复制到工作区”。

回答by Andreas Dietrich

If you reference a local project, but its version has been updated (usually increased), it could maybe only be found in your local repo and you have to update the (likely fixed) version of it in your POM(s).

如果你引用了一个本地项目,但它的版本已经更新(通常是增加了),它可能只能在你的本地仓库中找到,你必须在你的 POM 中更新它的(可能已修复)版本。



We have a "common project" (used everywhere) which does not necessarily need to be versioned since we tag it via source control. so either

我们有一个“通用项目”(随处使用),它不一定需要版本化,因为我们通过源代码控制对其进行标记。所以要么

  • keeping it at a fixed version or
  • referencing it with the special LATESTversion
  • 将其保持在固定版本或
  • 用特殊LATEST版本引用它

are good workarounds to always be on the safe side.

是始终处于安全状态的好方法。

回答by Tezra

I just needed to do this and I needed it to build with the external mvn clean install command. Here is the proper way to configure this in Eclipse. (With project B as a dependency of A)

我只需要这样做,我需要使用外部 mvn clean install 命令来构建它。这是在 Eclipse 中配置它的正确方法。(将项目 B 作为 A 的依赖项)

  1. Open the pom.xml for project A in Eclipse.
  2. Go to the Dependenciestab.
  3. Click the Add...button in the middle of the page (for the left side Dependencies box)
  4. In the popup, there should be a box under a line with text above it saying Enter groupId, artifactId or sha1 prefix or pattern (*):. Enter the artifact ID for project B into this box.
  5. Double click the jar you want to add as a dependency to this project
    1. You may need to update the project after.
    2. Right click project A in you Package explorer
    3. Maven -> Update Project...
    4. Then hit OK in the popup.
  1. 在 Eclipse 中打开项目 A 的 pom.xml。
  2. 转到Dependencies选项卡。
  3. 单击Add...页面中间的按钮(对于左侧的依赖项框)
  4. 在弹出窗口中,一行下方应该有一个框,上面有文字说Enter groupId, artifactId or sha1 prefix or pattern (*):。在此框中输入项目 B 的工件 ID。
  5. 双击要作为依赖项添加到此项目的 jar
    1. 之后您可能需要更新项目。
    2. 右键单击包资源管理器中的项目 A
    3. Maven -> 更新项目...
    4. 然后在弹出窗口中点击确定。