Springboot源码分析之项目结构

摘要:

  • 不管是从IDEA仍是其余的SDS开发工具亦或是https://start.spring.io/ 进行解压,咱们都会获得一样的一个pom.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 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>
关注点
<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath>../../spring-boot-dependencies</relativePath>
      </parent>

说明咱们的工程能够进行改造进行替换掉原来工程的parent标签.html

<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包时候没法正常启动.java

file

在配置文件加上repackage便可linux

<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工具
filegit

mvnw是基于linuxshell脚本命令github

mvnw.cmd是基于windows的脚本命令spring

.mvn包含了 maven下载路径以及版本等信息shell

也就是说咱们彻底不用本身下载maven而是能够直接使用IDEA给咱们提供的工具便可,只不过一般不会这么作。apache

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.jarwindows