In this blog, I will introduce how to use Maven to package a Spring Boot project and its dependencies into an executable jar file. We'll use Spring Boot's spring-boot-maven-plugin
plugins for this task.
1. Update the pom.xml file
In your pom.xml
file, you need to add or update <build>
a section to use spring-boot-maven-plugin
the plugin. Here is an example configuration:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
In this configuration, <includeSystemScope>true</includeSystemScope>
the plugin is instructed to include system-wide dependencies. <goal>repackage</goal>
is spring-boot-maven-plugin
one of the main goals of the plugin, which will create a new jar or war file containing the original application and all related dependencies.
2. Use Maven to package the project
After saving your pom.xml
file, you can package your project with the following command:
mvn clean package
After running this command, Maven will compile your project, run any tests, and package your application and all its dependencies.
3. Run your application
target
After packaging is complete, you will find a file in the project directory .jar
. You can java -jar
run this file with the command as follows:
java -jar target/yourproject-0.0.1-SNAPSHOT.jar
Please remember to yourproject-0.0.1-SNAPSHOT.jar
replace with your actual jar file name.
Through the above steps, you have successfully packaged the Spring Boot project and its dependencies into an executable jar file. Hope this blog is helpful to you! If you have any questions, please leave a message below.