如何让一个 Maven 模块依赖于另一个? [英] How can I make one Maven module depend on another?

查看:26
本文介绍了如何让一个 Maven 模块依赖于另一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我认为我了解如何使用 Maven...

OK, I thought I understood how to use Maven...

我有一个主项目 M,它有子项目 ABC.C 包含一些AB 需要的通用功能(主要是接口).我可以从项目根目录(M 目录)运行 mvn compile jar:jar 并获取 JAR 文件 A.jar, B.jarC.jar.(所有这些工件的版本目前都是 2.0-SNAPSHOT.)

I have a master project M which has sub-projects A, B, and C. C contains some common functionality (interfaces mainly) which is needed by A and B. I can run mvn compile jar:jar from the project root directory (the M directory) and get JAR files A.jar, B.jar, and C.jar. (The versions for all these artifacts are currently 2.0-SNAPSHOT.)

M目录下的主pom.xml文件在其标签下列出了C,以便 AB 可以通过只包含一个引用来引用 C,如下所示:

The master pom.xml file in the M directory lists C under its <dependencyManagement> tag, so that A and B can reference C by just including a reference, like so:

<dependency>
    <groupId>my.project</groupId>
    <artifactId>C</artifactId>
</dependency>

到目前为止,一切都很好.我可以从命令行运行 mvn compile 并且一切正常.但是,当我在 NetBeans 中打开该项目时,它会抱怨以下问题:某些依赖项不在本地存储库中",并说缺少的工件是 C.同样从命令行,如果我更改到 AB 目录并尝试运行 mvn compile 我得到构建错误:失败解决工件."

So far, so good. I can run mvn compile from the command line and everything works fine. But when I open the project in NetBeans, it complains with the problem: "Some dependency artifacts are not in the local repository", and it says the missing artifact is C. Likewise from the command line, if I change into the A or B directories and try to run mvn compile I get "Build Error: Failed to resolve artifact."

我希望我可以手动转到我的 C.jar 的构建位置并运行 mvn install:install-file,但我宁愿找到一个解决方案我直接在 NetBeans 中工作(和/或在 Eclipse 中使用 m2eclipse).

I expect I could manually go to where my C.jar was built and run mvn install:install-file, but I'd rather find a solution that enables me to just work directly in NetBeans (and/or in Eclipse using m2eclipse).

我做错了什么?

推荐答案

Maven 依赖于二进制依赖的概念,并通过本地存储库解决它们.换句话说,如果包之间存在依赖关系,则需要在本地存储库中安装"包,编译和打包代码是不够的.为此,您需要运行 install(这会将包安装到本地存储库中,用作本地其他项目的依赖项).

Maven relies on the concept of binary dependencies and resolves them through the local repository. In other words, you need to "install" packages in your local repository if you have dependencies between them, compiling and packaging code is not enough. And to do so, you need to run install (that will install the package into the local repository, for use as a dependency in other projects locally).

旁注:你不应该调用 mvn compile jar:jar 而是更喜欢 mvn package.首先,运行package阶段会触发package之前的所有阶段(包括compile)和package本身.其次,运行 package 会根据 调用 jar:jarwar:war 等> 项目的价值(查看生命周期简介 了解更多详情).这是 Maven 的一大优势:您不需要知道项目是 JAR、WAR、EJB 等,也不需要运行适当的目标来打包它.只需运行标准化的 package 阶段,Maven 就会完成这项工作(使用默认目标绑定).

Side note: you shouldn't invoke mvn compile jar:jar but prefer mvn package. First, running the package phase will trigger all the phases before package (including compile) and package itself. Second, running package will invoke jar:jar, or war:war, etc depending on the <packaging> value of the project (check the introduction to the lifecycle for more details on this). That's one of the big strength of Maven: you don't need to know if the project is a JAR, a WAR, an EJB, etc and to run the appropriate goal to package it. Just run the standardized package phase and Maven will do the job (using the default goals bindings).

那是 Maven 理论部分.在 IDE 中,为了使使用 Maven 更加方便,情况可能略有不同.IDE 可以使用项目依赖(即对 IDE 内部代码的依赖)而不是二进制依赖,以便在一个项目中所做的更改在其他模块中可见,而无需运行 <代码> mvn 安装.这是 Eclipse + M2Eclipse 的情况.这也适用于以下条件下的 NetBeans(请参阅依赖管理):

That was for the Maven theoretical part. Inside IDEs, things might be slightly different to make working with Maven more convenient. IDEs may use project dependencies (i.e. dependencies on code inside the IDE) instead of binary dependencies so that changes made in one project are made visible in other modules without having to run mvn install. This is the case of Eclipse + M2Eclipse. And this applies also to NetBeans under the following condition (see Dependency Management):

提示:如果您打开一个项目,其他项目取决于,其他中的图标项目更改为Maven 项目"表示 IDE 知道的图标关于项目之间的联系.然而,这样的链接只是当groupId建立时,artifactId 和 version 都匹配依赖项和项目宣言.经常发生问题是您更改了 API在您的库项目中签名,但是应用程序没有回升.往往是因为应用程序正在使用旧版本库工件的.神器图标可以帮助您追踪这些问题.

Hint: If you open a project that other projects depend on, the icon in other projects changes to a "maven project" icon to denote that the IDE knows about link between the projects. However such a link is only established when the groupId, artifactId and version all match in the dependency and project declaration. Frequently occurring problem is that you change an API signature in your library project, but the application is not picking up. Often it's caused by the fact that the application is using an older version of the library artifact. The artifact icon can help you track down these problems.

这篇关于如何让一个 Maven 模块依赖于另一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