背景
我们在建立 SpringBoot 项目的时候,刚建好都会继承自一个父的 parent:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.17.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
我们要自定义自己的 parent,有以下几个理由:
- 定义我们自己的开发规范:在自定义的 parent 中可以规范引入的依赖。
- 提高开发效率:不用每个人都去关心共性的部分。
开始
我们建立一个 maven 项目,因为我们是父pom,因此只需要保留pom文件,其它都可以直接删除掉。pom 文件内容如下:
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>parent</artifactId>
<!-- 注意:packaging 必须为pom -->
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<name>Self Parent Project</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.17.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
<java.version>1.8</java.version>
<commons-lang.version>3.4</commons-lang.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<finalName>${project.artifactId}</finalName>
<pluginManagement>
<plugins>
<!-- 发布插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<configuration>
<!--<preparationGoals>clean package</preparationGoals>-->
<autoVersionSubmodules>true</autoVersionSubmodules>
<!-- 不使用默认的profile -->
<tagNameFormat>v@{project.version}</tagNameFormat>
<useReleaseProfile>false</useReleaseProfile>
<!-- 在发布时不检查是否提交 svn 的文件过滤配置 -->
<allowReleasePluginSnapshot>true</allowReleasePluginSnapshot>
<allowTimestampedSnapshots>true</allowTimestampedSnapshots>
<checkModificationExcludes>
<checkModificationExclude>.project</checkModificationExclude>
<checkModificationExclude>.settings</checkModificationExclude>
<checkModificationExclude>.classpath</checkModificationExclude>
<checkModificationExclude>**\.project</checkModificationExclude>
<checkModificationExclude>**\.settings</checkModificationExclude>
<checkModificationExclude>**\.classpath</checkModificationExclude>
</checkModificationExcludes>
<!--<commitByProject>true</commitByProject>-->
</configuration>
<dependencies>
<dependency>
<groupId>org.apache.maven.scm</groupId>
<artifactId>maven-scm-provider-gitexe</artifactId>
<version>1.9.4</version>
</dependency>
</dependencies>
</plugin>
<!-- 生成sources源码包的插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- 用来校验约定遵守情况插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>3.0.0-M2</version>
<executions>
<execution>
<id>enforce-banned-dependencies</id> <!-- 一个执行实例的ID -->
<goals>
<goal>enforce</goal> <!-- 执行的命令 -->
</goals>
<configuration>
<rules> <!-- 规则 -->
<bannedDependencies> <!-- 禁止依赖 -->
<excludes>
<exclude>
commons-lang:commons-lang <!-- 不允许出现这个依赖 -->
</exclude>
</excludes>
</bannedDependencies>
</rules>
<fail>true</fail> <!-- 不符合规则时就报错 -->
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<!-- maven-enforcer-plugin 这个插件会对项目环境进行检查 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
</plugin>
</plugins>
</build>
<distributionManagement>
<repository>
<id>Releases</id>
<name>Local Nexus Repository</name>
<url>http://192.168.1.177:8081/nexus/content/repositories/releases</url>
</repository>
<snapshotRepository>
<id>Snapshots</id>
<name>Local Nexus Repository</name>
<url>http://192.168.1.177:8081/nexus/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>
</project>
这里有一点需要注意:
- packaging 类型需要指定为pom。
扩展
上面的 POM 里用到了几个 Maven 插件:
- maven-enforcer-plugin:该插件的使用和介绍可以参考这篇文章:https://blog.csdn.net/wangjunjun2008/article/details/11599307