Problem with transitive dependency while using BOM - java

Lets say I have
1. a bom project(named as bom)
2. a jar project(named as A)
3. a war project(named as B)
bom project has jackson-data version as 2.8.5 and 2.8.1 in properties and in dependencymanagement section 2.8.5 for jackson-data.
project A which inherits bom declares jackson-data dependency in it with version 2.8.1.
project B depends on project A and inherits bom. it does not declare the jackson-data in its dependency
now when I look at lib dir of Project B, jackson-data version is 2.8.5 where as project A is declaring it as 2.8.1.
irrespective of what is declared in dependency it is taking the version declared in bom. is there any way to resolve the transitive dependency of jackson-data in project B as it is declared in project A means a version of 2.8.1 and not 2.8.5 while using the bom?
Below are the poms
BOM PROJECT
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-
instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>bom</artifactId>
<version>1.1</version>
<packaging>pom</packaging>
<properties>
<jackson-databind.version>2.8.5</jackson-databind.version>
<jackson-databind.version.A>2.8.1</jackson-databind.version.A>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.test</groupId>
<artifactId>A</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson-databind.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
pom of project A
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.test</groupId>
<artifactId>bom</artifactId>
<version>1.1</version>
</parent>
<groupId>com.test</groupId>
<artifactId>A</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.1</version>
</dependency>
</dependencies>
</project>
Project B
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.emirates.ibe</groupId>
<artifactId>bom</artifactId>
<version>1.1</version>
</parent>
<groupId>com.test</groupId>
<artifactId>B</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>com.test</groupId>
<artifactId>A</artifactId>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warName>B</warName>
<warSourceDirectory>WebContent</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>
</project>

