Spring Boot multi module Maven project deployment - maven

We have multi module Maven project with following modules:
Commons
Model
Repository
Service
Web
We've googled around and we didn't found a solution how to make one executable jar when project has this kind of structure.
Here is the parent 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>Model</module>
<module>Web</module>
<module>Service</module>
<module>Repository</module>
<module>Common</module>
</modules>
<packaging>pom</packaging>
<name>Api</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<mssql.jdbc.driver.version>4.2</mssql.jdbc.driver.version>
<apache.commons.lang.version>3.3.2</apache.commons.lang.version>
<apache.commons.collection.utils.version>4.1</apache.commons.collection.utils.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-envers</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
</project>
So, very basic parent pom.xml. We know that we can't use Spring Boot Maven plugin when packaging is pom, so we've defined it in pom.xml from Web module:
<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">
<parent>
<artifactId>...</artifactId>
<groupId>...</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>...</groupId>
<artifactId>...</artifactId>
<packaging>jar</packaging>
<name>...</name>
<dependencies>
<dependency>
<groupId>...</groupId>
<artifactId>Service</artifactId>
<version>${project.version}</version>
</dependency>
<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>
<version>1.4.1.RELEASE</version>
<configuration>
<mainClass>...App</mainClass>
</configuration>
<!--<executions>-->
<!--<execution>-->
<!--<goals>-->
<!--<goal>repackage</goal>-->
<!--</goals>-->
<!--</execution>-->
<!--</executions>-->
</plugin>
</plugins>
</build>
</project>
The rest of pom.xmls from other modules are pretty much the same: Service's pom.xml includes Repository.jar, Repository includes Model and Common.
Problem 1: when we run mvn install, plugin creates fat jar in target folder of web module. But when we try to run it with java -jar name-of-jar it gives java.lang.NoClassDefFoundError about some class from Service module - and you can see from Web's pom.xml that Service module is included (there is also a Service.jar packed as library in Web.jar)
Problem 2: what's interesting is that Service.jar inside of Web.jar also contains pretty much all the dependencies that are already present in Web.jar - basically they are duplicated.
Did anyone managed to create executable fat jar using Spring Boot Maven plugin and Maven Module project structure?

Problem 1 & 2
You should not have devtools as dependency in your parent pom.
Move the following to the web/spring-boot module:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
If you look at devtools's pom, it includes the following:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
So your service jar ends up with some spring boot dependencies.

Related

Dependency activiti-spring-boot-starter not found

I'm starting the spring-boot and the activiti dependency cant' download from this repo - https://repo.maven.apache.org/maven2
The error - Could not find artifact org.activiti:activiti-spring-boot-starter:pom:unknown in central (https://repo.maven.apache.org/maven2)
<?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>[enter image description here][1]
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.10</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.activiti</groupId>
<artifactId>activiti-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</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>
The error shows that the version is not known:
Could not find artifact org.activiti:activiti-spring-boot-starter:pom:unknown.
You could specify a version like so:
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring-boot-starter</artifactId>
<version>7.1.0.M6</version>
</dependency>
Which I found here. However are you sure you have the right dependency? Looking at this guide the dependency shown is:
<dependency>
<groupId>org.activiti</groupId>
<artifactId>spring-boot-starter-basic</artifactId>
<version>${activiti.version}</version>
</dependency>
The difference being activiti-spring-boot-starter ->spring-boot-starter-basic

Spring boot app not starting with embedded erver

APPLICATION FAILED TO START
Description:
An attempt was made to call a method that does not exist. The attempt was made from the following location:
org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getWebServer(TomcatServletWebServerFactory.java:183)
The following method did not exist:
org.apache.tomcat.util.modeler.Registry.disableRegistry()V
Maven clean install not working
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.4.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>boot.practice</groupId>
<artifactId>config</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>config</name>
<description>practice project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<!-- <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency> -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</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>
I was finally able to resolve it . The problem was that the spring boot 2.5.0 was using tomcat 9.0.46 by default while my system had tomcat 9.0.0.M15 . So when I ran the application as "run on server" the tomcat 9.0.0.m15 added to my class path and then when ever I was doing run as java applicationit was throwing the error. Clearing the tomcat embedded repo from .M2 path and updating the local system with tomcat 9.0.46 fixed the problem

Spring-boot(maven) actuator,devtools not working

I'm a newbie to spring-boot. Trying to create a simple web application using spring-boot maven. Basic static page display using dependency spring-boot-starter-web worked fine. When I included spring-boot-devtools and spring-boot-starter-actuator in pom.xml it is giving build error Re-run Maven using the -X switch to enable full debug logging.
I tried including spring-webmvc dependency in pom file, still the result is same.
<?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.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.test</groupId>
<artifactId>jspTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>jspTest</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</artifactId>
</dependency>
<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>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</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>
I downloaded this project from spring.io (SPRING INITIALIZR website). Build project is getting failed.
<scope>runtime</scope> of spring-boot-devtools suppose not to be runtime. In fact according to the doc -
Developer tools are automatically disabled when running a fully
packaged application. If your application is launched from java -jar
or if it is started from a special classloader, then it is considered
a “production application”. Flagging the dependency as optional in
Maven or using a customdevelopmentOnly configuration in Gradle (as
shown above) is a best practice that prevents devtools from being
transitively applied to other modules that use your project.
So, change the dependency as below -
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
you can update your pom.xml and change the version number to any previous stable version.It worked for me.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>**xyz_previous_.RELEASE**</version>
<relativePath/> <!-- lookup parent from repository -->

What dependency is missing from this Spring Boot Application?

I would like to create a Spring boot Web application with H2 database and JPA support. But STS throws an error at pom.xml overview :
Missing artifact org.springrfamework.boot:spring-boot-test.jar:1.5.6.RELASE (Click for 49 more)
Here is my pom.xml , I have no idea what's the problem.
<?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>data-demo-CS</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</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>
Actually your ide not getting the dependency, you should clean build again and if still showing error then simply remove that dependencies from your local repository (I. e. ./.m2 if using maven) and clean build your project again then all required dependencies will be downloaded I your local.
Command mvn clean compile package install

Spring Boot Application cannot be resolved to a type but I imported it in pom.xm

I have added the spring boot in pom.xml but still it is giving an error.
Can anyone help me.
It is giving error The import org.springframework.boot.autoconfigure.SpringBootApplication cannot be resolved
and
SpringBootApplication cannot be resolved to a type
My pom. xml is
<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>
<groupId>com.webapp</groupId>
<artifactId>proj1</artifactId>
<packaging>war</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
</parent>
<version>0.0.1-SNAPSHOT</version>
<name>proj1 Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>proj1</finalName>
</build>
</project>
Your pom looks almost correct, but few things to note
No need to add dependency 'javax.servlet-api', it is available from 'spring-boot-starter-web'
And you probably you may be missing the 'spring-boot-maven-plugin', it is not compulsory but you may want that in your project. Where it provides some easy maven goals for you to run.
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
if your running eclipse you can generate your .classpath files just as usual
mvn eclipse:eclipse, once done your ide should look fine
then mvn spring-boot:run should set your application running

Resources