Building multi-module project from Maven into one war file - maven

My question has been asked before, which I know, but I think that I am trying to do something slightly different, where existing answers are not appropriate.
Essentially, I do want to have multiple projects in Eclipse that will be built (preferably) into one final WAR file. Ideally like this:
root - pom.xml
|___ java-app
|___ web-service-v1
|___ web-service-v2
|___ web-service-v3
|___ rest-service
|___ batch-service
Imagine the Java App as the actual application, and each additional component runs as a decoupled view layer for the Java App itself. Ultimately, the Java application will be running in a tomcat instance, with the different modules providing their services. I would like all the different modules also to run in the same Spring container.
I don't know that running in Maven modules is the best way of doing this, but I would really prefer to have each component in a separate Eclipse project that ultimately get built together.
Can anyone provide any suggestions as to how I would use Maven to build this?

Just make a separate war module:
root - pom.xml (packaging: pom!!!)
|___ java-app
|___ web-service-v1
..
+--- mod-war (pom.xml)
and put the dependencies of the modules you would like to have added to the war file into the pom and that's it.

The main Maven idea is that each module must produce a single build artifact (e.g. a jar or a war file). The parent pom is usually responsible for global configuration and dependency management and also for a proper module orchestration. If your final result need to be a WAR file, then last module in the list will be the web application. The other modules could supply classes that war file depend on.
There are more complicated build structures, but above one should be sufficient for you.

This is somewhat dated, but hope this additional info helps someone.
#Mouscellaneous An example of keeping Spring configs in each module and referencing them from the web.xml can be found here in the Sonatype book. I guess this is what you are looking for.

Related

Single-module vs Multi-modules maven project

I'm creating a new java web project. This web application will have many modules such as core/commons, business service, repository, security, integration, ldap, user management,.... I wonder if I should separate each module into each maven project (jar file) or create project that include all java packages of all modules into single one maven project.
Structure of Multi modules maven project
mycompany-core
mycompany-repository
mycompany-api
mycompany-usermanagement
mycompany-business
mycompany-web
Structure of Single module project:
mycompany-web
|___ src
|____ main
|____ java
|____ com.mycompany.core
|____ com.mycompany.repository
|____ com.mycompany.business
|____ com.mycompany.controller
When should we apply multi-modules or single-module project> Please give me some advice.
All products I've worked on in Maven were multi-module. This is because they tended to be big. However, when I create my own pet projects, they are normally single-module ones.
As a rule, as products grow, they will need to be organized into multi-modules. Some projects start as a single module and are split as they grow. Others, created by developers with more experience, are already divided up, because the developers already know how the code will grow and how it needs to be organized.
Specifically, from your list, "core/commons, business service, repository, security, integration, ldap, user management", I would separate "commons" into its own module, because it smells like it could be reused on other projects. The other parts could all fit into one module, but I'd need more insight into the project.
It is depend on your requirement. If you want to run this using .sh or .bat you should have single target(single jar with other libraries).
If your project build as an API it is better to have your build as multi module one.

Build several artifact with maven classifier plugin vs. modules

Here is the context : one application, let's name it "clientdata" is bundle into an ear to be deploy on WebSphere. We need to generate several jar for this application :
api-contract (interfaces)
ejb
ejb-client
impl
The ear contains ejb, impl and api-contract jar + the application dependencies
Let's call "checkclient" another application which need to call "clientdata" by RMI.
It will need to use api-contract and ejb-client jar.
For now the application is divided into 3 sub-modules (API, IMPL and EJB).
API module generate api-contract jar
IMPL module generate impl jar and use api-contract jar
EJB module generate ejb and ejb-client and use impl and api-contract jar
So here is my question : one member of the team thinks that we should use maven classifier plugin to generate all those jar so that we need but to maintain only one pom (one maven project).
I'm not so familiar with maven classifier plugin so i wanted to know, is this a good idea ?
Some of my readings make me think it is not, mostly because it violate a "core convention" and "Tools that have been designed to work with Maven won’t help you"
Here is the source of the citations : http://blog.sonatype.com/2010/01/how-to-create-two-jars-from-one-project-and-why-you-shouldnt/#.U7UhNCgucvQ
I would greatly appreciate your expertise on the subject.
Regards, GBE.
First the member of your team is simply wrong, cause that's the wrong way to deal with such default scenario in Maven. I assume (hope) you have created an appropriate folder structure
+-- root (pom.xml)
+-- mod-api (pom.xml)
+-- mod-ejb (pom.xml)
+-- mod-impl (pom.xml)
+-- mod-ear (pom.xml)
Based on the above folder structure you can simply define dependencies between the modules and let maven do the rest. Apart from that you can create the ejb-client automatically by maven-ejb-plugin and use this for furthere enhancements.
Now you can create an ear from it (mod-ear just add the dependencies to it) and just deploy it to WebSphere. That the way it should go. Creating a single pom to create that number of artifacts is by definition against the desing of Maven cause the convention in Maven is to have a single pom for an artifact. So you will start to fight against Maven and you will loose that combat.
Better got with the idea of Maven and make your life easier. Apart from that it's a separation of concern which means an single pom for a single part of the project which makes it clear where you can find anything in your project. Here is a simple example of a Java EE application.

