preface
In many springboot projects, we can see a piece of code similar to this in pom:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.5.RELEASE</version> </parent>
Because the version I use here is 2.3.5.RELEASE, there may be some differences.
So what does this paragraph do? Let's analyze the pom file content of the corresponding version.
function
Here, for space reasons, I only intercept the key code.
<!-- spring-boot-starter-parent-2.3.5.RELEASE.pom --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.3.5.RELEASE</version> </parent> <properties> <java.version>1.8</java.version> <resource.delimiter>@</resource.delimiter> <maven.compiler.source>${java.version}</maven.compiler.source> <maven.compiler.target>${java.version}</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> </properties> <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> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <parameters>true</parameters> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <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> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <mainClass>${start-class}</mainClass> <addDefaultImplementationEntries>true</addDefaultImplementationEntries> </manifest> </archive> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <configuration> <archive> <manifest> <mainClass>${start-class}</mainClass> <addDefaultImplementationEntries>true</addDefaultImplementationEntries> </manifest> </archive> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <configuration> <delimiters> <delimiter>${resource.delimiter}</delimiter> </delimiters> <useDefaultDelimiters>false</useDefaultDelimiters> </configuration> </plugin>
- Specify that the jdk version is 1.8
- The specified character set is UTF-8
- Inherits spring-boot-dependencies.2.3.5.RELEASE.
- Filter file resources ending in yml, yaml and properties, including files in different environments defined in profile.
- Automated plug-in configuration,
- Resource plug-in: Maven resources plugin (process resource files to the output directory)
- Test plug-in: Maven failsafe plugin
- Package plug-ins:
- Maven jar plugin (package jar)
- Maven war plugin (package war)
- Version dependent management. It is a more compatible version after the official test of spring, which can be viewed in spring-boot-dependencies.2.3.5.RELEASE.
Spring boot dependencies manages versions through dependency management and plugin management.
Take a look at the differences between dependencies and dependency management This article
Do not inherit spring boot parent
So what are the consequences of not using parent? Obviously, the functions provided by the above parent should be implemented by themselves
Basic configuration
<properties> <java.version>1.8</java.version> <resource.delimiter>@</resource.delimiter> <maven.compiler.source>${java.version}</maven.compiler.source> <maven.compiler.target>${java.version}</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> </properties>
You need to manually specify the character set and compiled version.
Version dependency
Because there is no place to control the dependent version, we need to fill in the version of all the imported dependencies.
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.3.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> <version>2.3.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> <version>2.3.5.RELEASE</version> </dependency> </dependencies>
At this time, the project can run normally locally, because the dependent configuration can be obtained, but it cannot be packaged into jar or war package.
Plug in configuration
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <mainClass>${start-class}</mainClass> <addDefaultImplementationEntries>true</addDefaultImplementationEntries> </manifest> </archive> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <configuration> <archive> <manifest> <mainClass>${start-class}</mainClass> <addDefaultImplementationEntries>true</addDefaultImplementationEntries> </manifest> </archive> </configuration> </plugin>
We need to configure the corresponding plug-in and version ourselves, such as the web.xml file required to package the war package, specifying the main class, etc., which need to be configured manually. Of course, we can also use other tools to package.
Directly inherit spring boot dependencies
In fact, the main function of spring boot dependencies is to provide a management of dependent versions. Let's simplify the version control, because it is a stable and compatible version officially tested by spring. Then we still need to make a pom file similar to spring boot parent, because this is the project plug-in and configuration we actually rely on.
To put it simply, it is actually the relationship between dependency management and dependencies.
summary
This is mainly the main idea of springboot: start dependence and simplify development. By simply inheriting spring boot parent, we can quickly solve the management of compilation, resource file output, configuration file, packaging and version dependency without complicated configuration.