Get maven dependency from child POM - java

Is there a way, without modifying parent POM to use version of some dependency from child POM?
For example in parent POM i have:
<dependency>
<groupId>org.dbunit</groupId>
<artifactId>dbunit</artifactId>
<version>2.4.8</version>
</dependency>
In child POM:
<dependency>
<groupId>org.dbunit</groupId>
<artifactId>dbunit</artifactId>
<version>2.5.0</version>
</dependency>

If you put that dependency in the dependencyManagement section of your child pom, then modules from your child pom downwards (it will not affect siblings of your child pom) will use the version you specify. It will, effectively, override the version in the parent pom.

Related

dependencyManagement and version of dependencies

In the pom file I have the dependencyManagement tag (could be also inherited from parent) and i would like to know how this tag can influence the version of dependencies. so let show an exemple:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>2.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.0</version>
<scope>test</scope>
</dependency>
</dependencies>
In the exemple below what version of the hamcrest-core dependency will be used? the version 2.2 or 1.0?
dependencies defined in the pom should get priority over dependencyManagement.
Using dependencyManagement is useful when you have child projects under a parent project,
Dependencies specified in the parent pom dependencies section will always be included as a dependency of the child modules.
Dependencies specified in the dependencyManagement section, will only be included in the child module if they were also specified in the dependencies section of the child module.
You can specify the version and scope in the parent, but you can leave them out when specifying the dependencies in the child POM (to ensure all projects are using same version).

DependcyManagement or dependencies ?? in pom maven

According to the maven docs Dependency management
is a mechanism for centralizing dependency information.
and from this question here SO Question, many people suggested to use Dependency managmement instead of dependencies when we have a common jar file for all children.
as in dependency management , dependencies are propogated only when children request for it but incase of dependencies the dependecies are propogated even when not required.
but wouldn't it be a better approch when the jar file is common to all children ,i.e when all the children inherit the same jar file
for example (rewritten example taken from maven docs)
child a
<dependencies>
<dependency>
<groupId>group-a</groupId>
<artifactId>artifact-a</artifactId>
</dependency>
<dependency>
<groupId>group-c</groupId>
<artifactId>artifact-b</artifactId>
</dependency>
</dependencies>
child b
<dependencies>
<dependency>
<groupId>group-c</groupId>
<artifactId>artifact-b</artifactId>
</dependency>
<dependency>
<groupId>group-a</groupId>
<artifactId>artifact-b</artifactId>
</dependency>
</dependencies>
parent of both a and b
<dependencyManagement>
<dependencies>
<dependency>
<groupId>group-a</groupId>
<artifactId>artifact-a</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</dependencyManagement>
but wouldn't this yeild the same result ??
parent of both a and b
<dependencies>
<dependency>
<groupId>group-a</groupId>
<artifactId>artifact-a</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
child a
<dependencies>
<dependency>
<groupId>group-c</groupId>
<artifactId>artifact-b</artifactId>
</dependency>
</dependencies>
child b
<dependencies>
<dependency>
<groupId>group-c</groupId>
<artifactId>artifact-b</artifactId>
</dependency>
</dependencies>
or did i misunderstand them??
and which one should i use and in what conditions ??
Dependency management section in the parent pom is managing versions and only versions of these libraries, when you are using them in the child projects.
Dependency section on the contrary ENFORCES all child modules to have these dependencies, regardless child modules need them or not.
If you are sure ALL your child modules will use group-a:module-a:version, then feel free to declare it in parent dependencies section.
If you have at least one child module in the project, where such enforced dependency is unnecessary, then dependencyManagement better suites your need.

Is it possible to re-scope an imported Maven dependency in the same pom.xml?

