Automatically build Maven Project in case of a dependency upgrade - maven

I am using Jenkins for CI/CD. Jenkins is integrated with my GIT account and as soon as there is an update in the code, Jenkins will build it automatically.
Lets suppose I am using Spring Framework and the version of the Spring framework gets updated. How can I use Jenkins and Maven to automatically build all the projects that were using the Spring framework?? Is there any plugin that will do it? Would be great if someone can share some tutorials/urls etc for the same?

Create your own parent pom and use a dependency management section
<?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.greg</groupId>
<artifactId>ear-example</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
Then in your child pom you will use the dependency but not specify a version, i.e. the version will be specified in the parent once for all children. You still need to include the dependency in each module.
<?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.greg</groupId>
<artifactId>ear-example</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>example-ear</artifactId>
<packaging>ear</packaging>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
</dependency>
</dependencies>

Related

How to reference sibling module in a test for my maven spring boot multi-module project?

How can I include a sibling module as a test dependency for my spring boot tests in maven?
I have a little multi-module maven project with a spring boot web app (querying-api ), a common library (domain-definitions), and an initializing script (database-loader).
I'm writing test cases in my querying-api module to test my API, and I would like to run my database-loader service before the tests begin to load the database with predefined test data, simultaneously testing that the loader still works. However I'm getting the following error when running mvn clean package:
[ERROR] Failed to execute goal on project querying-api: Could not resolve dependencies for project com.my.little.package:querying-api:jar:1.0.0: Failure to find com.my.little.package:database-loader:jar:1.0.0 in http://mycompanysnexusrepo.com/nexus/content/repositories/EAJAVA/ was cached in the local repository, resolution will not be reattempted until the update interval of EAJAVA has elapsed or updates are forced -> [Help 1]
The tests have no problem referencing the common domain-definitions library...
Parent POM File:
<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>
<packaging>pom</packaging>
<name>my-little-project</name>
<description>Project for my little microservice</description>
<groupId>com.my.little.package</groupId>
<artifactId>my-little-project</artifactId>
<version>1.0.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring.security.version>2.3.5.RELEASE</spring.security.version>
<springfox.version>2.7.0</springfox.version>
</properties>
<modules>
<module>domain-definitions</module>
<module>database-loader</module>
<module>querying-api</module>
</modules>
<distributionManagement>
<repository>
<id>EAJAVA</id>
<url>http://mycompanysnexusrepo.com/nexus/content/repositories/EAJAVA/</url>
</repository>
</distributionManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.my.little.package</groupId>
<artifactId>domain-definitions</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.my.little.package</groupId>
<artifactId>database-loader</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
querying-api POM file (Spring boot app for querying the database):
<?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>
<artifactId>querying-api</artifactId>
<packaging>jar</packaging>
<name>querying-api</name>
<description>Spring boot app for querying the database</description>
<parent>
<groupId>com.my.little.package</groupId>
<artifactId>my-little-project</artifactId>
<version>1.0.0</version>
</parent>
<dependencies>
<dependency>
<groupId>com.my.little.package</groupId>
<artifactId>domain-definitions</artifactId>
</dependency>
<dependency>
<groupId>com.my.little.package</groupId>
<artifactId>database-loader</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>querying-api</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
domain-definitions POM file (Common library):
<?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>
<groupId>com.my.little.package</groupId>
<artifactId>my-little-project</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>domain-definitions</artifactId>
<name>domain-definitions</name>
<description>Classes to define the structure of the tables</description>
</project>
database-loader POM file:
<?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>
<artifactId>database-loader</artifactId>
<packaging>jar</packaging>
<name>database-loader</name>
<description>Java script to load data into the database for the first time</description>
<parent>
<groupId>com.my.little.package</groupId>
<artifactId>my-little-project</artifactId>
<version>1.0.0</version>
</parent>
<dependencies>
<dependency>
<groupId>com.my.little.package</groupId>
<artifactId>domain-definitions</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
This might be because of the library jar not found in the local repository. You need to first execute :
mvn clean install.
This will install the lib jar in your local repository and maven can easily find it when you execute package goal.
Explained here: How are "mvn clean package" and "mvn clean install" different?
Furthermore, if you are using the sibling module only in tests then you can define the scope as test in dependency.
<dependency>
<groupId>com.my.little.package</groupId>
<artifactId>domain-definitions</artifactId>
<scope>test</scope>
</dependency>

How can I depend on a library with transitive dependencies which are adjusted by Maven dependency management, at the same versions as adjusted?

Consider the following project POM for a Java library:
<?xml version="1.0" encoding="UTF-8"?>
<?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>foo</groupId>
<artifactId>lib</artifactId>
<version>1</version>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>
</dependencies>
</project>
As desired, this project depends on commons-codec version 1.10:
$ mvn dependency:list | grep commons-codec
[INFO] commons-codec:commons-codec:jar:1.10:compile
However, if this library is used as a dependency in a downstream project, the version of commons-codec inherited transitively will be 1.9, because org.apache.httpcomponents:httpclient:4.5.3 depends on 1.9, not 1.10.
Here is an application POM which illustrates the issue:
<?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>foo</groupId>
<artifactId>app</artifactId>
<version>1</version>
<dependencies>
<dependency>
<groupId>foo</groupId>
<artifactId>lib</artifactId>
<version>1</version>
</dependency>
</dependencies>
</project>
And the resolved dependencies:
mvn dependency:list | grep commons-codec
[INFO] commons-codec:commons-codec:jar:1.9:compile
Is this a bug in Maven? Or intended behavior?
If intended, is there a way—without hardcoding transitive dependencies in either POM—to inherit the versions as they are resolved during the build of the library component?
One solution is to inherit the dependency management of the library, like so:
<?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>foo</groupId>
<artifactId>app</artifactId>
<version>1</version>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>foo</groupId>
<artifactId>lib</artifactId>
<version>1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>foo</groupId>
<artifactId>lib</artifactId>
<version>1</version>
</dependency>
</dependencies>
</project>
And then it works:
mvn dependency:list | grep commons-codec
[INFO] commons-codec:commons-codec:jar:1.10:compile
But is this the best/only solution? It seems unfortunate/unintended if one must always do this with every dependency to ensure that transitive versions actually match.
I have also posted this code as a gist.

