Combining jars for multi modules maven project in a single jar - maven

I have a parent maven project that includes some child projects and a build/assembly project. The structure looks like this
ParentProj
+ pom.xml
+ ChildProj1
++ pom.xml
+ ChildProj2
++ pom.xml
+ buildProj
++ pom.xml
I want to create an executable jar in the build project, or project that contains all child project jars. Basically the final result should be a jar that includes all child project jars in the classpath.

Your project buildProj should have the other child projects declared as dependencies, you could then configure the maven assembly plugin to use the predefined descriptor jar-with-dependencies which create a jar that contains the dependencies.
How can I create an executable JAR with dependencies using Maven?.
https://maven.apache.org/plugins/maven-assembly-plugin/descriptor-refs.html
EDIT
If you wish to add the dependencies jar in your jar, you could use something like onejar:
Creating a JAR file which contains other library files
http://one-jar.sourceforge.net/
There is a maven plugin for this tool: https://code.google.com/p/onejar-maven-plugin/

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/

Compile a jar during Maven build then package as ear from single pom.xml?

I have a maven project currently structured that has to build a jar first from some source files, then create an ear file containing that jar along with some other xml resources. The way I do it at the moment is by building the jar seperate in a 'child' pom.xml then at the root of the project folder I have a parent pom that does the ear packaging.
I know it isn't ideal, but I what to be able to compile the jar and then package it into an ear all with just one pom.xml file. Any ideas on how to do this?

Create a maven project with parent's packaging war

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.

maven - need to create ear bundle

I have a maven project which currently creates a war package on build. Now I need to bundle it as an ear project. Any pointers on how to do this. I added the m2e plugin and modified the pom.xml file, but, i am not able to get the directory structure as expected. I need the dir structure as below
project-name
- project-name.ear
- pom.xml
- project-name.war
- projec-name.jar
- META-INF/application.xml
Thanks.
It is strongly recommend that you organize your project structure in proper Maven style:
project-name/
- pom.xml (POM of project-name, multi-module POM)
+ project-name-jar
- pom.xml (main app JAR)
+ project-name-web
- pom.xml (WAR project to construct WAR by project-name-jar etc)
+ project-name-ear
- pom.xml (EAR project to construct EAR by project-name-war etc)
Normally I will have another child project call project-name-parent which use as parent POM for the whole project.
In Maven, you don't and you shouldn't have META-INF/application.xml as part of your source. It is generated dynamically base on configuration of maven-ear-plugin.

how to include dependency classes to war project's WEB-INF/classes folder in maven

I've a project B which has a dependency on project A. Both project's packaging is jar. Another project C packaging is war. Project C contains no java classes. It contains only webapps folder. Project C has dependency on project B. I want the war to be packaged in just a way that all the java classes from Project A and B should be copied to WEB-INF/classes folder. How can we achieve this ?
You can use the unpack goal of maven dependency plugin in your pom to unpack the desired dependencies to a folder and ensure maven war plugin includes this folder.

Resources