Maven combine two project - java

How can i combine two maven projects. One is webapp, and other is some javascript library (also webapp) which i want to combine with others project.
Or, would be better, how to add some outside folder with js files to maven project that can be deployed on testing server and then build to war.

Have a look at overlays in the Maven WAR Plugin documentation. This explains how Maven merges resources from different web projects into a single WAR.
In a nutshell, you create several WAR files of all the dependencies (usually, you already have this but you can even do this if they aren't real working web projects). Then you can pull these in as dependencies. The important part here is to specify the type of the dependency (<type>war</type>); otherwise Maven will try to add the JAR.
The WAR plugin will notice the additional WARs in the list of dependencies and merge them.

You can create One parent project and Two modul project. You will have 3 pom.xml files.
modul projects extendens dependencies from parent project. Maven parent pom vs modules pom, Multimodule project

Maven WAR Overlays could solve the problem. If you have two maven web projects, and one of your Web Project depends on the other's you could declare the dependent project as a dependency and do an overlay.
Reference:
http://maven.apache.org/plugins/maven-war-plugin/overlays.html

Related

maven multi module build with a shared utility project

Hi I am converting a ear project to Maven. Below is the structure
-projA-ear
-projA-static
-projA-web
-shared-util
The shared util is shared by multiple unrelated projects handled by our team. Currently my deployment assembly is handled by eclipse and the shared-util.jar is automatically built inside the projA-web.war's WEB-INF/lib directory
When I convert my setup to Maven project, I am reading that I need multi module aggregator setup, and need to define shared-util as a module, and then define the aggregator POM as the parent to my modules. The problem is I cannot define projA-mvn as parent in the shared-util project as the other projects would be using it too as a module(when they decide to move to maven). Can someone please suggest a solution?
1) Make shared-util as a separate standalone Maven project.
2) Put projA-ear, projA-web and projA-static under a separate parent Maven project.
3) Declare shared-util jar as a dependency in your projA-web pom.

In Eclipse, in a dynamic web project that is dependent on a maven project, how do I pick up jars from the maven project?

In Eclipse, I have two projects:
archangel.core - a Maven project
ArchangelWEB - a Dynamic Web Project (built for Tomcat).
The first, archangel.core has all of the base code and uses Maven to resolve dependencies. The second is the Web addition on top of the core project. This only has code specific to presentation/view. I want to keep them separate because I may have other projects in the future that will rely on the core, and I don't want the core project to have Web Library dependencies.
Right now, in ArchangelWEB's build path, I have the archangel.core project, and I also have archangel.core in its Deployment Assembly. This allows me to refer to code from archangel.core in ArchangelWEB without any build or runtime problems.
One of archangel.core's dependencies is apache-commons-lang. Within archangel.core, I can reference classes like ExceptionUtils. However, I cannot automatically reference this jar dependency from ArchangelWEB. If I try to import class from apache-commons-lang in ArchangelWEB, it doesn't know what I am talking about.
What is the best way to import/reference dependency jars from a maven project into this other project (my Dynamic Web Project), which uses the maven project as a dependency?
Dynamic Web Projects resolves dependency only by manually placing the Jars in the WEB-INF/libs folder. There is no other way.
So you need to place the dependent jars of your Dynamic Web Project into the libs folder manually.
Don't forget to do Right-click the Jars in Lib -> Add to Build path after adding them
This is why we use maven these days instead of the old Dynamic Web Projects.

Maven multi-module project and webapp project

