1. 程式人生 > >Springboot原始碼分析之專案結構

Springboot原始碼分析之專案結構

摘要:

  • 無論是從IDEA還是其他的SDS開發工具亦或是https://start.spring.io/ 進行解壓,我們都會得到同樣的一個pom.xml檔案

xml <?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.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.github.dqqzj</groupId> <artifactId>springboot</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot</name> <packaging>jar</packaging> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>

parent標籤的含義

  • 找到本地倉庫的spring-boot-starter-parent座標

xml <?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-dependencies</artifactId> <version>2.1.6.RELEASE</version> <relativePath>../../spring-boot-dependencies</relativePath> </parent> <!-- 省略無關程式碼...... --> <build> <!-- 省略無關程式碼...... --> <pluginManagement> <plugins> <!-- 省略無關程式碼...... --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <id>repackage</id> <goals> <goal>repackage</goal> </goals> </execution> </executions> <configuration> <mainClass>${start-class}</mainClass> </configuration> </plugin> <!-- 省略無關程式碼...... --> </plugins> </pluginManagement> </build> </project>

關注點

xml <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.1.6.RELEASE</version> <relativePath>../../spring-boot-dependencies</relativePath> </parent>

說明我們的工程可以進行改造進行替換掉原來工程的parent標籤.

xml <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <id>repackage</id> <goals> <goal>repackage</goal> </goals> </execution> </executions> <configuration> <mainClass>${start-class}</mainClass> </configuration> </plugin>

repackage必須要有否則打jar包時候無法正常啟動.

在配置檔案加上repackage即可

    <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>

IDEA自帶maven工具

mvnw是基於linuxshell指令碼命令

mvnw.cmd是基於windows的指令碼命令

.mvn包含了 maven下載路徑以及版本等資訊

也就是說我們完全不用自己下載maven而是可以直接使用IDEA給我們提供的工具即可,只不過通常不會這麼做。

repackage的作用

  • 官方文件

正常情況下打包會生成springboot-0.0.1-SNAPSHOT.jar.original,springboot-0.0.1-SNAPSHOT.jar這樣的2個檔案,而repackage的作用就是將springboot-0.0.1-SNAPSHOT.jar.original重新打包為我們可以執行的springboot-0.0.1-SNAPSHOT.jar