Java 打包jar无效聚合器项目需要pom作为打包

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

Packaging jar is invalid Aggregator project need pom as packaging

javamavenjarjavafx

提问by Anvay

My project has different modules.

我的项目有不同的模块。

  • Each module has a pom.xml which specifies jar packaging.
  • Each pom refers to common parent.
  • In the parent module there is also a pom.xml which includes all the modules.
  • 每个模块都有一个 pom.xml,用于指定 jar 包装。
  • 每个 pom 指的是共同的父级。
  • 在父模块中还有一个包含所有模块的 pom.xml。

When I tried to package using the pom.xml of the parent module, it shows the error - "Packaging jar is invalid Aggregator project need pom as packaging".

当我尝试使用父模块的 pom.xml 进行打包时,它显示错误-“打包 jar 无效聚合器项目需要 pom 作为打包”。

What can I do to make an executable jar of the application from maven?

我该怎么做才能从 maven 制作应用程序的可执行 jar?

回答by Sander Verhagen

Maven requires the parent to be of packagingpom.

Maven 要求父级为packagingpom.

You can make a pomproject behave as if it were a jarproject, by including a bunch of plugin executions and attaching them to their subsequent lifecycle phase. It's not a happy road. On the contrary, the following is.

您可以通过包含一堆插件执行并将它们附加到其后续生命周期阶段来使pom项目表现得像一个jar项目。这不是一条幸福的路。相反,以下

From an object oriented standpoint, what is it that you want? You have one object that is made up out of a bunch of other objects, right? In other words composition, as opposed to inheritance.

从面向对象的角度来看,您想要什么?你有一个由一堆其他对象组成的对象,对吗?换句话说,组合,而不是继承。

Your final delivery is made up out of the other (jar) projects, i.e. the other projects are dependencies of the final delivery project. You will define the other projects each as dependencyso that whomever uses your final delivery knows what (transitive) dependencies to get. Alternatively the final delivery jarcould be packaged up as "uber-jar"and thus contain all its dependencies. That all really depends on how the final delivery is to be used.

您的最终交付由其他 ( jar) 项目组成,即其他项目是最终交付项目的依赖项。您将定义其他项目,dependency以便使用您最终交付的任何人都知道要获得什么(可传递的)依赖项。或者,最终交付jar可以打包为“uber- jar,从而包含其所有依赖项。这一切都取决于如何使用最终交付。

At the same time the following two aspects (may) still exist:

同时以下两个方面(可能)仍然存在:

  • The parent project (which is different than the final delivery project, in fact it may be the parent of the final delivery project also) defines commonalities between its subsequent children, as is what you should expect from inheritance. A child is any project that refers to the parent through the parentconfiguration in its POM.
  • A project that defines modulesthat are to be easily built in one go. Modules are projects that are referred by use of modules.module. This is typically (I guess >99%) done in the parent project, but not necessarily. You could put it in the final delivery project also (without affecting inheritance, because that is thus a different beast), but it's atypical and I would not go there.
  • 父项目(与最终交付项目不同,实际上它也可能是最终交付项目的父项目)定义了其后续子项之间的共性,这也是您对继承的期望。子项是通过parent其 POM 中的配置引用父项的任何项目。
  • 一个定义modules了可以一次性轻松构建的项目。模块是使用modules.module. 这通常(我猜> 99%)在父项目中完成,但不一定。你也可以把它放在最终的交付项目中(不影响继承,因为这是一个不同的野兽),但它是非典型的,我不会去那里。

回答by ben75

To make things short: if your parent-aggregator project don't contains source code (and it's a good practice), just add this to your parent pom.xml:

简而言之:如果您的父聚合器项目不包含源代码(这是一个很好的做法),只需将其添加到您的父 pom.xml 中:

<packaging>pom</packaging>

If the parent project contains source code, I strongly suggest you to:

如果父项目包含源代码,我强烈建议您:

  • move this code in a new module (let's call it commons)
  • make commonsa child module of your parent project
  • add the commonsmodule as a dependency of all other modules requiring it (maybe all of them)
  • add <packaging>pom</packaging>in the parent pom.xml
  • 将此代码移动到新模块中(我们称之为commons
  • 制作commons父项目的子模块
  • 添加commons模块作为所有其他需要它的模块的依赖项(可能是所有模块)
  • 添加<packaging>pom</packaging>父 pom.xml