SpringBoot project jar package method packaging and deployment

SpringBoot project jar package method packaging and deployment

Traditional web applications are packaged and deployed, usually in the form of war packages, and then the war packages are deployed to servers such as Tomcat.

After the Spring Boot project is developed, it does support packaging into both JAR files and WAR files. However, officials usually recommend packaging Spring Boot projects into JAR files. This is because Spring Boot has a built-in embedded Tomcat server, allowing the application to run as an independent executable JAR file without deploying to an external Servlet container.

Although Spring Boot also supports packaging into WAR files and deploying to external Servlet containers, this approach is usually not preferred because it adds additional deployment complexity and may not take full advantage of some of the automatic configuration and simplification provided by Spring Boot. Function.

1. Complete plug-in configuration, add configuration in pom.xml file

 <build>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version><!-- version number in configuration -->
                <configuration>
                    <source>1.8</source><!-- Set source codeJDKVersion -->
                    <target>1.8</target><!-- Set the target codeJDKVersion -->
                    <encoding>UTF-8</encoding><!-- Set encoding method -->
                </configuration>
            </plugin>
            <!--maven Packaging plugin-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot.version}</version>
                <configuration>
                    <mainClass>com.example.demo.DemoApplication</mainClass><!-- Configure startup class -->
                    <skip>false</skip><!--Whether to ignore startup classes-->
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build> 

maven-compiler-plugin is a plug-in for Maven, mainly used for code compilation, and provides many configurable options to optimize the compilation process. main effect:

  1. Specify JDK version: You can clearly specify the JDK version used by the project source code and the JVM version that the compiled class library is intended to run, thereby ensuring the consistency and stability of the project in different environments.
  2. Set encoding method: Allows you to set the encoding method of source code and target code to prevent compilation errors or garbled characters caused by inconsistent encoding.
  3. Optimize the compilation process: The compilation process can be controlled in a fine-grained manner. For example, you can set whether to use incremental compilation, whether to generate debugging information, etc., to improve compilation efficiency and code quality.

spring-boot-maven-plugin is a Maven plug-in for Spring Boot projects, which plays a key role in the project’s building and packaging process. main effect:

  1. Package executable JAR/WAR file: This plug-in can package the Spring Boot application into an executable JAR or WAR file.

  2. Specify execution class: This plug-in can specify the class to be executed. If it is not specified, it can automatically detect the main function in the project and start Spring.
    Boot container.

2. Use IDEA development tools for packaging

Open the maven window, double-click the package package project in the Lifecycle column
Insert image description here

The Idea console shows that packaging is successful
Insert image description here
Generate Jar package in Idea project target directory
Insert image description here

3. Jar package deployment

Open the directory where the jar file is located, then open powerShell and execute the command line:

java -jar .\qvtu-web04-0.0.1-SNAPSHOT.jar 

Insert image description here