SpringBoot POM详细讲解
七步法则(帮助记忆):
1.引入boot或cloud两种方法,parent+dependencies
2.当前父POM文件GAV+打包类型(3+1)
3.配置文件变量定义放入properties中
4.repositories分两个,maven项目和maven插件
5.父POM只定义依赖,用dependencyManagement
6.外部依赖用dependencies
7.编译打包build中,基本元素、资源元素、和插件,插件也有pluginManagement
完整实例如下:
<?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">
<!--使用parent标签方式-->
<!--spring-boot-starter-parent是第一种方式,第二种是在dependencyManagement中使用spring-boot-dependencies-->
<!--进入spring-boot-starter-parent里,可以发现它其实依赖了我们第二种方式spring-boot-dependencies模块 。-->
<!-- <parent>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-parent</artifactId>-->
<!-- <version>2.2.1.RELEASE</version>-->
<!-- <relativePath/> <!– lookup parent from repository –>-->
<!-- </parent>-->
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>MyCloud</artifactId>
<version>1.0-SNAPSHOT</version>
<!--项目的打包类型:pom、jar、war-->
<!--所有的父级项目的packaging都为pom,packaging默认类型jar类型-->
<packaging>pom</packaging>
<!--properties:配置文件中的变量配置集合-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit.version>4.12</junit.version>
<spring-boot.version>2.1.6.RELEASE</spring-boot.version>
</properties>
<!--repositories:为项目配置【maven项目】的远程仓库,使用阿里云maven仓库-->
<repositories>
<repository>
<id>public</id>
<name>aliyun nexus</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
</repository>
</repositories>
<!--pluginRepositories:为项目配置【maven插件】的远程仓库,使用阿里云maven仓库-->
<pluginRepositories>
<pluginRepository>
<id>public</id>
<name>aliyun nexus</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
<!--dependencyManagement:-->
<!--Maven会沿着父子层次向上走,直到找到一个拥有dependencyManagement元素的项目,-->
<!--然后它就会使用在这个dependencyManagement元素中指定的版本号-->
<!--只是声明依赖,单不引用-->
<dependencyManagement>
<!--dependencies:所有生命在dependencies里的依赖都会自动引入,并默认被所有的子项目继承。-->
<dependencies>
<!--使用spring-cloud-dependencies方式-->
<!--引入cloud依赖,是第二种方式,第一种是使用parent-->
<!--大多数我们可能用到的包依赖和插件依赖都已经在spring-cloud-dependencies中定义好了-->
<!--注意:这种方式只能在dependencyManagement中使用-->
<!--好处:可以解决单继承的问题。没有使用parent标签,parent标签就可以用来继承其他父模块-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--引入boot依赖管理-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.9.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!--build标签描述了如何来编译及打包项目,而具体的编译和打包工作是通过build中配置的 plugin 来完成-->
<build>
<!--bigin 共用的基本build元素 -->
<!--defaultGoal: 执行build任务时,如果没有指定目标,将使用如下配置,如下例子:在命令中执行mvn时相当于执行mvn install-->
<defaultGoal>install</defaultGoal>
<!--directory:构建的结果所在目录,默认:${basedir}/target-->
<directory>${basedir}/target</directory>
<!--finalName:构建的最终结果名称,该名称可能在其他plugin中被修改-->
<finalName>${artifactId}-${version}</finalName>
<!-- 选择过滤配置文件的方法之一 也可以使用:在properties文件定义profile来实现-->
<!--在properties标签中自定义变量: <env>Dev</env>-->
<!-- <filters>-->
<!-- <filter>src/main/resources/filters/filter-${env}.properties</filter>-->
<!-- </filters>-->
<!--end 公共基本元素 -->
<resources>
<!--描述与项目关联的文件是什么在哪儿-->
<resource>
<!--指定编译后resource存放的文件夹路径,默认是basedir,通常resources目录存放在META-INF-->
<targetPath>META-INF</targetPath>
<!--配置当前的resources是否启用filter-->
<filtering>false</filtering>
<!--定义resources所在的文件夹,默认是${basedir}/src/main/resources-->
<directory>${basedir}/src/main/</directory>
<includes>
<include>configuration.xml</include>
</includes>
<!--指定那些文件被忽略,如果同一个文件符合includes和excludes,则excludesx生效-->
<excludes>
<exclude>*.properties</exclude>
</excludes>
</resource>
</resources>
<!--定义和resources一样,但是只在test的时候使用,默认路径是${basedir}/src/test/resources,-->
<!--testResource不被部署-->
<testResources>
</testResources>
<!--pluginManagement的元素的配置和plugins的配置是一样的,只是这里的配置只是用于集成-->
<!--使得可以在孩子pom中使用父pom:-->
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<!--除了groupId:artifactId:version标准坐标,plugin还需要如下属性:-->
<configuration>
<finalName>${project.build.finalName}</finalName>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<!--除了groupId:artifactId:version标准坐标,plugin还需要如下属性:-->
<!--extensions:是否加载plugin的extensions,默认是false-->
<extensions>false</extensions>
<!--inherited:是否应用到这个POM的所有子POM中,默认是true-->
<inherited>true</inherited>
<!--configuration:为这个plugin设置属性-->
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<compilerArguments>
<verbose/>
<bootclasspath>${java.home}\lib\rt.jar;${java.home}\lib\jce.jar</bootclasspath>
</compilerArguments>
</configuration>
</plugin>
</plugins>
</build>
</project>