Merge multiple spring web projects into one deployable war

Please help to find solution to merge two or more spring web projects into single deployable war file. I have following projects
myproject
a. core
- src
- pom.xml
- packaging = jar
b. dao
- src
- pom.xml
- packaging = jar
- dependency on core project
c. service
- src
- pom.xml
- packaging = jar
- dependency on core and dao projects
d. salem-web
- src
- pom.xml
- packaging = war
- dependency on core, dao and service projects
So far it is good. But I have a new requirement, for different customers we can have (customer specific features) salem-web-customer1, salem-web-customer2 etc..
e. service-customer1
- src
- pom.xml
- packaging = jar
- dependency on core and dao projects
f. salem-web-customer1
- src
- pom.xml
- packaging = war
- dependency on core, dao and service-customer1 projects
We need to deploy salem-web & salem-web-customer1 as one package for customer1 and so on for other customers.
I already tried solutions provided by maven overlays and other threads without any luck. Could you please suggest the correct configuration (if possible) to merge two web projects into one.
There are a few possibilities, you could do it like this:
Have a 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.greg</groupId>
<artifactId>salem</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>core</module>
<module>dao</module>
<module>service</module>
<module>service-customer1</module>
<module>salem-web</module>
<module>salem-web-customer1</module>
</modules>
</project>
Have projects with dependencies for each jar and war project, for 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.greg</groupId>
<artifactId>service-customer1</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>service-customer1</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.greg</groupId>
<artifactId>core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.greg</groupId>
<artifactId>dao</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
And for a war 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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.greg</groupId>
<artifactId>salem</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>salem-web-customer1</artifactId>
<packaging>war</packaging>
<name>salem-web-customer1 Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>com.greg</groupId>
<artifactId>core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.greg</groupId>
<artifactId>dao</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.greg</groupId>
<artifactId>service-customer1</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>salem-web-customer1</finalName>
</build>
</project>
When you build the whole project you will have all the wars you need in the different target folders:
./dao/target/dao-1.0-SNAPSHOT.jar
./salem-web/target/salam-web.war
./salem-web-customer1/target/salem-web-customer1/WEB-INF/lib/core-1.0-SNAPSHOT.jar
./salem-web-customer1/target/salem-web-customer1/WEB-INF/lib/dao-1.0-SNAPSHOT.jar
./salem-web-customer1/target/salem-web-customer1/WEB-INF/lib/service-customer1-1.0-SNAPSHOT.jar
./salem-web-customer1/target/salem-web-customer1.war
./service/target/service-1.0-SNAPSHOT.jar
./service-customer1/target/service-customer1-1.0-SNAPSHOT.jar

Maven inherit dependencies of dependency module

Currently I have a project where module B depends on module A.
Module A would be:
<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.A</groupId>
<artifactId>P</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>A</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>A</name>
<build>
<sourceDirectory>${sourceDirectory}/A/src</sourceDirectory>
</build>
<dependencies>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>5.0.0</version>
<optional>true</optional>
</dependency>
</dependencies>
</project>
Module B would be:
<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.A</groupId>
<artifactId>P</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>B</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>B</name>
<build>
<sourceDirectory>${sourceDirectory}/B/src</sourceDirectory>
</build>
<dependencies>
<dependency>
<groupId>com.A</groupId>
<artifactId>C</artifactId>
<version>1.0.0</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.A</groupId>
<artifactId>A</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>
In order to compile B I need the classes in a-dep. If I manually add a-dep as a dependency everything compiles fine. But the project needs me to depend on A to get to a-dep instead of explicitly declare a-dep.
Is there a way to get the classes from a-dep by depending on module A?
was able to solve it by removing the true tag as it prevents B to be able to see the dependency.

Maven deploy and versions of dependencies

When I run mvn deploy on a project where the version of a dependency in the child pom is defined as a property in the parent pom, the version number is not expanded in the deployed artifact.
In this example ${spring.version} is not expanded. Can I make maven expand the version property?
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>TestProject</groupId>
<artifactId>TestProject</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>test</module>
</modules>
<properties>
<spring.version>3.2.2.RELEASE</spring.version>
</properties>
<distributionManagement>
<snapshotRepository>
<id>dips_snapshot</id>
<url>http://mavenrepo.dips.local/repository/dips_snapshot</url>
</snapshotRepository>
</distributionManagement>
</project>
Child 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>TestProject</groupId>
<artifactId>subproject</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>TestProject</groupId>
<artifactId>TestProject</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
Deployed 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>TestProject</groupId>
<artifactId>subproject</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>TestProject</groupId>
<artifactId>TestProject</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
</project>
It seems you need to install locally (or even deploy) the parent pom. To do so you can simply run:
mvn clean install -N
in your parent project directory. The -N option is not mandatory, it only stop the recursion (so it will be very fast since only your parent pom will installed)
I prefer a different project organization, where parenthood and aggregation are separated. I use an aggregator project which contains my plain projects as modules and a parent project which is also a module of my aggregator project. This parent project is shared between all the plain projects and the aggregator. My parent and plain projects are stored in subdirectories of the aggregator project directory and the relativePath of each parent element is set accordingly.
This organization better separates concerns and ensures that inter-project dependencies are handled correctly, as long as you perform your builds from the aggregator project directory.

Resources