SpringBoot2.x的Maven依赖配置

1 Maven依赖的配置方式

使用Maven来配置SpringBoot2.x,有两种方式:

1.SpringBoot2.x的父依赖,在使用SpringBoot2.x生成器生成的项目中,会是这种方式

2.项目独立引入SpringBoot2.x的pom依赖

1.1 SpringBoot2.x父依赖

这种方式,项目完整的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>

	<groupId>cc.anxminise.spblearn</groupId>
	<artifactId>application</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>application</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.6.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</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>

以上关键的配置,如下:

xml节点名称 说明
parent 标明项目按SpringBoot2.x默认配置来处理
plugin.spring-boot-maven-plugin 对SpringBoot项目进行打包

如果项目中已经有自己的parent,此时,可以使用项目独立引入的方式。

1.2 项目独立引入

这种方式是通过pom文件中的dependencyManagement标签来实现SpringBoot的引入,完整的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>

	<groupId>cc.anxminise.spblearn</groupId>
	<artifactId>application</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>application</name>
	<description>Demo project for Spring Boot</description>

	<dependencyManagement>
             <dependencies>
                    <dependency>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-dependencies</artifactId>
                        <version>2.0.6.RELEASE</version>
                        <type>pom</type>
                        <scope>import</scope>
                    </dependency>
            </dependencies>
        </dependencyManagement>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</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>
                                <configuration>
                                   <!-- 程序的主启动类,即:用@SpringBootApplication注解,包含main方法的类 -->
                                  <mainClass>cc.anxminise.learnspboot.Application</mainClass>
                                 </configuration>
                                <executions>
                                    <execution>
                                        <goals>
                                            <goal>repackage</goal>
                                        </goals>
                                    </execution>
                                </executions>        
			</plugin>
		</plugins>
	</build>

</project>

以上关键的配置,如下:

xml节点名称 说明
dependencyManagement.dependencies.dependency.spring-boot-dependencies 标明项目按SpringBoot2.x默认配置来处理
plugin.spring-boot-maven-plugin 对SpringBoot项目进行打包,这里需要手动配置应用的启动类

这种方式,相对而言,更为的灵活。

posted @ 2018-11-05 06:50  忧臣解读  阅读(19179)  评论(0编辑  收藏  举报