自定義基于SpringBoot的parent pom

maven parent pom概述

maven parent pom 能夠定義項(xiàng)目模型,包括構(gòu)建方式春贸、項(xiàng)目環(huán)境混萝、項(xiàng)目依賴、輸出JavaDoc萍恕、發(fā)布source源碼等等逸嘀。parent pom能使項(xiàng)目規(guī)范化,可以限定各種框架和類庫(kù)的版本允粤,便于后續(xù)的維護(hù)和升級(jí)崭倘。

SpringBoot parent pom

目前SpringBoot已成為Java項(xiàng)目基礎(chǔ)框架,我們常用IDEA內(nèi)的Spring Assistant插件創(chuàng)建SpringBoot項(xiàng)目类垫。項(xiàng)目創(chuàng)建并加載完成后我們可以在項(xiàng)目的pom.xml中看到以下配置:

 <parent>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-parent</artifactId>
   <version>2.2.6.RELEASE</version>
 </parent>

這就是使用了SpringBoot parent pom司光,這個(gè)parent配置了spring-boot的編譯和打包插件。我們能夠看到spring-boot-starter-parent也有parent配置悉患,為spring-boot-dependencies残家。

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.2.6.RELEASE</version>
    <relativePath>../../spring-boot-dependencies</relativePath>
  </parent>

spring-boot-dependenciespom有各種依賴以及依賴的版本信息的管理,這些版本都是經(jīng)過(guò)SpringBoot官方測(cè)試過(guò)的购撼,在自己項(xiàng)目中直接添加依賴即可跪削,不需要設(shè)置版本號(hào),如果項(xiàng)目中想要使用其他版本的依賴迂求,配置版本號(hào)即可碾盐。

  <properties>
    <activemq.version>5.15.12</activemq.version>
    <antlr2.version>2.7.7</antlr2.version>
    <appengine-sdk.version>1.9.79</appengine-sdk.version>
    <artemis.version>2.10.1</artemis.version>
    <aspectj.version>1.9.5</aspectj.version>
    <assertj.version>3.13.2</assertj.version>
    <atomikos.version>4.0.6</atomikos.version>
    <awaitility.version>4.0.2</awaitility.version>
    <bitronix.version>2.1.4</bitronix.version>
    <build-helper-maven-plugin.version>3.0.0</build-helper-maven-plugin.version>
    <byte-buddy.version>1.10.8</byte-buddy.version>
    <caffeine.version>2.8.1</caffeine.version>
    <cassandra-driver.version>3.7.2</cassandra-driver.version>
    <classmate.version>1.5.1</classmate.version>
    <commons-codec.version>1.13</commons-codec.version>
    <commons-dbcp2.version>2.7.0</commons-dbcp2.version>
    <commons-lang3.version>3.9</commons-lang3.version>
    <commons-pool.version>1.6</commons-pool.version>
    <commons-pool2.version>2.7.0</commons-pool2.version>
    ...
  </properties>
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot</artifactId>
        <version>2.2.6.RELEASE</version>
      </dependency>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-test</artifactId>
        <version>2.2.6.RELEASE</version>
      </dependency>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-test-autoconfigure</artifactId>
        <version>2.2.6.RELEASE</version>
      </dependency>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-actuator</artifactId>
        <version>2.2.6.RELEASE</version>
      </dependency>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-actuator-autoconfigure</artifactId>
        <version>2.2.6.RELEASE</version>
      </dependency>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-autoconfigure</artifactId>
        <version>2.2.6.RELEASE</version>
      </dependency>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-autoconfigure-processor</artifactId>
        <version>2.2.6.RELEASE</version>
      </dependency>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-metadata</artifactId>
        <version>2.2.6.RELEASE</version>
      </dependency>
      ...
    </dependencies>
  <dependencyManagement>

自定義parent pom

