温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Springboot源码怎么分析项目结构

发布时间:2021-10-20 10:28:36 来源:亿速云 阅读:119 作者:柒染 栏目:大数据

Springboot源码怎么分析项目结构,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

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>
    <artifactId>spring-boot-starter-parent</artifactId>
    <packaging>pom</packaging>
    <name>Spring Boot Starter Parent</name>
    <description>Parent pom providing dependency and plugin management for applications
                  built with Maven</description>
    <url>https://projects.spring.io/spring-boot/#/spring-boot-starter-parent</url>
    <properties>
      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      <java.version>1.8</java.version>
      <resource.delimiter>@</resource.delimiter>
      <maven.compiler.source>${java.version}</maven.compiler.source>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <maven.compiler.target>${java.version}</maven.compiler.target>
    </properties>
    <build>
      <resources>
        <resource>
          <filtering>true</filtering>
          <directory>${basedir}/src/main/resources</directory>
          <includes>
            <include>**/application*.yml</include>
            <include>**/application*.yaml</include>
            <include>**/application*.properties</include>
          </includes>
        </resource>
        <resource>
          <directory>${basedir}/src/main/resources</directory>
          <excludes>
            <exclude>**/application*.yml</exclude>
            <exclude>**/application*.yaml</exclude>
            <exclude>**/application*.properties</exclude>
          </excludes>
        </resource>
      </resources>
      <pluginManagement>
        <plugins>
          <plugin>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-maven-plugin</artifactId>
            <version>${kotlin.version}</version>
            <executions>
              <execution>
                <id>compile</id>
                <phase>compile</phase>
                <goals>
                  <goal>compile</goal>
                </goals>
              </execution>
              <execution>
                <id>test-compile</id>
                <phase>test-compile</phase>
                <goals>
                  <goal>test-compile</goal>
                </goals>
              </execution>
            </executions>
            <configuration>
              <jvmTarget>${java.version}</jvmTarget>
              <javaParameters>true</javaParameters>
            </configuration>
          </plugin>
          <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
              <parameters>true</parameters>
            </configuration>
          </plugin>
          <plugin>
            <artifactId>maven-failsafe-plugin</artifactId>
            <executions>
              <execution>
                <goals>
                  <goal>integration-test</goal>
                  <goal>verify</goal>
                </goals>
              </execution>
            </executions>
            <configuration>
              <classesDirectory>${project.build.outputDirectory}</classesDirectory>
            </configuration>
          </plugin>
          <plugin>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
              <archive>
                <manifest>
                  <mainClass>${start-class}</mainClass>
                  <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                </manifest>
              </archive>
            </configuration>
          </plugin>
          <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
              <archive>
                <manifest>
                  <mainClass>${start-class}</mainClass>
                  <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                </manifest>
              </archive>
            </configuration>
          </plugin>
          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <configuration>
              <mainClass>${start-class}</mainClass>
            </configuration>
          </plugin>
          <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <configuration>
              <delimiters>
                <delimiter>${resource.delimiter}</delimiter>
              </delimiters>
              <useDefaultDelimiters>false</useDefaultDelimiters>
            </configuration>
          </plugin>
          <plugin>
            <groupId>pl.project13.maven</groupId>
            <artifactId>git-commit-id-plugin</artifactId>
            <executions>
              <execution>
                <goals>
                  <goal>revision</goal>
                </goals>
              </execution>
            </executions>
            <configuration>
              <verbose>true</verbose>
              <dateFormat>yyyy-MM-dd'T'HH:mm:ssZ</dateFormat>
              <generateGitPropertiesFile>true</generateGitPropertiesFile>
              <generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
            </configuration>
          </plugin>
          <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>
          <plugin>
            <artifactId>maven-shade-plugin</artifactId>
            <executions>
              <execution>
                <phase>package</phase>
                <goals>
                  <goal>shade</goal>
                </goals>
                <configuration>
                  <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                      <resource>META-INF/spring.handlers</resource>
                    </transformer>
                    <transformer implementation="org.springframework.boot.maven.PropertiesMergingResourceTransformer">
                      <resource>META-INF/spring.factories</resource>
                    </transformer>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                      <resource>META-INF/spring.schemas</resource>
                    </transformer>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                      <mainClass>${start-class}</mainClass>
                    </transformer>
                  </transformers>
                </configuration>
              </execution>
            </executions>
            <dependencies>
              <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.1.6.RELEASE</version>
              </dependency>
            </dependencies>
            <configuration>
              <keepDependenciesWithProvidedScope>true</keepDependenciesWithProvidedScope>
              <createDependencyReducedPom>true</createDependencyReducedPom>
              <filters>
                <filter>
                  <artifact>*:*</artifact>
                  <excludes>
                    <exclude>META-INF/*.SF</exclude>
                    <exclude>META-INF/*.DSA</exclude>
                    <exclude>META-INF/*.RSA</exclude>
                  </excludes>
                </filter>
              </filters>
            </configuration>
          </plugin>
          <plugin>
            <groupId>org.eclipse.m2e</groupId>
            <artifactId>lifecycle-mapping</artifactId>
            <version>1.0.0</version>
            <configuration>
              <lifecycleMappingMetadata>
                <pluginExecutions>
                  <pluginExecution>
                    <pluginExecutionFilter>
                      <groupId>org.codehaus.mojo</groupId>
                      <artifactId>flatten-maven-plugin</artifactId>
                      <versionRange>[1.0.0,)</versionRange>
                      <goals>
                        <goal>flatten</goal>
                      </goals>
                    </pluginExecutionFilter>
                    <action>
                      <ignore/>
                    </action>
                  </pluginExecution>
                  <pluginExecution>
                    <pluginExecutionFilter>
                      <groupId>org.apache.maven.plugins</groupId>
                      <artifactId>maven-checkstyle-plugin</artifactId>
                      <versionRange>[3.0.0,)</versionRange>
                      <goals>
                        <goal>check</goal>
                      </goals>
                    </pluginExecutionFilter>
                    <action>
                      <ignore/>
                    </action>
                  </pluginExecution>
                </pluginExecutions>
              </lifecycleMappingMetadata>
            </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`标签.

-     <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包时候无法正常启动.
 ![file](https://cache.yisu.com/upload/information/20210524/347/779545.jpg)

在配置文件加上`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`工具

![file](https://cache.yisu.com/upload/information/20210524/347/779546.jpg)

`mvnw`是基于`linux`的`shell`脚本命令

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

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

也就是说我们完全不用自己下载`maven`而是可以直接使用`IDEA`给我们提供的工具即可,只不过通常不会这么做。
#### `repackage`的作用

- [官方文档](https://docs.spring.io/spring-boot/docs/current/maven-plugin/repackage-mojo.html)
- 正常情况下打包会生成`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`

关于Springboot源码怎么分析项目结构问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI