separate versioning for child project that depends on other projects - maven

I have pom.xml in the child project of the multimodule project
<?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>Parent-POM</artifactId>
<groupId>parent.package</groupId>
<version>2.0.0-RELEASE</version>
</parent>
<artifactId>service</artifactId>
<packaging>pom</packaging>
<name>Service</name>
<dependencies>
<dependency>
<groupId>parent.package</groupId>
<artifactId>Server</artifactId>
<version>${project.version}</version>
</dependency>
...
I want to have different versioning for the child project, but preserve the version for dependency packages. Basically I want this
<?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>Parent-POM</artifactId>
<groupId>parent.package</groupId>
<version>2.0.0-RELEASE</version>
</parent>
<artifactId>service</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<name>Service</name>
<dependencies>
<dependency>
<groupId>parent.package</groupId>
<artifactId>Server</artifactId>
<version>2.0.0-RELEASE</version>
</dependency>
How to achieve that?
If I specify <version> for service project it affects ${project.version} of the dependency.

You can use ${parent.version}.
This references the version of the parent.

Related

Generate parent pom for existing project modules

I have following project modules. Each of the modules have their own pom.xml . Now I want to generate a parent POM (for existing modules like below) . Is there a way to do that? I know there is a way we can do if we are creating brand new project but my need here is I already have a project structure.
project-module-1
project-module-2
project-module-3
project-module-4
Thank you for your help
You need revising your exising project a little, it is not too hard.
Your project structure like this
parent pom.xml
<?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</groupId>
<artifactId>sample-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<description>sample</description>
<url>http://example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<modules>
<module>project-module-1</module>
<module>project-module-2</module>
<module>project-module-3</module>
<module>project-module-4</module>
</modules>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
</plugins>
</build>
</project>
Child pom.xml
<?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.example</groupId>
<artifactId>sample-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<groupId>com.example</groupId>
<artifactId>project-module-1</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<description>sample</description>
<url>http://example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
</plugins>
</build>
</project>
Sample project source code: https://github.com/donhuvy/maven_parent_childs/archive/master.zip

Automatically build Maven Project in case of a dependency upgrade

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>

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.

Update version properties during Maven release prepare

How do I update properties during a mvn release:prepare
My pom.xml looks like this:
<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.mycompany.proj</groupId>
<artifactId>mavenproject1</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<proj-dependency-version>1.0.0-SNAPSHOT</proj-dependency-version>
</properties>
<dependencies>
<dependency>
<groupId>my.proj.dependency</groupId>
<artifactId>proj-dependency</artifactId>
<version>${proj-dependency-version}</version>
</dependency>
</dependencies>
</project>
I found a solution via the maven version:update_properties plugin

Resources