在公司項(xiàng)目中往往會(huì)使用相同的框架和工具類庫(kù)包,如果每個(gè)項(xiàng)目使用的類庫(kù)版本各不相同揩局,后期協(xié)同速度將下降毫玖,因?yàn)槊總€(gè)人了解到的類庫(kù)接口可能不一致。使用相同的類庫(kù)版本能一定程度解決這個(gè)問(wèn)題,而且團(tuán)隊(duì)之間能形成規(guī)范付枫。如果類庫(kù)版本需要集中更新烹玉,只需要更新parent pom中配置的版本即可,使用此parent pom的項(xiàng)目拉取新包后進(jìn)行測(cè)試即可阐滩。

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <name>xx-springboot-v2-parent</name>
    <description>customized parent pom</description>
    <groupId>com.example</groupId>
    <artifactId>xx-springboot-v2-parent</artifactId>
    <version>1.0</version>
    <packaging>pom</packaging>

    <developers>
        <developer>
            <name>Moosion</name>
            <email>moosion@163.com</email>
        </developer>
    </developers>

    <properties>
        <java.version>1.8</java.version>
        <resource.delimiter>@</resource.delimiter>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

        <swagger.version>2.8.0</swagger.version>
        <gson.version>2.7</gson.version>
        <json-smart.version>2.2.1</json-smart.version>
        <spring-boot.version>2.2.6.RELEASE</spring-boot.version>
        <mysql-connector-java.version>5.1.44</mysql-connector-java.version>
        <mybatis-spring-boot-starter.version>2.0.0</mybatis-spring-boot-starter.version>

        <!-- 公司內(nèi)部私有類庫(kù) -->
        <xx-bigtable.version>1.0.3</xx-bigtable.version>
        <xx-bigcache.version>2.0.5</xx-bigcache.version>
        <xx-bigstore.version>1.5.6</xx-bigstore.version>
        <xx-msgq.version>1.5.2</xx-msgq.version>
    </properties>

    <!-- 依賴管理 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>com.example</groupId>
                <artifactId>xx-bigtable</artifactId>
                <version>${xx-bigtable.version}</version>
            </dependency>
            <dependency>
                <groupId>com.example</groupId>
                <artifactId>xx-bigstore</artifactId>
                <version>${xx-bigstore.version}</version>
            </dependency>
            <dependency>
                <groupId>com.example</groupId>
                <artifactId>xx-bigcache</artifactId>
                <version>${xx-bigcache.version}</version>
            </dependency>
            <dependency>
                <groupId>com.example</groupId>
                <artifactId>xx-msgq</artifactId>
                <version>${xx-msgq.version}</version>
            </dependency>
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger2</artifactId>
                <version>${swagger.version}</version>
            </dependency>
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger-ui</artifactId>
                <version>${swagger.version}</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>${mybatis-spring-boot-starter.version}</version>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql-connector-java.version}</version>
            </dependency>
            <dependency>
                <groupId>net.minidev</groupId>
                <artifactId>json-smart</artifactId>
                <version>${json-smart.version}</version>
            </dependency>
            <dependency>
                <groupId>com.google.code.gson</groupId>
                <artifactId>gson</artifactId>
                <version>${gson.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <!--  springboot所需編譯和打包插件 -->
    <build>
        <resources>
            <resource>
                <filtering>true</filtering>
                <directory>${basedir}/src/main/resources</directory>
                <includes>
                    <include>**/application*.yml</include>
                    <include>**/application*.yaml</include>
                    <include>**/application*.properties</include>
                </includes>
            </resource>
            <resource>
                <directory>${basedir}/src/main/resources</directory>
                <excludes>
                    <exclude>**/application*.yml</exclude>
                    <exclude>**/application*.yaml</exclude>
                    <exclude>**/application*.properties</exclude>
                </excludes>
            </resource>
        </resources>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.jetbrains.kotlin</groupId>
                    <artifactId>kotlin-maven-plugin</artifactId>
                    <version>${kotlin.version}</version>
                    <executions>
                        <execution>
                            <id>compile</id>
                            <phase>compile</phase>
                            <goals>
                                <goal>compile</goal>
                            </goals>
                        </execution>
                        <execution>
                            <id>test-compile</id>
                            <phase>test-compile</phase>
                            <goals>
                                <goal>test-compile</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <jvmTarget>${java.version}</jvmTarget>
                        <javaParameters>true</javaParameters>
                    </configuration>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <parameters>true</parameters>
                    </configuration>
                </plugin>
                <plugin>
                    <artifactId>maven-failsafe-plugin</artifactId>
                    <executions>
                        <execution>
                            <goals>
                                <goal>integration-test</goal>
                                <goal>verify</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <classesDirectory>${project.build.outputDirectory}</classesDirectory>
                    </configuration>
                </plugin>
                <plugin>
                    <artifactId>maven-jar-plugin</artifactId>
                    <configuration>
                        <archive>
                            <manifest>
                                <mainClass>${start-class}</mainClass>
                                <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                            </manifest>
                        </archive>
                    </configuration>
                </plugin>
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <configuration>
                        <archive>
                            <manifest>
                                <mainClass>${start-class}</mainClass>
                                <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                            </manifest>
                        </archive>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <configuration>
                        <mainClass>${start-class}</mainClass>
                    </configuration>
                </plugin>
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <configuration>
                        <delimiters>
                            <delimiter>${resource.delimiter}</delimiter>
                        </delimiters>
                        <useDefaultDelimiters>false</useDefaultDelimiters>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>pl.project13.maven</groupId>
                    <artifactId>git-commit-id-plugin</artifactId>
                    <executions>
                        <execution>
                            <goals>
                                <goal>revision</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <verbose>true</verbose>
                        <dateFormat>yyyy-MM-dd'T'HH:mm:ssZ</dateFormat>
                        <generateGitPropertiesFile>true</generateGitPropertiesFile>
                        <generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>repackage</id>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <mainClass>${start-class}</mainClass>
                    </configuration>
                </plugin>
                <plugin>
                    <artifactId>maven-shade-plugin</artifactId>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>shade</goal>
                            </goals>
                            <configuration>
                                <transformers>
                                    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                        <resource>META-INF/spring.handlers</resource>
                                    </transformer>
                                    <transformer implementation="org.springframework.boot.maven.PropertiesMergingResourceTransformer">
                                        <resource>META-INF/spring.factories</resource>
                                    </transformer>
                                    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                        <resource>META-INF/spring.schemas</resource>
                                    </transformer>
                                    <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                        <mainClass>${start-class}</mainClass>
                                    </transformer>
                                </transformers>
                            </configuration>
                        </execution>
                    </executions>
                    <dependencies>
                        <dependency>
                            <groupId>org.springframework.boot</groupId>
                            <artifactId>spring-boot-maven-plugin</artifactId>
                            <version>2.2.6.RELEASE</version>
                        </dependency>
                    </dependencies>
                    <configuration>
                        <keepDependenciesWithProvidedScope>true</keepDependenciesWithProvidedScope>
                        <createDependencyReducedPom>true</createDependencyReducedPom>
                        <filters>
                            <filter>
                                <artifact>*:*</artifact>
                                <excludes>
                                    <exclude>META-INF/*.SF</exclude>
                                    <exclude>META-INF/*.DSA</exclude>
                                    <exclude>META-INF/*.RSA</exclude>
                                </excludes>
                            </filter>
                        </filters>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

propertiesdependencyManagement根據(jù)自己公司項(xiàng)目需求配置二打,build可完全照抄spring-boot-starter-parentpom內(nèi)定義的build,也可根據(jù)自己公司項(xiàng)目需求配置掂榔。

測(cè)試無(wú)誤后發(fā)布到私有maven倉(cāng)庫(kù)继效,便于其他項(xiàng)目使用。

自定義parent pom的使用

創(chuàng)建新項(xiàng)目時(shí)装获,創(chuàng)建無(wú)模板maven項(xiàng)目瑞信,創(chuàng)建完成后在pom.xml中引入。同時(shí)需要添加springboot的maven插件spring-boot-maven-plugin穴豫,這樣才能構(gòu)建為和springboot相同的jar包凡简。propertiesdependencies根據(jù)項(xiàng)目需求配置。

<parent>
    <groupId>com.example</groupId>
    <artifactId>xx-springboot-v2-parent</artifactId>
    <version>1.0</version>
    <relativePath/>
</parent>

<properties>
    <java.version>1.8</java.version>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <resource.delimiter>@</resource.delimiter>
    <maven.compiler.source>${java.version}</maven.compiler.source>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.target>${java.version}</maven.compiler.target>
    ...
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    ...
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>${spring-boot.version}</version>
        </plugin>
    </plugins>
</build>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末精肃,一起剝皮案震驚了整個(gè)濱河市秤涩,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌司抱,老刑警劉巖溉仑,帶你破解...
    沈念sama閱讀 207,113評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異状植,居然都是意外死亡浊竟,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,644評(píng)論 2 381
  • 文/潘曉璐 我一進(jìn)店門津畸,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)振定,“玉大人,你說(shuō)我怎么就攤上這事肉拓『笃担” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 153,340評(píng)論 0 344
  • 文/不壞的土叔 我叫張陵暖途,是天一觀的道長(zhǎng)卑惜。 經(jīng)常有香客問(wèn)我,道長(zhǎng)驻售,這世上最難降的妖魔是什么露久? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 55,449評(píng)論 1 279
  • 正文 為了忘掉前任,我火速辦了婚禮欺栗,結(jié)果婚禮上毫痕,老公的妹妹穿的比我還像新娘征峦。我一直安慰自己,他們只是感情好消请,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,445評(píng)論 5 374
  • 文/花漫 我一把揭開(kāi)白布栏笆。 她就那樣靜靜地躺著,像睡著了一般臊泰。 火紅的嫁衣襯著肌膚如雪蛉加。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 49,166評(píng)論 1 284
  • 那天缸逃,我揣著相機(jī)與錄音七婴,去河邊找鬼。 笑死察滑,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的修肠。 我是一名探鬼主播贺辰,決...
    沈念sama閱讀 38,442評(píng)論 3 401
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼嵌施!你這毒婦竟也來(lái)了饲化?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 37,105評(píng)論 0 261
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤吗伤,失蹤者是張志新(化名)和其女友劉穎吃靠,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體足淆,經(jīng)...
    沈念sama閱讀 43,601評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡巢块,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,066評(píng)論 2 325
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了巧号。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片族奢。...
    茶點(diǎn)故事閱讀 38,161評(píng)論 1 334
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖丹鸿,靈堂內(nèi)的尸體忽然破棺而出越走,到底是詐尸還是另有隱情,我是刑警寧澤靠欢,帶...
    沈念sama閱讀 33,792評(píng)論 4 323
  • 正文 年R本政府宣布廊敌,位于F島的核電站,受9級(jí)特大地震影響门怪,放射性物質(zhì)發(fā)生泄漏骡澈。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,351評(píng)論 3 307
  • 文/蒙蒙 一掷空、第九天 我趴在偏房一處隱蔽的房頂上張望秧廉。 院中可真熱鬧伞广,春花似錦、人聲如沸疼电。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,352評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)蔽豺。三九已至区丑,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間修陡,已是汗流浹背沧侥。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 31,584評(píng)論 1 261
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留魄鸦,地道東北人宴杀。 一個(gè)月前我還...
    沈念sama閱讀 45,618評(píng)論 2 355
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像拾因,于是被迫代替她去往敵國(guó)和親旺罢。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,916評(píng)論 2 344