javascript
Spring Boot 不使用默认的 parent,改用自己的项目的 parent
前些天發(fā)現(xiàn)了一個(gè)巨牛的人工智能學(xué)習(xí)網(wǎng)站,通俗易懂,風(fēng)趣幽默,忍不住分享一下給大家。點(diǎn)擊跳轉(zhuǎn)到教程。
在初學(xué)spring boot時(shí),官方示例中,都是讓我們繼承一個(gè)spring的?spring-boot-starter-parent?這個(gè)parent:
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.1.RELEASE</version> </parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency> </dependencies>但是,一般情況下,在我們自己的項(xiàng)目中,會(huì)定義一下自己的 parent 項(xiàng)目,這種情況下,上面的這種做法就行不通了。那么,該如何來做呢?其實(shí),在spring的官網(wǎng)也給出了變通的方法的:
在我們自己 parent 項(xiàng)目中,加下下面的聲明
?
<dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>1.5.1.RELEASE</version><type>pom</type><scope>import</scope></dependency></dependencies> </dependencyManagement>請注意,它的?type?是?pom,scope?是?import,這種類型的?dependency?只能在?dependencyManagement?標(biāo)簽中聲明。
?
?
?
然后,把我們項(xiàng)目中的 子項(xiàng)目 中,parent 的聲明,修改為我們自己項(xiàng)目的 parent 項(xiàng)目就可以了,比如,我的是:
<parent><groupId>org.test</groupId><artifactId>spring</artifactId><version>0.1-SNAPSHOT</version></parent>?
有一點(diǎn),需要注意一下。?
在 子項(xiàng)目 的?dependencies?中,不需要(也不能)再次添加對?spring-boot-dependencies?的聲明了,否則 子項(xiàng)目 將無法編譯通過。?
?
即,在 子項(xiàng)目 中,下面的配置是多余的:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId> </dependency>為什么會(huì)這個(gè)樣子呢??
因?yàn)?spring-boot-dependencies?根本就沒有對應(yīng)的jar包,它只是一個(gè)?pom?配置,可以去?maven倉庫?看一下。?
它里面定義了?非常多?的依賴聲明。
所以,有了它之后,我們在 子項(xiàng)目 中使用到的相關(guān)依賴,就不需要聲明version了,如:
?
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency> </dependencies>如,spring-boot-starter-web?和?spring-boot-starter-test?在?spring-boot-dependencies?中的聲明分別為:
?
?
?
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>1.5.1.RELEASE</version> </dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><version>1.5.1.RELEASE</version><exclusions><exclusion><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId></exclusion></exclusions> </dependency>?
參考文檔?
-?spring 官方文檔
?
?
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的Spring Boot 不使用默认的 parent,改用自己的项目的 parent的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于DOM元素
- 下一篇: JSch - Java Secure C