Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declare as dependency or as parent

If you have Project A and it needs all the dependencies of Project B, do you declare Project B a parent of Project A, using inheritance, or a dependency of project A? They seem to have the same effect.

like image 911
Jeffrey Ramos Avatar asked Jan 18 '12 01:01

Jeffrey Ramos


People also ask

What is the difference between parent and dependency in Maven?

A dependency is libraries you need to get your code to compile. This can be your own code, or libraries such as Apache Commons. A parent contains information, but nothing to actually build, that is shared between a number of your projects.

What is a dependency in Maven?

What is Maven Dependency? In Maven, a dependency is just another archive—JAR, ZIP, and so on—which our current project needs in order to compile, build, test, and/or run. These project dependencies are collectively specified in the pom. xml file, inside of a <dependencies> tag.

How do you get dependency from parent pom?

Now child POM need to refer the parent POM using parent tag and specifying groupId/artifactId/version attributes. This pom file will inherit all properties and dependencies from parent POM and additionally can include extra sub-project specific dependencies as well.

How does Maven resolve dependencies?

How does maven locate and resolve dependencies? Unlike other repository formats (APT, YUM Rubygems, there is no main index file that enumerates all possible artifacts available for that repository. Maven uses the coordinates values for a given dependency to construct a URL according to the maven repository layout.


2 Answers

The two approaches are different, one is inheritance, and the other one is a simple dependency.

By dependency you'll only have the binary transitive dependencies of the project B.

By using as a parent project you'll inherit the configurations like plugins, building model, repositories, dependency-management, dependencies and so on, it depends on the case.

My rule of thumb is for scm configuration, project configuration, and development or company standards, I use a parent project (inheritance).

like image 106
Germán Avatar answered Oct 09 '22 10:10

Germán


I'd say if Project B is intended to act as a "universal parent" or somesuch - that is, a starting point for many projects with identical dependencies (or with the same starting point for their dependencies) then go right ahead and make B the parent.

Otherwise, if it's actually more of a "handy thing to have" - for example it's got utility/core type methods that a lot of projects will find useful, but don't rely upon for their very existence, then it's logically a dependency.

You might even consider splitting Project B into two along those lines. That's what I've done in the past:

mygroup-parent is a project that consists solely of a POM file with dependencies that I want every single project to get - enforcing a standard approach. For example, I've got TestNG and Mockito in there.

mygroup-core is a fully-fledged Java project that has handy things that get used a lot, but are not a must. I've got helper methods for things specific to my site, and dependencies like Apache HttpClient and Google Guava.

like image 9
millhouse Avatar answered Oct 09 '22 12:10

millhouse