48
votes

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.

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

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

2

2 Answers

73
votes

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:

<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 commons a child module of your parent project
  • add the commons module as a dependency of all other modules requiring it (maybe all of them)
  • add <packaging>pom</packaging> in the parent pom.xml
17
votes

Maven requires the parent to be of packaging pom.

You can make a pom project behave as if it were a jar project, 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.

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 dependency so that whomever uses your final delivery knows what (transitive) dependencies to get. Alternatively the final delivery jar could 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.

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 parent configuration in its POM.
  • A project that defines modules that 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.