Create a maven project with parent's packaging war - maven

I want to create a Maven project. I want it to have a parent project. The problem is that the parent project has a package in: war.
I see an error :
Invalid packaging for parent POM must be "pom" but is "war"
What should I do ?

A parent project (packaging with type pom) is by definition a container of submodules. Only the submodules are allowed to be of specific packaging types (like war or jar). You use a parent project to aggregate common dependencies and build configurations.
I suggest that you put the code you want to reuse in a submodule of type jar and then add this submodule as a dependency of other projects you have (with packaing type war or jar).
You could read Chapter 6 of the book Maven by Example where it illustrates how to build a maven multi-module project.

Related

Converting one module to standalone Maven project, including a minimal working POM

I would like to distribute the main artifact sources (main Java and tests) of a multi module project as a simple - standalone - Maven project.
The easy parts of this can be implemented using the maven-source-plugin. This also is able to include the POM in the generated source code jar. However, this is the artefact POM, which refers to the parent POM which is not included in the jar.
Other than creating the POM manually, is there a way to generate a minimal POM which contains the dependencies (extracted from the artifact POM and its parent)?
If you create the POM with the flatten-maven-plugin, all parent relations are resolved and you get an equivalent POM without the unnecessary parts.
https://www.mojohaus.org/flatten-maven-plugin/

How to create ear using using another project war through multi module approach

I have one maven project(lets say Proj1) which creates a war file. I want to create a parent project(lets day Proj2) which will create an .ear using the war project. So that when I build Proj2, it should build the war from Proj1 and then use it to create .ear
What you need is your project to be structured this way:
Parent project (packaged as pom)
-->Child-EAR project (packaged as EAR)
-->Child-WAR project (packaged as WAR)
Each project should have a pom file, parent project should include the child projects as its module. The child project should include the parent module. Please see this link: https://www.javacodegeeks.com/2014/04/java-ee7-and-maven-project-for-newbies-part-1-a-simple-maven-project-structure-the-parent-pom.html it explains in detail.

Maven multi project

I have a several maven module (3 jar and 2 war, modules are interconnected).
My purpose is combine this modules to one ear file.
Example: in ear maven project I run in lifecycle "package" goal, when all maven project, who include in global project are execute "package" goal and when creating global ear, who include jar (war) from other maven project.
How do this?
I create global project as <packaging>pom</packaging>, it execute "package" goal from dependency project
<modules>
<module>...</module>
</modules>
but how create ear?
You should use <packaging>ear</packaging> instead of pom.
It will force maven to use maven-ear-plugin on package phase (so you don't need to provide any executions of this plugin).
You could find simple example here: Maven EAR Plugin Example

how to run only specific maven module

how to run only specific maven module from maven eclipse project.
I have few modules in my EAR maven project and when I build whole project it fails at one module WAR,saying this war artifact module not found in repository.
Your multi module project must e having a common parent. First install that parent pom in your local repository. Now go to child module project and maven build the project.
It should work given that all its dependencies are present.

Maven packaging ear finding dependency from project level but not from parent

I've currently got a parent pom that declares two modules: an ear and a war. The ear is reliant on the war (and declares a dependency for it with group/artifact id and packaging type).
When packaging from the parent pom level, the reactor picks up both artifacts and properly packages the war into the ear as you would expect. However, when packaging from the ear's project pom (despite having declared elements in both projects pointing to the parent pom) the ear fails to find the war artifact.
I know that when packaging at the ear level Maven finds its way to the parent correctly, but does it not then iterate down to the various modules that the parent contains to pick up artifacts?
Thanks :)
That's the way Maven works. It's OK. When resolving dependencies, Maven looks for them in reactor, then local repository, then remote repos. So, when doing a build from a parent project level, both projects are in the reactor, so EAR can pick WAR easily. (To be specific, it's not because the parent-child relation, but the fact they are modules.) However, when you build an EAR module in isolation, reactor can't provide WAR as well as local repo as well as remote repos. If you install WAR module into local repo by mvn install and then try to build EAR, WAR artifact will be found using local repo.
Sounds like your parenting structure is broken, if the EAR depends on the WAR then it should be a child module.

Resources