Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 724
user2148736 Avatar asked Nov 15 '13 20:11

user2148736


2 Answers

  • 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.

like image 118
Daniel Kaplan Avatar answered Sep 28 '22 10:09

Daniel Kaplan


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

like image 22
MariuszS Avatar answered Sep 28 '22 09:09

MariuszS