Maven generating two different WAR files for same project - maven

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.

Related

Assembling a jar using Maven containing only files in a specified folder

I have the following Maven project structure.
project
-- src
-- main
-- java
-- models
-- resources
I want to create and deploy a jar project-models.jar containing everything inside the folder models and nothing else. Since I'm not very familiar with Maven, I'd really appreciate if you could provide me some example.
models belongs to resources (They should neither be compiled nor tested, should they?)
See How to create an additional attached jar artifact from the project:
Specify a list of fileset patterns to be included or excluded by adding <includes>/<include> or <excludes>/<exclude> and add a classifier in your pom.xml.
Note: the jar-plugin must be defined in a new execution, otherwise it will replace the default use of the jar-plugin instead of adding a second artifact. The classifier is also required to create more than one artifact.

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.

Sharing code AND configuration using parent module in Maven

In my project I have two modules (A,B) depend on common module C.
I would like A,B share common configuration, such as repository configuration, plugin configuration, etc. In order to achieve this, C is made the parent pom and A,B inherit from it (no aggregation is required, so C does not reference A,B)
However, C by itself contains some java code, along with unit tests. Now, if I run mvn test inside C nothing is executed. Supposedly, pom-packaged modules should not include any code.
Is the above configuration terribly flawed? Should yet another common pom be introduced even though one already exists?
At very typical pattern in Maven projects is to have a module named "parent" which holds your common Maven-specific configuration, profiles, properties, dependency versions, etc. This is The Maven Way (TM).
The structure looks something like the following:
|-- parent
|-- common
|-- module-a
|-- module-b
So, common, module-a and module-b each depend on parent, and module-a and module-b depend on common.
See this excellent resource:
http://www.sonatype.com/books/mvnex-book/reference/multimodule-web-spring.html
Note that "parent" can be a sibling of its child modules, in terms of directory structure. There is no requirement that the directory structure hierarchy match that of the modules.

Building multi-module project from Maven into one war file

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.

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