Java 如何让一个 Maven 模块依赖于另一个?

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

How can I make one Maven module depend on another?

javamaven-2netbeans

提问by Daniel Pryden

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

好吧,我以为我了解如何使用 Maven ......

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

我有一个主项目M具有子项目ABCC包含A和所需的一些常见功能(主要是接口)B。我可以运行mvn compile jar:jar从项目的根目录(该M目录),并得到JAR文件A.jarB.jarC.jar。(所有这些工件的版本目前都是2.0-SNAPSHOT。)

The master pom.xmlfile in the Mdirectory lists Cunder its <dependencyManagement>tag, so that Aand Bcan reference Cby just including a reference, like so:

pom.xml文件中的M目录列表中C的下<dependencyManagement>标签,这样AB可以引用C由只包括一个参考,就像这样:

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

So far, so good. I can run mvn compilefrom 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 Aor Bdirectories and try to run mvn compileI get "Build Error: Failed to resolve artifact."

到现在为止还挺好。我可以从命令行运行mvn compile,一切正常。但是,当我在 NetBeans 中打开该项目时,它会抱怨以下问题:“某些依赖项工件不在本地存储库中”,并且它说缺少的工件是C. 同样,在命令行中,如果我切换到AB目录并尝试运行,mvn compile我会收到“构建错误:无法解析工件”。

I expect I could manually go to where my C.jarwas 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).

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

What am I doing wrong?

我究竟做错了什么?

采纳答案by Pascal Thivent

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).

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

Side note: you shouldn't invoke mvn compile jar:jarbut prefer mvn package. First, running the packagephase will trigger all the phases before package(including compile) and packageitself. Second, running packagewill invoke jar:jar, or war:war, etc depending on the <packaging>value of the project (check the introduction to the lifecyclefor 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 packagephase and Maven will do the job (using the default goals bindings).

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

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

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 项目”图标,表示IDE 知道项目之间的链接。然而,只有当依赖项和项目声明中的 groupId、artifactId 和 version 都匹配时,才会建立这样的链接。经常出现的问题是您更改了库项目中的 API 签名,但应用程序没有启动。通常是由应用程序使用旧版本的库工件这一事实引起的。工件图标可以帮助您追踪这些问题。

回答by Ken Liu

You need to run mvn installinstead of mvn compile. The installgoal will copy the built jar into the local repository after compiling and packaging it. If you only run compile, it will only compile the class files into the targetdirectory and not make them available to other projects.

您需要运行mvn install而不是mvn compile. 该install目标将编译和打包后,它内置的jar复制到本地存储库。如果只运行compile,它只会将类文件编译到target目录中,而不会将它们提供给其他项目。

In Eclipse, if you import a pom from the top level, it will import the subprojects into separate Eclipse projects and set up the dependencies of related projects then set up each project's classpath to depend on others. I'm not familiar with Netbeans but I am pretty sure there is some way to do the same thing.

在 Eclipse 中,如果从顶层导入一个 pom,它会将子项目导入到单独的 Eclipse 项目中并设置相关项目的依赖项,然后设置每个项目的类路径以依赖其他项目。我不熟悉 Netbeans,但我很确定有某种方法可以做同样的事情。

回答by mkleint

netbeans links projects together by the local repository content, so mvn install is necessary in most scenarios.

netbeans 通过本地存储库内容将项目链接在一起,因此在大多数情况下需要 mvn install。

回答by Alfonso Leon

That's not true, performing a deploy will do all the previous setps...

事实并非如此,执行部署将完成之前的所有设置...

Please hava a look at:

请看一看:

http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html

http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html