The Spring Boot project uses Maven to package and bring dependencies

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-pluginplugins for this task.

1. Update the pom.xml file

In your pom.xmlfile, you need to add or update <build>a section to use spring-boot-maven-pluginthe 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-pluginone 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.xmlfile, 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

targetAfter packaging is complete, you will find a file in the project directory .jar. You can java -jarrun this file with the command as follows:

java -jar target/yourproject-0.0.1-SNAPSHOT.jar

Please remember to yourproject-0.0.1-SNAPSHOT.jarreplace 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.

Guess you like

Origin blog.csdn.net/qq_39997939/article/details/131258893