SpringBoot系列之二_pom文件
在SpringBoot项目的根目录下,有一个pom.xml文件。我们知道使用maven管理jar包时,可以通过pom.xml文件列出依赖jar包的清单。
我把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>com.hanhf</groupId> <!-- 项目名称 --> <artifactId>demo</artifactId> <!-- 软件版本 --> <version>0.0.1-SNAPSHOT</version> <!-- 打包方式 --> <packaging>jar</packaging> <!-- 名称与描述 --> <name>demo</name> <description>Demo project for Spring Boot</description> <!-- 建议继承spring-boot-starter-parent中的pom设定 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.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.7</java.version> </properties> <!-- 依赖项 --> <!-- 此项目只引入了spring-boot-starter-web和test两个依赖项 --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <!-- 构建设置 --> <!-- 引入spring-boot-maven-plugin插件 --> <!-- 引入maven-compiler-plugin插件 --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <!-- 以下设置可以指定JDK编译器版本 --> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> </plugins> </build> </project> 对于pom.xml文件,重点说明几个节点:1. parent节点。SpringBoot对所有项目有一个基础设定,放在spring-boot-parent中。我们的SpringBoot项目只需继承这个设定,再略加修改即可。SpringBoot官方建议用户的SpringBoot项目继承spring-boot-parent设定。如果想看一看spring-boot-parent的设定到底有哪些,可以看这个文件:
【maven仓库目录】/org/springframework/boot/spring-boot-parent/1.5.6.RELEASE/spring-boot-parent-1.5.6.RELEASE.pom
2. dependencies节点。以前用maven的时候,如果有几十个jar包,dependencies节点中就要写几十项,现在好了,这个项目的dependencies节点里只有spring-boot-starter-web和和spring-boot-starter-test两项。
3. build节点。build节点指定项目编译生成jar包时的选项。这里指定了两个插件(plugins),分别是spring-boot-maven-plugin和maven-compiler-plugin,其中前者是必要的,后者是我新加的,它可以指定编译时所使用的JDK版本,它将覆盖maven软件的默认配置文件的设定,比较重要。