Index of / Hasan Ünlü KILINÇ

Fantastic World and Software Fetishist

Science & Art & Philosophy

Contact Me

Name Date
Continuous Delivery With Maven Flatten and Spring Boot

Continuous Delivery With Maven Flatten and Spring Boot


This article for software developer which use java technologies. I try to explain briefly “How to convert your project continuous delivery structure”.

Continuous Delivery is a software engineering approach in which teams produce software in short cycles, ensuring that the software can be reliably released at any time and, when releasing the software, without doing so manually. It aims at building, testing, and releasing software with greater speed and frequency.

This is wikipedia explain for continuous delivery.

So why we need this lifecycle, version system is necessary for every software project and in order to handle this we use Git or SVN. But we try to include deployment process in this cycle , may be little confusion.

In maven project structure , there are 2 type which are single project and multiple modules project. Single project has a single pom.xml as a easy understand. Multiple module has two or more pom.xml and there is inheritance in it.

Like below

In this modular structure, there are difference version approach, for example You developed new feature for your project or bug fixed in the Sub Child One Project 1. And you will deploy your code. Which project version will be increase after deployment? Just Sub Child One Project 1 or Child Project One or Whole Projects. By the way this discussion is still continue among developers. However both system even managed from single version or multiple version you have to write version for every pom.xml.

With Apache Maven 3.5.0 , ${revision} , #{sha1} , ${changelist} came. In this tutorial we will be focused on ${revision}.

Via revision, you can define pom xml without <version> tag.

Example Pom Files

I gave the below example parent and child pom.xml and explaint how it works.

Your parent pom.xml should be like 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>


	<groupId>io.geniusbrain</groupId>
	<artifactId>great</artifactId>
	<version>${revision}</version>
	<packaging>pom</packaging>
	<name>great</name>


	<modules>
		<module>child-module</module>
		<!-- etc.. -->
		<!-- etc.. -->
	</modules>


	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
		<spring.boot.version>2.1.2.RELEASE</spring.boot.version>
		<spring.cloud.config.version>2.1.2.RELEASE</spring.cloud.config.version>
		<!-- etc.. -->
	</properties>


	<build>
			<plugins>
				<plugin>
					<groupId>org.codehaus.mojo</groupId>
					<artifactId>flatten-maven-plugin</artifactId>
					<configuration>
						<updatePomFile>true</updatePomFile>
					</configuration>
					<executions>
						<execution>
							<id>flatten</id>
							<phase>process-resources</phase>
							<goals>
								<goal>flatten</goal>
							</goals>
						</execution>
						<execution>
							<id>flatten.clean</id>
							<phase>clean</phase>
							<goals>
								<goal>clean</goal>
							</goals>
						</execution>
					</executions>
				</plugin>
			</plugins>
	</build>

	<dependencyManagement>
		<dependencies>

			<dependency>
				<groupId>org.projectlombok</groupId>
				<artifactId>lombok</artifactId>
				<version>${lombok.version}</version>
			</dependency>

			<dependency>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-dependencies</artifactId>
				<version>${spring.boot.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>

			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-config-dependencies</artifactId>
				<version>${spring.cloud.config.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
			
			<!-- etc.. -->		
			<!-- etc.. -->

		</dependencies>
	</dependencyManagement>

	<dependencies>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
		</dependency>
	</dependencies>


</project>

Your child pom xml should be like 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>
		<groupId>io.geniusbrain</groupId>
		<artifactId>great</artifactId>
		<version>${revision}</version>
	</parent>

	<name>child-module</name>
	<artifactId>child-module</artifactId>
	<packaging>jar</packaging>


	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<version>${spring.boot.version}</version>
			</plugin>
		</plugins>
		<pluginManagement>
			<plugins>
				<!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
				<plugin>
					<groupId>org.eclipse.m2e</groupId>
					<artifactId>lifecycle-mapping</artifactId>
					<version>1.0.0</version>
					<configuration>
						<lifecycleMappingMetadata>
							<pluginExecutions>
								<pluginExecution>
									<pluginExecutionFilter>
										<groupId>
											org.codehaus.mojo
										</groupId>
										<artifactId>
											flatten-maven-plugin
										</artifactId>
										<versionRange>
											[1.1.0,)
										</versionRange>
										<goals>
											<goal>flatten</goal>
										</goals>
									</pluginExecutionFilter>
									<action>
										<ignore></ignore>
									</action>
								</pluginExecution>
							</pluginExecutions>
						</lifecycleMappingMetadata>
					</configuration>
				</plugin>
			</plugins>
		</pluginManagement>
	</build>

	<dependencies>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<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.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jetty</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>


		<!-- etc.. -->
		<!-- etc.. -->
		<!-- etc.. -->

	</dependencies>




</project>

Moreover you have to give revision as a parameter with maven command

-Drevision=3.0.0 clean install

After the whole procjets build also you can build your sub project with spring-boot

-Drevision=3.0.0 clean install spring-boot:repackage

So, If you configured your deployment repostory, can also use deploy command

-Drevision=3.0.0 clean deploy

This is end of the article so I hope after the applied all steps will see below output 🙂

[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.595 s
[INFO] Finished at: 2019-08-13T14:23:49+03:00
[INFO] -----------------------------------------------------------------------

Reference Link : https://www.geniusbrain.io/continuous-delivery-with-maven-flatten-and-spring-boot/

About Post Author


Tags: , , ,


Leave a Reply