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

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

问题描述

好的,我想到我明白了如何使用Maven ......

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

我有一个主项目 M 其子项目 A B C C 包含一些常见功能(主要是接口), A B 。我可以从项目根目录( M 目录)运行 mvn compile jar:jar 并获取JAR文件 A.jar B.jar C.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 >< dependencyManagement> 标记,以便 A B 可以引用 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 。同样,从命令行,如果我更改为 A B 目录并尝试运行 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编译jar:jar 但是更喜欢 mvn package 。首先,运行阶段将触发 package 之前的所有阶段(包括 compile )和本身。其次,运行将调用 jar:jar ,或 war:war 等取决于项目的< packaging> 值(查看生命周期简介,了解更多详情)。这是Maven的一大优势:你不需要知道项目是JAR,WAR,EJB等,并且运行适当的目标来打包它。只需运行标准化的阶段,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 install 。这是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和版本都在
中匹配依赖项和项目
声明时,这样的链接只是
。经常出现的
问题是你在库项目中更改了一个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天全站免登陆