J2EE webapp Utilities project best practises

I have a Maven based J2EE project structure
Customer (root pom)
|
|------ WebTier (JSF 2/Primefaces)
|
|------ BusinessTier (EJB 3.*, interfaces, interface impl)
|
|------ PersistenceTier (JPA 2)
|
|------ CustomerEar
Now I have found that I'd need a new Java class or classes for handling basic routines
I guess its not good idea to place this Utility class under WebTier, because Utilities might be used also from BusinessTier.
Is it the "best practise" to create an independent Java Project, implement necessary classes and include this jar to the ear??
based on my experience , i recommend to have separate project folder for utility and include this as jar where ever is required
advantages are
easy maintenance, since you need to change code once .
plugin & unplug when ever you need.
you can have this as common and utilize for
other projects.
for instance
apache string util
http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html
bean util
http://commons.apache.org/proper/commons-beanutils/
It would be hard to try and justify a "best practice" in this kind of situation as it is a very project-specific question.
There would be nothing inherently wrong with adding a "CommonLibrary" project or such-like that is a dependency of both the WebTier and BusinessTier. In fact the project I am working on now is almost exactly that structure (with a common library).
As this is a maven project, just make sure the "CommonLibrary" project dependecy scope is set to "provided" in the Business/Web/Persistence tiers, and "compile" in the ear project.
e.g. In WebTier, the dependency would be:
<dependency>
<groupID>group.name</groupID>
<artifactID>common-library</artifactID>
<scope>provided</scope>
</dependency>
EDIT: The scope would be 'provided' in the Web Tier as it would be a .war. The other tiers would be jars so the scope can be 'compile' for the persistence and business tiers.

Maven generating two different WAR files for same project

I have a maven web-service project consisting of two different service classes testClass1 and testClass2 . I want to have two different WAR files for both classes of same project. Presently I am generating single WAR file for both services .
How can I generate two different WAR file for same project ?
Thanks In Advance.
General Considerations
As per doc says, what you try to achieve is a bad pratice :
Producing Multiple Unique JARs from a Single Source Directory
As many people that complain about not being able to spread out their sources into multiple source directories seem to complain about not wanting to spread anything out, producing several unique artifacts from a single directory using includes and excludes.
Why isn't this recommended?
This practice can be confusing and risky.
You may end up building two JARs that include the same classes - this indicates that the common functionality should have been abstracted into a separate dependency.
You may end up introducing a dependency between the two JARs that you didn't realise, and often a circular dependency. This indicates that the classes are in the wrong JAR, or perhaps that everything should just be a single JAR.
Solutions that fit the maven best pratices
You should consider splitting you project into two differents ones, having each one a pom, and so each one generating an artifact, here a war.
Two simples projects
You may achieve that by using either two simple projects :
service1-simple-webproject
|-- src
`-- pom.xml
service2-simple-webproject
|-- src
`-- pom.xml
This is quite simple, but you may not be able tho share easily properties / dependencies / relations between them.
Just split your own code into separates directory, and try to make it work as two independant projects.
If you think that there is some code to share between them (utilities, configuration, ...), see next section about multi module.
Multi module structure
Or you can use an appopriate multi-module hierarchy :
parent-multimodule-project
|-- service1-simple-webmodule
| |-- src/
| `-- pom.xml
|-- service2-simple-webmodule
| |-- src
| `-- pom.xml
`-- pom.xml
This will allow you to have relations between projects, share configuration at a higher level, ...
Ta ke a look to this documentation :
Maven Official doc : http://maven.apache.org/guides/mini/guide-multiple-modules.html
Sonatype fantastic doc : http://www.sonatype.com/books/mvnex-book/reference/multimodule.html
I will probably lead you to create a third project, probably named core, or util, that would produce a jar which will contains common classes to both web project.
This is really the best way !
Functionnal solutions but strongly not advised ones
I would only give you some information, because I'm even sure it could work in this case.
Maven Assembly Plugin allow you to generate different format of package (zip, jar, war, other, ...) based on xml description files.
Here documentation : http://maven.apache.org/plugins/maven-assembly-plugin/
Once again, I strongly advise you to NOT use this kind of workaround.

Using multiple web pages folder with maven

Im new with Maven and i want to migrate my framework from ant to maven but im stuck in a problem. This is the situation.
My project structure is something like
project
module1
module2
...
Each module may contain a webapps folder and an ant scripts joins every modules in a build folder with all webapps folders unified in one. I need this schema because my framework is one of this modules and it contain jsp and web stuff but there can be some other modules with custom stuff or extensions.
Thing is that maven modules does not fit because its no like module2 depends on module1 or parent "needs" this modules. Its like "i need to join all this splitted stuff together."
So the questions are
Is there a way to create a module exporting "web" stuff (not in war, just to join web content with parent project web content)?
If not, is there a way to split web content in several folders/modules/something so i can keep it modularized?
WAR Overlays might be able to help. Importing module A as an overlay into module B will essentially mean that module B gets all of module A's web content as well as its own. You can also configure inclusion and exclusion filters if you need to. Both module A and module B are WAR projects.

Resources