Packaging jar is invalid Aggregator project need pom as packaging - java

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?

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

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.

Related

Maven self-contained child artifact

I have a project which has 3 pom files: parent file (very basic one, just declares its children), main pom for building project itself and a pom file for generating swagger client library. The client artifact is getting downloaded into our Nexus.
The problem is that when I want to use a client library in another project as a dependency it also requires a parent artifact. I don't want to download it into Nexus since it's so basic and will only flood the repository. I've tried packing some kind of an uber-jar, but it doesn't work for me - jar is huge and contains all dependencies, yet it still needs parent artifact. Are there any workarounds?
While I generally would not care about having an additional Parent POM in the repository (our repository contains 2000 different self-created artifacts in various versions, it is not "flooded"), you can have a look at the
https://www.mojohaus.org/flatten-maven-plugin/
which allows you to make your pom smaller, and includes the possibility to get away without a Parent POM.

Can we use user defined classes defined in parent project into child project maven?

We can inherit dependencies from parent project into child project in maven. But I am no able to use classes defined in parent project into child project.
Do I need to add parent project into dependencies of child project?
Parent and child project probably assumes that you use maven's inheritance feature.
Here is what's written in Maven documentation
The packaging type required to be pom for parent and aggregation (multi-module) projects.
So, your parent project should be with packaging type "pom" and enlist your child projects that can be of packaging jar or even pom if they have children in turn.
But in this case, the parent project cannot really contain the java code, so your question is kind of confusing here
When it comes to dependencies in a parent project, yes, if you define dependencies on parent pom they'll be automatically inherited by all children projects
In the same section of the link I've provided above, you can find a list of elements that get inherited and dependencies section appears there.
If you want to reuse your own code between many modules you should just create a module with packaging jar (regular maven module), put the shared code there and then in other modules, just declare the dependency on your shared module.

Build maven multimodule project in JAR

I have a parent Maven project (pom packaging) and 4 Maven modules (jar packaging) as children of that. Is there any way/plugin to create a importable dependency of the parent project including all the children? I mean that I want to create some kind of abstraction, so when somebody wants to import my project, it won't be necessary to import a specific childermodule (the module that he will use), he will just import the whole project (all the children modules)
The Maven parent project corresponds with inheritance. It is meant the children projects to inherit the settings, so you do not have to repeat them and you may set all the common stuff in a single place.
On other side, the convenience Maven project containing dependencies corresponds with composition.
You should not mix these two things together. Parent project is a common super-class of your modules. This convenience project you wish to have is a collection containing all your modules as dependencies. You should not want the theAnimal class to depend on all the animals.
Just accept the fact that you need two separate Maven projects:
the parent (if you need it), and
the convenience project for transitive dependencies.
The solution for your problem is described e.g. here: 3.6.1. Grouping Dependencies
If you have a set of dependencies which are logically grouped together. You can create a project with pom packaging that groups dependencies together. For example, let’s assume that your application uses Hibernate... Every project which uses Hibernate might also have a dependency on the Spring Framework and a MySQL JDBC driver. Instead of having to include these dependencies in every project that uses Hibernate, Spring, and MySQL you could create a special POM that does nothing more than declare a set of common dependencies. You could create a project called persistence-deps (short for Persistence Dependencies), and have every project that needs to do persistence depend on this convenience project.

How to depend on multiple projects in a maven library?

My project depends on an external library, which consists of a number of maven projects.
Do I have to define each of the projects in the library to be a module in my project's parent pom.xml? Is there a way to define the library as a whole in my project without individually listing all the projects?
My project directly depends on only one project in the library, but that project depends on other projects in the library.
Do I need to define all the projects in the library in my project's dependencies?
Ideally, in parent pom you define modules i.e, external libraries which you want to build. When you build the parent pom, it will build all modules which are defined in parent pom. And further, modules will build other dependencies/modules.
You need a composite pom -- a pom which just declares a bunch of dependencies. You depend on it, and transitively, you get its dependencies.
See this discussion for more information.

Maven / eclipse project structure

I am still fairly new to Maven, I finally have it how I want but now I need to break it all over again.
Here is my scenario:
I need to write two different server applications, which use identical core functionality; just, what is done with that framework is very different. One server application is very easy/simple - it's already done - whereas the other is a lot more complicated.
The code is written in a dependency injection style (using Guice, if it matters), so it should be extremely easy to break apart.
My question is this: how would you structure the projects in Eclipse, using Maven? Would you set up three different projects, something like:
server-core
server-appEasy
server-appComplicated
where each server would have it's own pom. Or, would you keep it all in one project? I need to be able to easily recompile appEasy in, say, a month from now, while I work on appComplicated. The classes for appEasy are already in a subpackage. Note: core would not work by itself without at least a mock dependency injection. It doesn't have a main class.
All thoughts appreciated, even on things I haven't thought of.
I would have a structure like this:
/server
/server-core
pom.xml
/server-appeasy
pom.xml
/server-appcomplicated
pom.xml
pom.xml
So each project has its own pom.xml that allows you to build that project in isolation.
However the parent folder also has a pom.xml, which will build all the projects if run. You can do this by including the projects as modules in the parent pom.
E.g. In the parent pom.xml
<modules>
<module>server-core</module>
<module>server-appeasy</module>
<module>server-appcomplicated</module>
</modules>
You can also use managed dependencies in the parent pom tio allow you to centralise external dependency and plugin version numbers.
I would suggest to structure all as a Maven Multi Module project.
The parent project, would have the three projects as modules, the 3th party dependency versions, and the version of your project as a property.
Then, in the server-appComplicated and server-appEasy I would add a dependecy to the server-core.
In this way you will gain:
1- A root project to compile (the parent), that it will generate the two servers and the core-lib.
2- A point where to handle the version numbers and the common dependencies.
I hope it helps
Im not a maven expert but here is my 2 cents.
Each project needs its own pom.
Do you need to build all the projects together? In that case it might make sense to have a parent pom , which has all the common dependencies.
EDIT: In that case, I feel just have three separate 3 pom files for each project.
There are multiple ways to do this, depending on how you need it when it comes to deployment. Assuming that 'server-core' is a shared artifacts among your 'server-appEasy' and 'server-appComplicated' artifacts, I would suggest something as below
Create a Maven Project 'server-core'
Add two module projects
by name 'server-appEasy' and 'server-appComplicated'
Make sure the module projects have their parent set as the 'server-core'
In the end you should have three projects (each has separate pom.xml), where
a. Building 'server-core' will also build the modules
b. Building either of the 'easy' and 'complicated' modules independently on need basis will also build the server-core.
Hope this helps!

Categories

Resources