SpringBoot系列:自定义Parent工程

环境

  • Spring Boot 2.0.4.RELEASE
  • JDK 1.8.0_181
  • Maven 3.5.4
  • IntelliJ IDEA 2018.1.6

需求

一般来说公司都会自己jar版本管理工程,那如何和spring-boot-starter-parent结合使用呢?

配置

  • 创建一个名为demo-spring-boot-parent的工程
  • 修改工程的pom.xml文件
 <dependencyManagement>
    <dependencies>
        <dependency>
            <!-- Import dependency management from Spring Boot -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.0.4.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

  • 添加jar版本管理
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.6</version>
</dependency>
  • 也可以覆盖spring-boot-dependencies中定义的版本,比如spring-aop
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>5.0.8.RELEASE</version>
</dependency>

  • 完整配置如下

<?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.itxiaoer</groupId>
    <artifactId>demo-spring-boot-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <name>demo-spring-boot-parent</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <!-- Import dependency management from Spring Boot -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.0.4.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>commons-io</groupId>
                <artifactId>commons-io</artifactId>
                <version>2.6</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aop</artifactId>
                <version>5.0.8.RELEASE</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

  • 打包发布
# 本地发布使用
$ mvn install 

#或者发布到maven仓库 
$ mvn deploy 

[INFO] Scanning for projects...
[INFO] 
[INFO] ----------------< com.itxiaoer:demo-spring-boot-parent >----------------
[INFO] Building demo-spring-boot-parent 0.0.1-SNAPSHOT
[INFO] --------------------------------[ pom ]---------------------------------
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ demo-spring-boot-parent ---
[INFO] Installing /Users/liuyukuai/Documents/develop/demo-spring-cloud/demo-spring-boot-parent/pom.xml to /Users/liuyukuai/Documents/maven/com/itxiaoer/demo-spring-boot-parent/0.0.1-SNAPSHOT/demo-spring-boot-parent-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.424 s
[INFO] Finished at: 2018-08-31T13:23:16+08:00
[INFO] ------------------------------------------------------------------------

使用

其他项目pom.xml文件添加parent就可以了


<parent>
    <groupId>com.itxiaoer</groupId>
    <artifactId>demo-spring-boot-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <relativePath/>
</parent>

完整代码:https://github.com/liuyukuai/demo-spring-cloud/tree/master/demo-spring-boot-parent