I'm currently try to structure my Spring Boot Maven application with multiple pom files. I have two applications which needs to share a lot of classes. I have the following structure:
.
├── application1
| |── src
│ └── pom.xml
|── application2
| |── src
│ └── pom.xml
|── shared
| └── src
| └── pom.xml
|
└── pom.xml
The pom in the root directory 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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/>
</parent>
<groupId>nl.example.parent</groupId>
<artifactId>example-parent</artifactId>
<packaging>pom</packaging>
<version>1.0.0-SNAPSHOT</version>
<!-- sub modules -->
<modules>
<module>application1</module>
<module>application2</module>
<module>shared</module>
</modules>
<properties>
<java.version>1.8</java.version>
<maven.plugins.version>2.22.1</maven.plugins.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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>exec</classifier>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${maven.plugins.version}</version>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven.plugins.version}</version>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>application1</id>
<modules>
<module>shared</module>
<module>application1</module>
</modules>
</profile>
<profile>
<id>application2</id>
<modules>
<module>shared</module>
<module>application2</module>
</modules>
</profile>
</profiles>
</project>
The pom in both the application1 and application2 directory look 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>nl.example.parent</groupId>
<artifactId>example-parent</artifactId>
<version>1.0</version>
</parent>
<groupId>nl.example.application1</groupId>
<artifactId>example-child</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<dependencies>
<!-- Is this the right way to import the shared module into application1 or application2? -->
<dependency>
<groupId>nl.example.shared</groupId>
<artifactId>example-shared</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</project>
Then I have a shared module of which most classes will be shared between application1 and application2. The pom of the shared directory 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>
<parent>
<groupId>nl.example.parent</groupId>
<artifactId>example-parent</artifactId>
<version>1.0</version>
</parent>
<groupId>nl.example.shared</groupId>
<artifactId>example-shared</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<dependencies>
<!-- Shared dependencies here -->
</dependencies>
</project>
However, when I try to build the specific profile for one of the two applications I get errors for every import that I try to make from the shared module. Obviously I'm doing something wrong but I can't figure out why the classes from the shared folder are not reachable when called from application1? I did include the shared module as dependency in both application1 and application2
Your Parent POM has Spring Boot as parent. But this does not work. You will not get the dependencies in your modules.
You have to configure a project that does not inherit from Spring Boot Parent and add Spring Boot dependencies to dependency management.