SpringBoot2.x的Maven依賴配置
阿新 • • 發佈:2018-11-05
本篇主要說明以下內容:
1.SpringBoot2.x中Maven的配置內容,即:pom.xml
的內容說明
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專案進行打包,這裡需要手動配置應用的啟動類 |
這種方式,相對而言,更為的靈活。