Java Maven 项目可以有多个父级吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1636801/
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
Can maven projects have multiple parents?
提问by Wim Deblauwe
We have Java and Flex projects. We currently have 1 base pom that contains the configurations we want to use for both projects. Problem with this is: Flex projects inherit configuration, for example, for javadoc
and pmd
plugins, which is not desirable.
我们有 Java 和 Flex 项目。我们目前有 1 个基本 pom,其中包含我们要用于两个项目的配置。这样做的问题是:Flex 项目继承配置,例如 forjavadoc
和pmd
plugins,这是不可取的。
I want to clean it up and have a real base pom, and then a java-base-pom
and a flex-base-pom
. But how does this work in a multi-module that has both a Flex part and a Java part?
我想清理它并拥有一个真正的基础 pom,然后是 ajava-base-pom
和 a flex-base-pom
。但是,这在同时具有 Flex 部分和 Java 部分的多模块中是如何工作的呢?
We have plugins to our own application where we use the following structure:
我们在自己的应用程序中使用了以下结构的插件:
- my-plugin
- my-plugin-client (flex)
- my-plugin-server (java)
- 我的插件
- 我的插件客户端(弹性)
- 我的插件服务器 (java)
my-plugin
just contains a pom.xml
with <modules/>
section. I would use my-plugin
pom.xml as a parent for both, but then I cannot also use the java base-pom or the flex base-pom as parent. What would be the best approach for this?
my-plugin
只包含一个pom.xml
with<modules/>
部分。我会使用my-plugin
pom.xml 作为两者的父级,但是我不能同时使用 java base-pom 或 flex base-pom 作为父级。什么是最好的方法?
采纳答案by Pascal Thivent
A project can have only one parent (unlike multiple inheritance in C++) but this parent can be part of a bigger parent hierarchy. As pointed out by others, you could thus have something like this:
一个项目只能有一个父级(与 C++ 中的多重继承不同),但该父级可以是更大的父级层次结构的一部分。正如其他人所指出的那样,你可能会有这样的事情:
base-pom/ |-- flex-base-pom | |-- my-plugin-client | | `-- pom.xml | `-- pom.xml |-- java-base-pom | |-- my-plugin-server | | `-- pom.xml | `-- pom.xml `-- pom.xml
That said, I noticed you wrote that your actual problem is that:
也就是说,我注意到您写道,您的实际问题是:
flex projects inherit configuration for javadoc and pmd for example, which they do not want.
flex 项目继承了 javadoc 和 pmd 的配置,例如,他们不想要的。
You should use the pluginManagement
element to avoid this situation:
您应该使用pluginManagement
元素来避免这种情况:
pluginManagementis an element that is seen along side plugins. Plugin Management contains plugin elements in much the same way, except that rather than configuring plugin information for this particular project build, it is intended to configure project builds that inherit from this one. However, this only configures plugins that are actually referenced within the plugins element in the children. The children have every right to override pluginManagement definitions.
pluginManagement是一个与插件一起看到的元素。插件管理以大致相同的方式包含插件元素,除了不是为此特定项目构建配置插件信息,而是旨在配置从该项目构建继承的项目构建。但是,这仅配置在子项的 plugins 元素中实际引用的插件。孩子们有权覆盖 pluginManagement 定义。
So, in the parent pom, configure your plugins in pluginManagement
(javadoc and pmd for example), and reference them within the plugins
element in the desired children (only in my-plugin-server here). This would solve your current issue.
因此,在父 pom 中,在pluginManagement
(例如 javadoc 和 pmd)中配置您的插件,并plugins
在所需子项的元素中引用它们(仅在此处的 my-plugin-server 中)。这将解决您当前的问题。
回答by Romain Linsolas
Just image that pom.xml
are in fact Java classes: you can have only one parent (or extends a class), but this parent can also have another parent, and so on.
只是pom.xml
实际上是 Java 类的图像:您只能有一个父类(或扩展一个类),但是这个父类也可以有另一个父类,依此类推。
As I explained here, you must distinguish the parent and aggregation principles in Maven, which means that my-plugin would be considered as an aggregation project, not necessarily a parent project for both my-plugin-client and my-plugin-parent.
正如我在这里解释的,您必须区分 Maven 中的 parent 和聚合原则,这意味着 my-plugin 将被视为聚合项目,而不一定是 my-plugin-client 和 my-plugin-parent 的父项目。
So to summarize:
所以总结一下:
my-plugin
will define the base pom for all your projects. Then, you create two new pomprojects: java-base-pom
and flex-base-pom
. They have both my-plugin
as parent. Now, my-plugin-client will have java-base-pom
as parent, while my-plugin-server will use flex-base-pom
for his parent.
my-plugin
将为您的所有项目定义基础 pom。然后,您创建两个新的pom项目:java-base-pom
和flex-base-pom
. 他们都my-plugin
作为父母。现在, my-plugin-client 将java-base-pom
用作父级,而 my-plugin-server 将用作flex-base-pom
其父级。
This way, my-plugin-client will inherit all properties defined in the my-plugin
pom.xml, and also from java-base-pom
project.
这样,my-plugin-client 将继承my-plugin
pom.xml 和java-base-pom
项目中定义的所有属性。
回答by David Rabinowitz
The only way is to have base-pom as parent of java-base-pom and flex-base-pom.
唯一的方法是让 base-pom 作为 java-base-pom 和 flex-base-pom 的父级。
I have similar structure for my spring projects:
我的春季项目有类似的结构:
base-pom (basic configuration - eclipse, reports, repositories, etc)
|
+ spring-base-pom (spring definitions)
|
+ spring-jar-base-pom (jar specific definitions)
|
+ spring-war-base-pom (spring web and servlet dependencies)
|
+ spring-webapp-base_pom (spring web mvc dependencies)
回答by LE GALL Beno?t
I've cross this exact proble also, and the best solution I found was to use Inheritance and Aggregation as suggest in this question : does maven support multiple parents (multiple inheritance) ?
我也遇到了这个确切的问题,我发现的最佳解决方案是使用继承和聚合作为这个问题中的建议: maven 支持多个父级(多重继承)吗?
You can have an aggregator pom that is not the parent of the projects it aggregates.
您可以拥有一个聚合器 pom,它不是它聚合的项目的父级。
and explain in the Maven Documentation
并在Maven 文档中解释
Inheritance and aggregation create a nice dynamic to control builds through a single, high-level POM (...) Conversely, a POM project may aggregate projects that do not inherit from it.
继承和聚合创建了一个很好的动态来控制通过单个高级 POM 的构建(...)相反,POM 项目可能会聚合不从它继承的项目。
From this I had my POMs inheritance (pom-master contains communes configurations, and each children the specifics ones) :
从此我有了我的 POM 继承(pom-master 包含公社配置,每个孩子都有具体的配置):
pom-master |-- pom-java |-- pom-flex
and so my project can get the specifics for each modules configurations as wished :
所以我的项目可以根据需要获得每个模块配置的细节:
project (aggregate project-flex & project-java) |-- project-java | `-- pom.xml => parent = pom-java |-- project-flex | `-- pom.xml ==> parent = pom-flex `-- pom.xml => parent = pom-master
Hope it will help others as well :)
希望它也能帮助其他人:)
回答by sibidiba
You can achieve multiple inheritance with profiles:
您可以使用配置文件实现多重继承:
You create (multiple) profiles in the root pom, and auto activate any variation of these profiles achieves multiple inheritance of maven configuration.
您在根 pom 中创建(多个)配置文件,并自动激活这些配置文件的任何变体以实现 maven 配置的多重继承。
回答by Peter Szanto
Even though maven projects have single parent, they can import any number of other pom's like this:
即使 Maven 项目只有单亲,它们也可以像这样导入任意数量的其他 pom:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>my-shared-dependencies</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
This has two important differences compared to a parent:
与父级相比,这有两个重要区别:
- Plugins defined in the imported pom won't be imported
- Dependencies defined in the imported pom won't be added to the current pom, it will only import dependencies into the dependency management section
- 导入的 pom 中定义的插件不会被导入
- 导入的 pom 中定义的依赖不会被添加到当前的 pom 中,它只会将依赖导入到依赖管理部分
However if your parent pom has a < dependencies> section and you want to include those into your dependencies then you can add the parent to the dependencies section just like a regular dependency :
但是,如果您的父 pom 有一个 <dependencies> 部分并且您想将它们包含在您的依赖项中,那么您可以像常规依赖项一样将父项添加到依赖项部分:
<dependency>
<groupId>org.example</groupId>
<artifactId>my-shared-dependencies</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
Even though the same dependency is already imported, the version tag has to be specified again. To reduce duplication, it can be stored in a property
即使已经导入了相同的依赖项,也必须再次指定版本标签。为了减少重复,它可以存储在一个属性中