[Resolve] the spring-boot source spring-boot dependency management

Key words: spring-boot dependency management, spring-boot-dependencies, spring-boot-parent

problem

maven project, dependency management is very basic and very important function now works increasingly large, rely more and more variety of two side bags, a tripartite pack too much, it is really dependent on conflict management headache, often we need to involve multiple places need to be adjusted.

Micro-channel public number: Yifei Come (focus on java source code analysis in the field of knowledge, understanding framework / tools from the principle source, verify CS expertise)

solution

Unified dependency management module to manage engineering all depend.

spring-boot works often use spring-boot-dependencies, spring-boot-starter-parent dependent on project management.

spring-boot the most superior engineering is spring-boot-build, step by step, in-depth understanding of the beginning of the following spring-boot-dependent solutions.

spring-boot in Scheme

spring-boot-build

spring-boot of the top engineering, specify maven profiles, maven repositories, maven pluginRepositories, maven build pluginManagement.

  • profiles: contains the style code check, the format code style; easier introduction eclipse; maven warehouse
  • repositories: Allows you to import snapshots and BOM milestone in the development process. This section was deleted flatten plug-in during the install / deploy. It contains maven central repository, spring snapshot warehouse, spring warehouse milestone
  • pluginRepositories: Plug warehouses, central warehouses contain maven, spring snapshot warehouse, spring warehouse milestone
  • pluginManagement: build plug-in management, the plug-in configuration only for storing Eclipse m2e set, it does not affect the Maven build itself.

spring-boot-dependencies

dependencies parent project is Spring-Build-Boot , does not contain the code, only to manage dependencies pom, the pom.xml as follows:

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-build</artifactId>
        <version>${revision}</version>
        <relativePath>../..</relativePath>
</parent>
<artifactId>spring-boot-dependencies</artifactId>
<packaging>pom</packaging>
<dependencyManagement>
  <!-- 省略具体依赖管理 -->
</dependencyManagement>
<build>
  <pluginManagement>
    <!-- 省略具体构建插件管理 -->
  </pluginManagement>
  <plugins>
    <!-- 省略具体构建插件 -->
  </plugins>
</build>

As can be seen from the pom, spring-boot-dependencies in addition to the introduction of (3) plug-in, more to do version management.

Among them, the introduction of plug-ins are:

  • flatten-maven-plugin: plugins to streamline pom
  • xml-maven-plugin:.. 1 according to validate an XML document schema; 2 using XSLT stylesheets for transforming XML documents
  • build-helper-maven-plugin: specify multiple source directory

dependencyManagement in almost a spring-boot project management all depend.

pluginManagement manage a variety of commonly used maven plugin, not detailed here.

Which contains Maven-Clean-plugin, Maven-Compiler-plugin, Maven-Assembly-plugin, Maven-WAR-plugin, Maven-JAR-plugin, spring-boot-maven-plugin , wherein the spring-boot-maven-plugin widget for spring-boot very important project, it will be packaged into a jar maven repackaged into an executable jar.

spring-boot-starter-parent(重要)

Since there is spring-boot-dependencies so rich in relies, plug-in version management, you also get a spring-boot-starter-parent do?

spring-boot-starter-parent the parent project is Spring-Boot-Dependencies , does not contain the code, only to manage dependencies with pom, pom.xml as follows:

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-dependencies</artifactId>
  <version>${revision}</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>
<properties>
  <main.basedir>${basedir}/../../..</main.basedir>
  <java.version>1.8</java.version>
  <!-- 资源分隔符 -->
  <resource.delimiter>@</resource.delimiter> <!-- delimiter that doesn't clash with Spring ${} placeholders -->
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  <maven.compiler.source>${java.version}</maven.compiler.source>
  <maven.compiler.target>${java.version}</maven.compiler.target>
</properties>
<build>
  <resources>
    <resource>
      <directory>${basedir}/src/main/resources</directory>
      <filtering>true</filtering>
      <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 -->
      <!-- spring-boot 提供的 maven 重打包插件,重要!!! -->
      <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>
  <plugins>
            <!-- 引入公共插件:flatten-maven-plugin、xml-maven-plugin -->
  </plugins>