I have two questions about maven project structure:
1) I am creating a maven multi-module project and would like to know if I can put all the common dependencies of the modules in the parent pom? Is that the right way to do?
2) If I am creating a maven webapp project that contains all web.xml and JSP files, where should the web controllers, listeners and model objects be residing? In a separate maven jar project or in the same webapp project?
1) Read up on the difference between dependencyManagement and dependencies. Putting all common dependencies in the parent POM will work, but it's likely to cause spurious dependencies when you add a new module.
dependencyManagement is recommended. You can specify all versions and exclusions in a central place, and then it's enough to specify groupId and artifactId for each dependency of each module.
2) Java sources directly related to the web layer usually go into the war module (unless you want to reuse them for a different project, then it's better to factor them out into a separate jar).

Single or Multiple Maven pom.xml configuration files?

I would like to ask these questions related to pom.xml files in Maven projects.
What is a reason to have multiple pom.xml for all dependency libraries instead of having all dependencies in one single pom.xml?
Where should be these pom.xml files in Maven project placed?
This is an example of pom.xml for Spring framework - http://search.maven.org/remotecontent?filepath=org/springframework/spring-core/3.2.5.RELEASE/spring-core-3.2.5.RELEASE.pom
What is a reason to have multiple pom.xml for all dependency libraries instead of having all dependencies in one single pom.xml?
A maven project can be made of many artifacts. One artifact may be a String manipulation library. Another may be a webapp that uses that String manipulation library.
Here's why you shouldn't put all your dependencies in one pom: Your String manipulation library should not have a reference to the servlets.jar just because an unrelated pom is a webapp. Each artifact should have only what it needs in its classpath.
(You may be interested to learn about the dependencyManagement tag, but it does not directly relate to your question.)
Where should be these pom.xml files in Maven project placed?
As #MariuszS linked to, here's the Standard Directory Layout.
At the top level files descriptive of the project: a pom.xml file (and any properties, maven.xml or build.xml if using Ant). In addition, there are textual documents meant for the user to be able to read immediately on receiving the source: README.txt, LICENSE.txt, etc.
This depends on your project, if project is small and product of this project (artifact) is only one then one pom is enough. But if your project have many artifacts (libraries, WARs, EARs etc) then for every artifact pom is required (generally).
Maven concept is that one project definition (POM) generate only one artifact (there are exceptions). Every project should have separate directory with pom.xml inside and source directories if needed.
One maven project can build two diffrent apps (for example desktop and webapp). This diffrent applications has diffrents dependencies.
Sample multimodule project structure: https://github.com/peter-lawrey/Java-Chronicle
Read more
Introduction to the Standard Directory Layout
Guide to Working with Multiple Modules
Maven by Example
Chapter 6. A Multi-module Project

difference between MutliModule and EAR

What is the difference between maven multimodule project(pom packaging) and ear packaging.
As i know Ear is used to package a group of related Module (EJB, JPA, JSF).
After reading maven documentation i found that multimodule project is used for the same thing.
Are they the same? Can i use multimodule project instead of EAR? can mutlimodule project be deployed to application servers?
A multi-module project (packaging of pom) is a way of defining a parent pom that has child modules. This is convenient for many reasons. For one, you can just build the parent pom with mvn compile and it will build all of its modules, too. Without the parent pom, you'd have to go into each pom and manually type mvn compile otherwise.
Not only that, but using modules gives you other really important features. See the answer to this question. To summarize, imagine you have a continuous integration server that just installed in the middle of you building locally. By using module, you ensure that you compile against your local code instead of the continuous integration server's code. This will prevent a lot of heisenbugs(sp?).
Now the packaging of ear is not directly related to this first multi-module concept. That packaging just determines the binary output. It will output an ear file. In the maven-ear-plugin plugin, you can include other modules like wars/jars/ejbs, but it won't do any of the things I've described in the first paragraph. For example, typing mvn compile in the directory of the ear's pom.xml file will not compile the war file it depends on.
Also, you don't have to include other maven modules in your ear. Another completely unrelated project may install an ejb and you can use that ejb in your ear just as a dependency.
multi module is just a way of organising your project into modules/components.the packaging need not be ear always.it can be for instance a war project that has all its server side content as a jar file in one of its module.
in short,multi module organisation can be opted for a ear project but the converse that mutli module is always a ear project is not true.
The two are not the same. A multi-module project just builds all sub modules, it does not produce an artifact.

Categories