Spring Boot 自定义starter-parent

背景

我们在建立 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。



作者:FlySheep_ly
链接:https://www.jianshu.com/p/105ec388963d
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值