Java Maven:添加对父 pom 项目的引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19395973/
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
Maven: adding a reference to a parent pom project
提问by trebor
I'm not sure if I'm understanding how to use a parent pom project correctly. I have the following parent pom defined:
我不确定我是否理解如何正确使用父 pom 项目。我定义了以下父 pom:
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>../child1</module>
<module>../child2</module>
</modules>
And then the children pom reference the parent (each child has their own set of dependencies which are not shown):
然后子级 pom 引用父级(每个子级都有自己的一组未显示的依赖项):
<parent>
<groupId>com.example</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../Parent/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>child1</artifactId>
This setup works fine and resolves correctly in eclipse (m2eclipse). I can deploy these to my local repository and end up with the following structure, which should be correct:
此设置工作正常并在 eclipse (m2eclipse) 中正确解析。我可以将这些部署到我的本地存储库并最终得到以下结构,这应该是正确的:
--com
--example
--parent
--0.0.1-SNAPSHOT
--parent-0.0.1-SNAPSHOT.pom
--child1
--0.0.1-SNAPSHOT
--child1-0.0.1-SNAPSHOT.jar
--child1-0.0.1-SNAPSHOT.pom
--child2
--0.0.1-SNAPSHOT
--child2-0.0.1-SNAPSHOT.jar
--child2-0.0.1-SNAPSHOT.pom
My issue is that I now want to reference the parentproject in a differentproject (not parent, child1, or child2) and thus pull in all of the parent's children. I can add a reference to it in my other project:
我的问题是我现在想在不同的项目(不是父、子 1 或子 2)中引用父项目,从而拉入父的所有子项。我可以在我的另一个项目中添加对它的引用:
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>pom</type>
</dependency>
</dependencies>
When doing this the project shows no errors in eclipse but no artifacts are being added to my classpath: not child1, child2, or any of their dependencies.
执行此操作时,项目在 eclipse 中没有显示任何错误,但没有向我的类路径添加任何工件:不是 child1、child2 或它们的任何依赖项。
I keep thinking there must be a way to have a "master" pom project which isn't a jar in itself but has only references to other jars and then be able to reference that "master" somewhere, but I cannot find out how this is accomplished.
我一直认为必须有一种方法来拥有一个“主”pom 项目,它本身不是一个 jar,而是只引用其他 jar,然后能够在某处引用该“主”,但我不知道这是如何实现的完成。
回答by MikeW
Try creating a super pom with same packaging type and add your current parent as a dependency.
尝试创建一个具有相同包装类型的超级 pom 并将您当前的父项添加为依赖项。
回答by khmarbaise
The first thing I would suggest to change your structure of the projects.
我建议的第一件事是改变你的项目结构。
--com
--example
--parent
--0.0.1-SNAPSHOT
--parent-0.0.1-SNAPSHOT.pom
--child1
--0.0.1-SNAPSHOT
--child1-0.0.1-SNAPSHOT.jar
--child1-0.0.1-SNAPSHOT.pom
--child2
--0.0.1-SNAPSHOT
--child2-0.0.1-SNAPSHOT.jar
--child2-0.0.1-SNAPSHOT.pom
I would suggest to go the following way:
我建议采取以下方式:
--parent (pom.xml)
+--child1
+--child2
The parent can be checked in into a version control (Git, SVN) either into the master or into the trunk. Apart from that all childs have to reference the parent like this:
父节点可以被签入到版本控制(Git、SVN)中,可以是主节点也可以是主干节点。除此之外,所有孩子都必须像这样引用父母:
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.example</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>child1</artifactId>
and in your parent you can use things like this:
在你的父母中你可以使用这样的东西:
<groupId>com.example</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>child1</module>
<module>child2</module>
</modules>
Furthermore you can use a company pom just simply by adding a parent reference to your project parent:
此外,您只需向项目父项添加父级引用即可使用公司 pom:
<parent>
<groupId>com.example</groupId>
<artifactId>master-pom</artifactId>
<version>0.0.1</version>
</parent>
<groupId>com.example</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>child1</module>
<module>child2</module>
</modules>
In the company pom you can (and should) pin different plugin versions and define company defaults and suggestions for dependencies with their appropriate versions.
在公司 pom 中,您可以(并且应该)固定不同的插件版本,并使用相应版本定义公司默认值和依赖项建议。
If you like having a company super-pom you simple create a separate maven project which contains only pom file which contains your configurations like plugins, dependencies etc. and of course check it into a version control (Git, SVN etc.). separately from the other project.
如果您喜欢公司的超级 pom,您只需创建一个单独的 maven 项目,该项目仅包含 pom 文件,其中包含您的配置,如插件、依赖项等,当然还可以将其签入版本控制(Git、SVN 等)。与其他项目分开。
If you want to reference a jar file which means in other words an artifact of your multi-module build you have to use the correct groupId, artifactId and version. If you like to use child1 in an other project you need to give the following:
如果你想引用一个 jar 文件,换句话说就是你的多模块构建的工件,你必须使用正确的 groupId、artifactId 和 version。如果您想在其他项目中使用 child1,则需要提供以下内容:
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>child1</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
It is important to understand that the parent of a multi module build is not a an artifact which is usually used, cause based on the packaging type pom
it does not really create an artifact (jar file).
重要的是要了解多模块构建的父级不是通常使用的工件,因为基于打包类型,pom
它并没有真正创建工件(jar 文件)。
Update:You can do that in a limited way only for the dependencyManagement area with the scope import
but this is only intended for using in the depdencyManagement and not for dependencies.
An other solution might be to create a separate dummy project which has the dependencies too two childs as transitive deps. But this is not really beauty.
更新:您只能以有限的方式为具有范围import
的dependencyManagement 区域执行此操作,但这仅用于在depdencyManagement 中使用,而不用于依赖项。另一种解决方案可能是创建一个单独的虚拟项目,该项目具有两个子项作为传递依赖项的依赖项。但这并不是真正的美。
So the solution is simply to add the two or more dependencies directly which you are using in you project and that's it.
因此,解决方案只是直接添加您在项目中使用的两个或多个依赖项,仅此而已。
回答by Raghuram
As @khmarbaise pointed out, the parent of a multi-module build is not usually used as a dependency elsewhere.
正如@khmarbaise 指出的那样,多模块构建的父级通常不用作其他地方的依赖项。
You should be able to add each of your child modules as dependencies and achieve what you wanted to do by adding the parent pom.
您应该能够将每个子模块添加为依赖项,并通过添加父 pom 来实现您想要做的事情。