example of Multi-module maven with Spring Boot - spring

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

Related

How to use other's dependency from github packages in my own maven project

Recently, I try to add a maven dependency from github packages and exactly do what the pages said to me. That page looks like this.
this is my 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 https://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>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>ssotest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ssotest</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
...
<dependency>
<groupId>io.github.kaixindeken.TwTSSO</groupId>
<artifactId>twtsso</artifactId>
<version>0.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
and then I got this
Cannot resolve io.github.kaixindeken.TwTSSO:twtsso:0.3
Much appreciated if anybody can tell me why is that and how to fix it
Your Maven will automatically download all dependencies from Maven Central. There, the last (and only) version that currently exists is 0.2: https://search.maven.org/artifact/io.github.kaixindeken/TwTSSO
You could probably use the instructions from the README to install version 0.3 locally.

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>

Why does pom.xml file give an unknown error

I,m trying to learn spring boot and install spring boot to eclipse IDE. When I create my first project it shows an error in first line of pom.xml. It shows as unknown error.
I tried many solutions from stackoverflow. But none of them work for me.
Maven pom.xml error in eclipse
When maven says "resolution will not be reattempted until the update interval of MyRepo has elapsed", where is that interval specified?
My pom.xml as below.
<?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>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Can anyone help me for resolve this.
I got the same problem 2 weeks ago. Then, I fixed it with adding a line as below
<properties>
<!-- ... -->
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
</properties>
And it seems the error was fixed in Eclipse already.
Just update your Eclipse or STS4. You may see m2e connector is involved in the update list.

Multi-module Spring Boot project and inherited dependencies

Trying to understand why my effective pom is missing inherited dependencies.
I have a very simple set up, in fact, identical to the one shown in "Creating a Multi Module Project" guide on Spring.io.
The root 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>org.springframework</groupId>
<artifactId>gs-multi-module</artifactId>
<version>0.1.0</version>
<packaging>pom</packaging>
<modules>
<module>library</module>
<module>application</module>
</modules>
</project>
The library module pom.xml is this (notice my addition of com.google.http-client dependency:
<?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>gs-multi-module-library</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client</artifactId>
<version>1.22.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
The module that lists the "library" module as a dependency is 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>com.example</groupId>
<artifactId>gs-multi-module-application</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>gs-multi-module-library</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
YET, the effective pom.xml does NOT show com.google.http-client as one of the dependencies for this last "application" module:
cd application
mvn help:effective-pom
Why is this happening and how do I make sure that regular maven dependency transitivity still works?
I tried the sample provided and I am seeing dependencies resolved as expected:
Effective POM(includes library reference):
Resolved dependencies(includes google-httpclient):
Here is the answer:
help:effective-pom does NOT show transitive dependencies.
To show all the dependencies that are brought in as a result of transitive dependencies, use:
mvn dependency:tree

Spring boot default initalization relativePath

I have used the spring boot initializer to generate a project.
What does this line do? Why is it used? What would happen if its not used?
<relativePath/> <!-- lookup parent from repository -->
An extract from The pom looks 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>com.rob.jpa.troubleshooting</groupId>
<artifactId>jpademo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>jpademo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</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>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
...etc
It's the relative path from the module's pom.xml to the parent's pom.xml
(Ref: Maven Documentation)
In your case, its not required. because parent's pom is taken from JAR file.
Scenario :
If we have Application1 as parent to the module Application2, then we need to specifiy to locate the pom of Parent in the module's pom.xml

Resources