Maven deploy and versions of dependencies - maven

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.

Related

separate versioning for child project that depends on other projects

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.

Multi module liquibase project

I have a multi module spring boot application backed by maven. There is a parent pom.xml which defines all the dependencies. There are 2 modules A and B. Module B is dependent on module A. Both the modules are spring applications and I have repackaged them as jars so that the interdependency between the modules would work. My application runs fine, but I have a problem while generating the liquibase diffs. My module A and B have got all the domain/hibernate entities. The Parent does not have any code. It just has the pom.xml.
When I do a mvn liquibase:diff from the parent, I get
Error setting up or running Liquibase: javax.persistence.PersistenceException: Unable to resolve persistence unit root URL: class path resource [] cannot be resolved to URL because it does not exist
When I do a mvn liquibase:diff from the module B, I get (I have the dependency defined in the parent pom)
Failed to execute goal org.liquibase:liquibase-maven-plugin:3.5.5:diff (default-cli) on project leave: Execution default-cli of goal org.liquibase:liquibase-maven-plugin:3.5.5:diff failed: Plugin org.liquibase:liquibase-maven-plugin:3.5.5 or one of its dependencies could not be resolved: Could not find artifact org.springframework.boot:spring-boot-starter-data-jpa:jar:1.0-SNAPSHOT -> [Help 1]
I dont know if restructuring the jars in the parent pom with the following code which got the application working is causing this problem.
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>repackage</id>
<configuration>
<classifier>exec</classifier>
</configuration>
</execution>
</executions>
</plugin>
Parent POM.xml and it has got a bit list of all the dependencies and plugins
<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>org.xxx</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
</parent>
<modules>
<module>A</module>
<module>B</module>
</modules>
A Pom.xml
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.xxx</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>A</artifactId>
<packaging>jar</packaging>
<properties>
<java.version>1.8</java.version>
</properties>
</project>
b Pom.xml
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.xxx</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>B</artifactId>
<packaging>jar</packaging>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.xxx</groupId>
<artifactId>A</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>

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>

example of Multi-module maven with Spring Boot

I want to know how i can create a multi module project with maven using Spring Boot.
Can someone show me an example of the "parent-pom" and "child-pom"?
Thanks
This question is too simple to answer or too complex to be understood, otherwise I cannot explain why there are no answers.
I would go for a solution that uses a folder structure like the following
project folder
|---pom.xml
|---module1
|---pom.xml
|---src
|---module2
|---pom.xml
|---src
|---module3
|---pom.xml
|---src
I would define the pom.xml of the project with a pom packaging, e.g.:
<?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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.7-RELEASE</version>
<relativePath/>
</parent>
<groupId>com.myproject</groupId>
<artifactId>project</artifactId>
<packaging>pom</packaging>
<version>0.0.1-SNAPSHOT</version>
<modules>
<module>module1</module>
<module>module2</module>
<module>module3</module>
</modules>
<name>MySupercoolProject</name>
<description>What else?</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!-- Put here dependencies shared by your project -->
</dependencies>
</project>
As you see, the parent of your project will be a spring-boot-starter-parent.
Then, the pom.xml of one of your modules will be, for instance:
<?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.myproject</groupId>
<artifactId>project</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.myproject</groupId>
<artifactId>module1</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Application</name>
<description>SudenGut application</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<start-class>com.myproject.MyApplication</start-class>
<java.version>1.8</java.version>
<tomcat.version>8.0.24</tomcat.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--- this will not be enough to provide a cool app :) -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins
</build>
</project>
where the main application is MyApplication.java in package com.myproject of module1.
I would use the jar packaging for the other modules without a main application, unless you have other submodules (i.e., pom packaging again).
Modules as a spring boot project
Spring boot usually works with parent. But if you create multi module project and if you want to use spring boot on some of your module there is a important question, which one will be the parent? Of course your main project must be parent. So you should use spring boot as a dependency in your module.
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.9.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
For detailed information please review following documentation;
https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-build-systems.html#using-boot-maven-without-a-parent
#juanhl it is so simple. You can create Multi-module maven project with Spring Boot.
Steps :
1)Create Maven project.The Parent project's pom.xml is like following,
<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.springmultumoduleweb</groupId>
<artifactId>multimodule-web-app</artifactId>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<modules>
<module>multimodule-api</module>
<module>multimodule-service</module>
<module>EarModule</module>
</modules>
<version>0.0.1-SNAPSHOT</version>
</project>
2) add maven module to the parent modules
child modules may have one or more dependencies of other modules and packaging type may be jar or war.
3)create one more module to generate ear file.(packaging type : ear )
which contains dependencies of all modules as below,
<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.springbootparent.demo</groupId>
<artifactId>com.springbootparent.demo</artifactId>
<version>0.0.1-SNAPSHOT</version
</parent>
<artifactId>com.springboot.ear</artifactId>
<packaging>ear</packaging>
<dependencies>
<dependency>
<groupId>com.springbootparent.demo</groupId>
<artifactId>com.springboot.service1</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.springbootparent.demo</groupId>
<artifactId>com.springboot.service2</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.springbootparent.demo</groupId>
<artifactId>com.springboot.war1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>war</type>
</dependency>
<dependency>
<groupId>com.springbootparent.demo</groupId>
<artifactId>com.springboot.war2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>war</type>
</dependency>
</dependencies>
</project>
4) clean and install maven parent project.
5) deploy created ear/war file on server.
(for ear file you have to deploy on Enterprise Application server)
-Good luck

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