DependencyManagement overrides transitive dependencies. This is what happens here (and this is how it's meant to be).
If you want a different version, either change the dependencyManagement or override in the project in which you use A

Related

maven wildfly deploy could not resolve dependencies

I have a maven project with two modules : core and webapp.
The webapp module is a war file to be deployed to wildfly. It has a dependency to the core module.
I can run mvn clean package in the parent directory of my modules. I am able to deploy de war file manually to wildfly and it works. Unfortunately if I run mvn wildfly:deploy in the webapp directory I get :
Failed to execute goal on project webapp: Could not resolve dependencies for project gbt:webapp:war:1.0-SNAPSHOT: Could not find artifact gbt:core:jar:1.0-SNAPSHOT
What shall I do to deploy the war file with the wildfly plugin ?
parent pom
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>gbt</groupId>
<artifactId>unite_compte</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<version.server.bom>25.0.1.Final</version.server.bom>
<version.microprofile.bom>25.0.1.Final</version.microprofile.bom>
<jakartaee.version>8.0.0</jakartaee.version>
<version.wildfly.maven.plugin>2.1.0.Final</version.wildfly.maven.plugin>
<failOnMissingWebXml>false</failOnMissingWebXml>
</properties>
<dependencyManagement>
<dependencies>
<!-- importing the jakartaee8-with-tools BOM adds specs and other useful artifacts as managed dependencies -->
<dependency>
<groupId>org.wildfly.bom</groupId>
<artifactId>wildfly-jakartaee8-with-tools</artifactId>
<version>${version.server.bom}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>jakarta.platform</groupId>
<artifactId>jakarta.jakartaee-api</artifactId>
<version>${jakartaee.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<version>${version.wildfly.maven.plugin}</version>
</plugin>
</plugins>
</pluginManagement>
</build>
<modules>
<module>core</module>
<module>webapp</module>
</modules>
</project>
webapp pom :
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>gbt</groupId>
<artifactId>unite_compte</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<groupId>gbt</groupId>
<artifactId>webapp</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>gbt</groupId>
<artifactId>core</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<!--finalName>${project.artifactId}</finalName-->
<finalName>unite_compte</finalName>
<plugins>
<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
core pom
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>gbt</groupId>
<artifactId>unite_compte</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<groupId>gbt</groupId>
<artifactId>core</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
</build>
</project>
You need to run mvn install, or the artifact for the core module is built but never installed into the local repository and cannot be resolved when building the webapp module.

My Maven Module isn't generating a JAR

I have the following hierarchy:
my-app
\__ pom.xml
\__ jar-module
\__ pom.xml
\__ webapp-module
\__ pom.xml
\__ core-module
\__ pom.xml
Where core-module acts as a library providing common code for webapp-module and jar-module.
webapp-module works just fine, it's generating a war as expected and the code from core-module is being used.
The problem is with jar-module. I have no idea why, but jar-module isn't outputting a jar, even when I select to run (in IntelliJ IDEA) a simple main with a println() it does run, but there's no jar on the target folder.
How can I solve this and make the jar-module output a jar?
I am not very experienced with Maven, my goal is to be able to use the same Jersey code on a local Glassfish and in a jar to be deployed at AWS Lambda Serverless Java Container so I can easily test it locally before deployment.
I would appreciate any corrections on my hierarchy and on pom errors as well.
my-app pom.xml has a packaging of pom, but core-module and jar-module have a packaging of jar and webapp-module has a packaging of war.
here are the POMs:
my-app POM:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.myapp</groupId>
<artifactId>myapp</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>core-module</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>
core-module POM:
(I also tried it with <packaging>pom</packaging> and no jar skip properties, it didn't work)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>myapp</artifactId>
<groupId>com.example.myapp</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>webservice-core</artifactId>
<packaging>jar</packaging>
<modules>
<module>../jar-module</module>
<module>../webapp-module</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.deploy.skip>true</maven.deploy.skip>
<jar.skipIfEmpty>true</jar.skipIfEmpty>
<maven.install.skip>true</maven.install.skip>
<javax-ws-rs-version.version>2.1</javax-ws-rs-version.version>
<jersey.version>2.26</jersey.version>
</properties>
<dependencies>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>${javax-ws-rs-version.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>${jersey.version}</version>
</dependency>
</dependencies>
</project>
webapp-module POM:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>core-module</artifactId>
<groupId>com.example.myapp</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>webapp-module</artifactId>
<packaging>war</packaging>
<name>webapp Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<jersey.version>2.26</jersey.version>
</properties>
<dependencies>
<dependency>
<artifactId>core-module</artifactId>
<groupId>com.example.myapp</groupId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>${jersey.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>webapp</finalName>
</build>
</project>
jar-module POM:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>core-module</artifactId>
<groupId>com.example.myapp</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>jar-module</artifactId>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<jersey.version>2.26</jersey.version>
<maven-shade-plugin.version>3.1.0</maven-shade-plugin.version>
</properties>
<dependencies>
<dependency>
<artifactId>core-module</artifactId>
<groupId>com.example.myapp</groupId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.amazonaws.serverless</groupId>
<artifactId>aws-serverless-java-container-jersey</artifactId>
<version>0.8</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>${maven-shade-plugin.version}</version>
<configuration> <createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
my-app is your parent module and rest are child so provide a dependencies order in parent project pom so that when you build the project ,maven will provide the dependencies as you provided in parent module.
<modules>
<module>core-module</module>
</modules>
Provide the proper ordering like parent and child modules to understand the maven for building.
my-app
<modules>
<module>core-module</module>
<module>jar-module</module>
<module>web-module</module>
</modules>
core-module
no <modules>
This is a clean hierarchy, mirrored by the directory hierarchy.
There is a difference between modules and there container and the parent relation, and other subtleties. So maybe this helps.

How to package a maven multi-module properly?

I'm building a multi-module rest service with Maven and Spring Boot that will be published on a WebLogic Server, and I'm a bit confuse about how to configure the packaging of the application modules.
My current structure is:
/application
/main (main mehtod for spring-boot)
/api
/service
/dao
The parent project (application folder) is set up as pom packaging, but child projects I'm not sure how to configure it. I believe the correct way would be packaging like .jar file, but i saw some examples on the internet of Java projects with Maven multi-modules defined as war.
can someone tell me what's the correct way to define the child modules packaging in this case?
My parent pom:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.radarveicular</groupId>
<artifactId>radarveicular-application</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<javax.inject.version>1</javax.inject.version>
</properties>
<modules>
<module>radarveicular-api</module>
<module>radarveicular-service</module>
<module>radarveicular-main</module>
<module>radarveicular-dao</module>
</modules>
<build>
<plugins>
<plugin>
<artifactId>maven-eclipse-plugin</artifactId>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
<wtpversion>2.0</wtpversion>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
main pom:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.radarveicular</groupId>
<artifactId>radarveicular-application</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>radarveicular-main</artifactId>
<dependencies>
<dependency>
<groupId>com.radarveicular</groupId>
<artifactId>radarveicular-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- Spring Framework -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
api pom:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.radarveicular</groupId>
<artifactId>radarveicular-application</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>radarveicular-api</artifactId>
<dependencies>
<dependency>
<groupId>com.radarveicular</groupId>
<artifactId>radarveicular-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
service pom:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.radarveicular</groupId>
<artifactId>radarveicular-application</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>radarveicular-service</artifactId>
<dependencies>
<dependency>
<groupId>com.radarveicular</groupId>
<artifactId>radarveicular-dao</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
</dependencies>
</project>
dao pom:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.radarveicular</groupId>
<artifactId>radarveicular-application</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>radarveicular-dao</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.4</version>
</dependency>
</dependencies>
</project>
I think your problem is that your parent pom.xml shouldn't handle your spring boot project. Meaning, you should have this in it :
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
I recommend that your pom.xml parent only handles your modules and dependencies as pom packaging :
Packaging
<packaging>pom</packaging>
Modules
<modules>
<module>radarveicular-api</module>
<module>radarveicular-service</module>
<module>radarveicular-main</module>
<module>radarveicular-dao</module>
</modules>
Then, you will have your module radarveicular-main with the spring boot parent :
Parent
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
Packaging
<packaging>war</packaging>
I hope this will help you. =)
I feel your question is more towards designing part of multi module.
Multi module management of application can depend on various things.
One thing could be if you want to deploy each sub module as separate module/service based, which can be turned off/on based on need. In that case it would help you if you build each of module as separate war and deploy them separately.
If you have to deploy it as a single war than most straight forward would to create all dependent modules as jar and include them in parent project. In this way a single war will be deployed.
You can have a hybrid approach of above two.
Hopefully i have understood your question correctly. :)
Happy coding!!

Import and Dependency Management in Maven

I was learning about "import" scope in maven and did a sample project below,
Project_1 POM file :
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
project_2 POM file :
<parent>
<groupId>com.sample</groupId>
<artifactId>project1</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
...
...
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.sample</groupId>
<artifactId>project1</artifactId>
<version>1.0-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
However, this throws an error stating that the JUnit packages are not available in Project2. When I remove the dependencyManagement tag from project_1 POM file. Everything works fine.
But as per the maven docs,
This scope is only supported on a dependency of type pom in the
section. It indicates the dependency to be
replaced with the effective list of dependencies in the specified
POM's section. Since they are replaced,
dependencies with a scope of import do not actually participate in
limiting the transitivity of a dependency.
I've done as mentioned in the docs, what went wrong here?
It looks like you're trying to use a bill-of-materials (BOM) POM and import that.
Your Project_1 is the BOM POM in this case, and you include all your project's dependencies in the <dependencyManagement> element. It looks like you're doing this correctly.
To import a BOM POM in your project however, you need both <dependencyManagement> and <dependencies>.
For example, your Project_2 pom.xml should include:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sample</groupId>
<artifactId>project1</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencyManagement>
<dependencies>
<!-- Imports the bill-of-materials POM. -->
<dependency>
<groupId>com.sample</groupId>
<artifactId>bom</artifactId>
<version>1.0-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- Add a dependency from the BOM POM.
Note: no version or scope is required, it's inherited from the BOM POM! -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
</project>
Here is the BOM POM definition for the above example:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sample</groupId>
<artifactId>bom</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
There is documentation about this on the Apache Maven website (search for "bom").
Update:
BOM POMs and parent POMs are similar but different. BOM POMs are meant purely for importing dependencies into your project. Parent POMs are meant for multi-module projects, and allow you to define Maven coordinates, plugins, and dependencies that will be inherited by all of modules using the parent POM.
Here is an example of using an inherited dependency from a parent POM. Note that there are several elements included here that don't make sense in a BOM POM.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.sample</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>project1</artifactId>
<packaging>jar</packaging>
<dependencies>
<!-- Inherited from the parent POM. -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
</project>
Here is the parent POM definition for the above example:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sample</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>child1</module>
<module>child2</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!--Global dependencies...-->
</dependencies>
<build>
<pluginManagement>
<!--Plugins...-->
</pluginManagement>
</build>
</project>
First Make sure you are using Maven version 2.0.9 or later that supports this functionality.
Second your Project1 Pom.Xml is missing the attribute "packaging" Make sure your packaging and type are of the same name so that it inherits all the dependencies.
Also your artifact Id and group ID has to be the same as your project 1's Pom.xml file for it to import the dependency
When in doubt refer to this link
https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Importing_Dependencies

maven multi-module project with jersey & spring

I've been referring stackoverflow questions for quite some time. now i have got one of my own. I'm basically trying to create a Rest webservice with Jersey 2.22.1. I'm using this archetype to create a maven jersey project from section 1.4 here
mvn archetype:generate -DarchetypeArtifactId=jersey-quickstart-webapp \
-DarchetypeGroupId=org.glassfish.jersey.archetypes -DinteractiveMode=false \
-DgroupId=com.example -DartifactId=simple-service-webapp -Dpackage=com.example \
-DarchetypeVersion=2.22.1
This generates a sample rest jersey webservice. Now i want to access mongodb through this webservice. I'm trying to create a separate jar maven project that uses spring to connect to mongodb. its structure is
com.yuvi.mongo.service
com.yuvi.mongo.dao
I have a main class inside service which loads spring context , retrieve dao bean n performs db access.
Now i want to connect these two projects.
I created a parent project, included these 2 as its modules and added mongo as dependency in service. but when i try to access package com.yuvi.mongo from my service it shows package not found error. I created two simple maven projects and followed same procedure for creating multi-module projects that was working perfectly. but when i try it here i can't access the package. I'm displaying my pom files here.,
parent pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yuvi</groupId>
<artifactId>Parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>Mongo</module>
<module>RService</module>
</modules>
</project>
Mongo pom
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.yuvi</groupId>
<artifactId>Parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.yuvi.mongo</groupId>
<artifactId>Mongo</artifactId>
<name>Mongo</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring-version>4.2.3.RELEASE</spring-version>
<mongodb-version>1.8.1.RELEASE</mongodb-version>
</properties>
<dependencies>
<!-- Spring 4 dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>${mongodb-version}</version>
</dependency>
</dependencies>
</project>
RService pom
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.yuvi</groupId>
<artifactId>Parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.yuvi.service</groupId>
<artifactId>RService</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>RService</name>
<build>
<finalName>RService</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.yuvi.mongo</groupId>
<artifactId>Mongo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
<!-- uncomment this to get JSON support
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>
-->
</dependencies>
<properties>
<jersey.version>2.22.1</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

Categories

Resources