maven父子工程中的依賴引用的實(shí)現(xiàn)
簡述
項(xiàng)目越來越趨向模塊化開發(fā),使用maven構(gòu)建工程,必然涉及到父子pom的關(guān)聯(lián),父pom文件的父級(jí)又會(huì)繼承springboot項(xiàng)目,就這樣在開發(fā)中踩坑不少,簡單記錄一下。
看問題之前先了解maven中的兩個(gè)標(biāo)簽<dependencyManagement>
和<dependencies>
,明白的直接跳過。
maven標(biāo)簽
1、<dependencyManagement>
這里其實(shí)是起到管理依賴jar版本號(hào)的作用,一般只會(huì)在項(xiàng)目的最頂層的pom.xml中使用到,所有子module如果想要使用到這里面聲明的jar,只需要在子module中添加相應(yīng)的groupId和artifactId即可,并不需要聲明版本號(hào),需要注意的是這里面只是聲明一個(gè)依賴,并不是真實(shí)的下載jar,只有在子module中使用到,才會(huì)去下載依賴。
2、<dependencies>
我們是這里引入了一個(gè)jar包之后,這里如果沒有加上version版本號(hào)的話,那么maven就會(huì)去<dependencyManagement>
里找對(duì)應(yīng)groupId和artifactId的jar,如果有就繼承他,如果沒有就會(huì)報(bào)錯(cuò),這時(shí)候其實(shí)在我們配置的本地倉庫中會(huì)真實(shí)的下載對(duì)應(yīng)的jar包,這時(shí)候所有的子module都會(huì)默認(rèn)繼承這里面所有聲明的jar。
總的來說,就是在中聲明依賴和版本號(hào),該標(biāo)簽中的依賴不會(huì)被子模塊繼承,僅僅是聲明,子pom中直接引入依賴,具體的版本號(hào)會(huì)在父子中去找。
父pom的packaging都是pom,子項(xiàng)目pom的packaging都是jar。關(guān)于在父子配置pom的引用有兩種方案,這里以springboot
項(xiàng)目為例說明問題。
第一種pom配置
我們希望在父pom中引入相關(guān)依賴,都記錄在<dependencies>
下,子模塊直接繼承父pom的依賴,在子模塊中開發(fā)中就不必再去引入依賴,但在項(xiàng)目中有模塊可能就是單一的工具包,它并不需要springboot
的依賴,這時(shí)候啟動(dòng)就會(huì)沖突??梢赃@樣解決,在父pom中定義springboot版本號(hào),子模塊作為項(xiàng)目啟動(dòng)的模塊配置springboot插件依賴,普通的dao,serivce,common不必引入。如下配置文件,文件中只列舉個(gè)別依賴包,重在說明問題:
父pom.xml配置:
<?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.demo</groupId> <artifactId>demo-api</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging> <modules> <module>demo-web</module> <module>demo-common</module> <module>demo-service</module> </modules> <properties> <spring-boot.version>2.1.8.RELEASE</spring-boot.version> <java.version>1.8</java.version> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <fastjson.version>1.2.47</fastjson.version> <pagehelper.version>5.1.6</pagehelper.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> </dependencies> </dependencyManagement> <dependencies> <!--這兩個(gè)依賴都將被子模塊繼承--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>${fastjson.version}</version> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>${pagehelper.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>8</source> <target>8</target> </configuration> </plugin> </plugins> </build> <profiles> <profile> <id>dev</id> <properties> <package.environment>dev</package.environment> </properties> <!-- 是否默認(rèn) true表示默認(rèn)--> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> <profile> <!-- 測(cè)試環(huán)境 --> <id>test</id> <properties> <package.environment>test</package.environment> </properties> </profile> <profile> <!-- 生產(chǎn)環(huán)境 --> <id>prod</id> <properties> <package.environment>prod</package.environment> </properties> </profile> </profiles> </project>
普通子模塊pom.xml配置:
<?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"> <parent> <artifactId>demo-api</artifactId> <groupId>com.demo</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <groupId>com.demo.common</groupId> <artifactId>demo-common</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo-common</name> <description>demo-common project</description> <packaging>jar</packaging> </project>
啟動(dòng)類子模塊pom.xml配置:
<?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> <parent> <groupId>com.demo</groupId> <artifactId>demo-api</artifactId> <version>1.0-SNAPSHOT</version> </parent> <groupId>com.demo.web</groupId> <artifactId>demo-web</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo-web</name> <description>demo-web project</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <!--引入子模塊相關(guān)jar --> <dependency> <groupId>com.demo.common</groupId> <artifactId>demo-common</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>com.myway.service</groupId> <artifactId>share-read-service</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <!--重要 如果不設(shè)置resource 會(huì)導(dǎo)致application.yaml中的@@找不到pom文件中的配置--> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> <plugins> <!--僅在啟動(dòng)項(xiàng)目中引入springboot插件 --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
第二種pom配置
將所有的依賴在父pom的中聲明,子模塊把需要的都引入一遍:
父pom.xml配置:
<?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.demo</groupId> <artifactId>demo</artifactId> <version>3.0.0</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <druid.version>1.1.14</druid.version> <pagehelper.boot.version>1.2.5</pagehelper.boot.version> <fastjson.version>1.2.70</fastjson.version> </properties> <!-- 依賴聲明 --> <dependencyManagement> <dependencies> <!-- SpringBoot的依賴配置--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.1.8.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <!--阿里數(shù)據(jù)庫連接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>${druid.version}</version> </dependency> <!-- pagehelper 分頁插件 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>${pagehelper.boot.version}</version> </dependency> </dependencies> </dependencyManagement> <modules> <module>demo-web</module> <module>demo-common</module> </modules> <packaging>pom</packaging> <dependencies> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>${java.version}</source> <target>${java.version}</target> <encoding>${project.build.sourceEncoding}</encoding> </configuration> </plugin> </plugins> </build> <repositories> <repository> <id>public</id> <name>aliyun nexus</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>public</id> <name>aliyun nexus</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories> <profiles> <profile> <id>dev</id> <properties> <package.environment>dev</package.environment> </properties> <!-- 是否默認(rèn) true表示默認(rèn)--> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> <profile> <!-- 測(cè)試環(huán)境 --> <id>test</id> <properties> <package.environment>test</package.environment> </properties> </profile> <profile> <!-- 生產(chǎn)環(huán)境 --> <id>prod</id> <properties> <package.environment>prod</package.environment> </properties> </profile> </profiles> </project>
普通子模塊pom.xml配置:
<?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"> <parent> <artifactId>demo</artifactId> <groupId>com.demo</groupId> <version>1.0.0</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>demo-system</artifactId> <dependencies> <dependency> <groupId>com.demo</groupId> <artifactId>demo-common</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> </dependency> </dependencies> </project>
啟動(dòng)類子模塊pom.xml配置:
<?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"> <parent> <artifactId>demo</artifactId> <groupId>com.demo</groupId> <version>3.0.0</version> </parent> <modelVersion>4.0.0</modelVersion> <packaging>jar</packaging> <artifactId>demo-admin</artifactId> <description> web服務(wù) </description> <dependencies> <dependency> <artifactId>demo</artifactId> <groupId>com.demo</groupId> <version>1.0.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.1.8.RELEASE</version> <configuration> <fork>true</fork> <!-- 如果沒有該配置,devtools不會(huì)生效 --> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>3.0.0</version> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> <warName>${project.artifactId}</warName> </configuration> </plugin> </plugins> </build> </project>
以上是個(gè)人在構(gòu)建項(xiàng)目中總結(jié)出來的,可供參考,重在理解。
到此這篇關(guān)于maven父子工程中的依賴引用的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)maven父子工程依賴引用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Netty學(xué)習(xí)之理解selector原理示例
這篇文章主要為大家介紹了Netty學(xué)習(xí)之理解selector原理示例使用分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪<BR>2023-07-07java設(shè)計(jì)模式之建造者模式學(xué)習(xí)
建造者模式(Builder Pattern)主要用于“分步驟構(gòu)建一個(gè)復(fù)雜的對(duì)象”,在這其中“分步驟”是一個(gè)穩(wěn)定的算法,下面給出了詳細(xì)的示例2014-01-01如何解決Nacos服務(wù)下線報(bào)錯(cuò)問題
這篇文章主要介紹了如何解決Nacos服務(wù)下線報(bào)錯(cuò)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07Elasticsearch中FST與前綴搜索應(yīng)用實(shí)戰(zhàn)解析
這篇文章主要為大家介紹了Elasticsearch中FST與前綴搜索應(yīng)用實(shí)戰(zhàn)解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08