characteristic

  • Default compiled version: Java 1.8
  • Source encoding: UTF-8
  • DependencyManagement inherited from spring-boot-dependencies of
  • spring-boot-maven-plugin goal is to repackage
  • maven resource filtering (application * .yml, application * .yaml, application * .properties, etc.), plug-in configuration
  • Resource separator: "@", used in the application * .yml @ referenced in maven property, common usage is as follows: spring.application.name = @ artifactId @

Note that, since the application.properties and application.yml files accept Spring style placeholders (${…}), the Maven filtering is changed to use @..@placeholders. (You can override that by setting a Maven property called resource.delimiter.)

Translation:

Note that since application.properties and application.yml acceptance spring style file placeholder ($ ...), so maven filter will be changed to use the @ ... @ placeholder. (This property can be covered by setting the maven property named resource.delimiter.)

spring-boot-parent

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>${revision}</version>
        <relativePath>../spring-boot-dependencies</relativePath>
</parent>
<artifactId>spring-boot-parent</artifactId>
<packaging>pom</packaging>
<dependencyManagement>
  <!-- 省略具体依赖管理 -->
</dependencyManagement>
<dependencies>
  <!-- 省略具体依赖 -->
</dependencies>
<build>
  <pluginManagement>
    <!-- 省略具体构建插件管理 -->
  </pluginManagement>
  <plugins>
    <!-- 省略具体构建插件 -->
  </plugins>
  <profiles>
    <!-- 省略具体 profile -->
  </profiles>
</build>

dependencyManagement

It consists of two parts:

  • Unpublished internal spring-boot dependence
  • Spring guide additional dependency (the user is invalid)

Thus, there is the added dependency management, users do not need to be concerned about, well, peace of mind.

dependencies

Public reliance, mainly because some tests rely on, such as: junit, hamcrest, mockito, spring -test, as well as assertions rely on: assertj.

plugins

Add some spring-boot utility plug-ins, such as: maven-compiler-plugin, maven-jar-plugin, maven-war-plugin, maven-source-plugin, etc.

profiles

Users basically do not care. Omission

select

spring-boot-dependencies and spring-boot-starter-parent, spring-boot-parent provides dependency management functions, then we in the development process, the use of which in the end it?

  • spring-boot-parent: the purpose is not available to users, the use of spring-boot open source project for spring-boot-project management of large projects in addition to the entire spring-boot-starters (provided to each of us to use out of the box ready to use tripartite package) of the other modules.
  • spring-boot-starter-parent: We construct a spring-boot program by Spring Initializr time, the official default is to allow us to use spring-boot-starter-parent, can generally be considered the official recommended to use this way of management depends, after all this way provided dependency, plug-in management more and more suitable to use.
  • spring-boot-dependencies: if at the time of use, the project did not want to specify the parent project, or you must use the parent company's engineering, may be introduced by this dependency management dependencyManagement.

Using spring-boot-dependencies, as compared to spring-boot-starter-parent when special attention to add Spring-Boot-Maven-plugin , as follows:

<build>
  <plugins>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
  </plugins>
  <pluginManagement>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <version>${springboot.version}</version>
      </plugin>
    </plugins>
  </pluginManagement>
</build>

As for the other additional spring-boot-starter-parent specified jar, added on demand.

actual use

When used in the project, all of the two parties, three parties jar should be unified management, in addition to rely on spring-boot provided, we have a lot jar need to manage, such as: mysql driver package, mybatis package, or various toolkits company like in the two side bag. Thus, it is preferable to use a separate module to build their own dependencies or parent.

to be continued

em ...... I write to you on the end? It seems to have been, but also a detailed analysis of how some specific dependence is chosen, such as: spring-boot choice is what logging framework, logback? log4j2? log4j? For the time code that is not spring-boot with the specified log realization, spring-boot is how to do it? We look forward to future updates? Or do not update more, who knows?

Focus on java source code analysis areas of knowledge, understanding framework / tools from the principles of the source code, to verify the application of CS expertise
No public

Guess you like

Origin www.cnblogs.com/lw5946/p/11444634.html