Il y a 4 ans -
Temps de lecture 3 minutes
Import Spring Boot Application as a Maven dependency
In this article, I will describe the different steps that allow us to import a Spring Boot application as a Maven dependency. I will detail the issues that we have been through before presenting an adequate solution for this purpose.
The issue:
On a recent project, we used the Spring Boot Maven Plugin which includes a run goal to quickly compile and run our application like so:
mvn spring-boot:run
It works very well for running our applications. However, when we tried to import the spring boot executable JAR as a dependency in another project, it didn’t work.
In order to understand what was happening, we started by checking the JAR generated by the Spring Boot Maven Plugin.
All packages are prepended with BOOT-INF and there is an org package which contains many Spring Boot packages and classes.
A quick reminder about Maven Packaging: When no packaging is declared, Maven assumes the default packaging is JAR.
The JAR generated by the Spring Boot Maven Plugin overrides the default one generated by Maven.
In contrast, this is what a regular JAR file generated by Maven looks like:
The solution:
The solution that we found is to generate another JAR which will be used as a dependency to be imported from other projects.
To achieve this goal, we used the Apache Maven JAR Plugin which allows us to generate the JAR to be imported later:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<goals>
<goal>jar</goal>
</goals>
<phase>package</phase>
<configuration>
<!--to be imported on other projects-->
<classifier>app-to-import</classifier>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.1.3.RELEASE</version>
</plugin>
</plugins>
Now, we got two JARs, `artifactId-version.jar` that will be used to start the Spring Boot application and the second one `artifactId-version-app-to-import.jar` which can be used for import into other projects.
Going further with Maven Classifier:
Since we can now separate the JAR to be imported from the one used for running the Spring Application, we could make more customized changes to our importable JAR to keep only the necessary packages.
For example, we may need to share generic services and object models while excluding application-specific configurations like schedulers, controllers, or security. This can be easily be achieved :
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<goals>
<goal>jar</goal>
</goals>
<phase>package</phase>
<configuration>
<!--to be imported on other projects-->
<classifier>app-to-import</classifier>
</configuration>
<includes>
<include>**/service/*</include>`
<include>**/model/*</include>
</includes>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.1.3.RELEASE</version>
</plugin>`
</plugins>
In order to use the generated JAR that was created above you need to specify the same classifier when importing that project as a dependency:
<dependency>
<groupId>com.project</groupId>
<artifactId>artifactId</artifactId>
<version>1.0</version>
<classifier>app-to-import</classifier>
</dependency>
Links:
- Apache Maven JAR Plugin doc: https://maven.apache.org/plugins/maven-jar-plugin/
- Maven packaging: http://maven.apache.org/pom.html#Maven_Coordinates
- Spring Boot Maven Plugin repackage-classifier: https://docs.spring.io/spring-boot/docs/current/maven-plugin/examples/repackage-classifier.html
Commentaire