I'm working in an application server environment in which I'm using a bom to gather the dependency information like so:
<dependency>
<groupId>org.jboss.bom.eap</groupId>
<artifactId>jboss-javaee-6.0-with-security</artifactId>
<version>${jboss.bom.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
However, this particular bom specifies a dependency as "compile" that I wish to have scoped for all of my projects as "provided". However, when I attempt to override the scope in the same pom from which I'm importing the dependency like so:
<dependency>
<groupId>org.picketlink</groupId>
<artifactId>picketlink-federation</artifactId>
<scope>provided</scope>
</dependency>
Maven complains that it cannot find the version, or if I use the version property specified in the bom, the property cannot be found.
I'm fairly certain this is an issue with the import + override in the same pom, because I can override the scope in child projects just fine. Is there a way to both import and override the scope in a single pom?
*all code snippets above come from the same section.
It is certainly doable:
<dependencyManagement>
...
<dependency>
<groupId>org.jboss.bom.eap</groupId>
<artifactId>jboss-javaee-6.0-with-security</artifactId>
<version>${jboss.bom.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
...
</dependencyManagement>
<dependencies>
...
<dependency>
<groupId>org.picketlink</groupId>
<artifactId>picketlink-federation</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
Just remember that you need to redefine your scopes in the <dependencies>and not in the <dependencyManagement> section.
Your scope override will of course propagate to any child POMs, if you use inheritance.

How to inherit Java-EE API for Webapps in Maven?

My project has the following structure with multiple jars and wars:
root (pom)
+--core (jar)
+--webapp (jar)
+--childgroup (pom)
+--actual-web-application (war)
+--some-library (jar)
+--othergroup (pom)
+--another-web-application (war)
Where actual-web-application depends on webapp which depends on core. Ideally, I would like to specify the Java EE dependency only in the webapp module and let the actual-web-application inherit it. But because it's a provided dependency, this doesn't work and I have to manually add dependencies for provided stuff like Java EE, jax-rs etc. in every web application.
Is there any way, with dependencyManagement for example, to let actual-web-application inherit the javaee-api from the webapp?
What I've tried, in webapp's pom.xml:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
</dependencies>
</dependencyManagement>
And in actual-web-application's pom.xml:
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<scope>provided</scope>
</dependency>
However, it complies about the version so it seems like it's still not carrying over the dependency from webapp's pom.xml.
Put <scope>provided</scope> into dependencyManagement also
Provided dependencies are inherited just as any other dependency would be.
parent pom.xml:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
</dependencyManagement>
child pom.xml:
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
</dependency>
</dependencies>
And the child's dependency tree:
--- maven-dependency-plugin:2.8:tree (default-cli) # child ---
com.iei.web:child:war:1.0-SNAPSHOT
\- javax:javaee-api:jar:7.0:provided
\- com.sun.mail:javax.mail:jar:1.5.0:provided
\- javax.activation:activation:jar:1.1:provided
If you are trying to avoid explicitly defining dependencies in the child at all, then you would of course have to move it out of dependencyManagement in the parent and make it a direct dependency, assuming you want every child module to inherit it.
Dependency Management

m2Eclipse dependencyManagement section doesn't show graph

I don't know if this is a bug or intended functionality, but the POM editor for m2Eclipse has a wonderful graph representation ('Dependency Graph' tab) and tree ('Dependency Heirarchy' tab) if the dependencies are in the dependency section all by themselves. However, when you move them into the 'dependencyManagement' node (useful for module based projects) these tabs no longer work.
Does anyone know if this is a bug, intended functionality, etc?
EDIT #1: The dependencies in the dependency management section are NOT declared outside of the dependency management section in the parent. They are there to share amongst the child modules; to keep consistency (there was some standard posted somewhere that we're modeling this on).
EDIT #2: The tooling works at the child module level. I am referring to it not working at the parent level.
TIA
I have POMs with dependencies declared under dependencyManagement and they are just shown as expected in the Dependency Graph and Dependency Hierarchy tabs.
For example, I have a parent POM with:
<dependencyManagement>
<dependencies>
<!-- SL4J API -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- SLF4J JDK14 Binding -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
...
</dependencyManagement>
And a child POM with:
<dependencies>
...
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
</dependency>
...
</dependencies>
And the Dependency Graph of the child just works:
Are the dependencies you moved under dependencyManagement actually also declared as dependencies? Can you show a simplified pom.xml illustrating the problem?
The tooling works at the child module level. I am referring to it not working at the parent level.
Dependencies declared in the dependencyManagement element are not dependencies of the project (if I declare foo in the dependencyManagement, I'm still not depending on foo). If the parent doesn't declare any dependencies, there is nothing to show.

Categories

Resources