maven父子工程中的依賴引用的實現(xiàn)
簡述
項目越來越趨向模塊化開發(fā),使用maven構(gòu)建工程,必然涉及到父子pom的關聯(lián),父pom文件的父級又會繼承springboot項目,就這樣在開發(fā)中踩坑不少,簡單記錄一下。
看問題之前先了解maven中的兩個標簽<dependencyManagement>
和<dependencies>
,明白的直接跳過。
maven標簽
1、<dependencyManagement>
這里其實是起到管理依賴jar版本號的作用,一般只會在項目的最頂層的pom.xml中使用到,所有子module如果想要使用到這里面聲明的jar,只需要在子module中添加相應的groupId和artifactId即可,并不需要聲明版本號,需要注意的是這里面只是聲明一個依賴,并不是真實的下載jar,只有在子module中使用到,才會去下載依賴。
2、<dependencies>
我們是這里引入了一個jar包之后,這里如果沒有加上version版本號的話,那么maven就會去<dependencyManagement>
里找對應groupId和artifactId的jar,如果有就繼承他,如果沒有就會報錯,這時候其實在我們配置的本地倉庫中會真實的下載對應的jar包,這時候所有的子module都會默認繼承這里面所有聲明的jar。
總的來說,就是在中聲明依賴和版本號,該標簽中的依賴不會被子模塊繼承,僅僅是聲明,子pom中直接引入依賴,具體的版本號會在父子中去找。
父pom的packaging都是pom,子項目pom的packaging都是jar。關于在父子配置pom的引用有兩種方案,這里以springboot
項目為例說明問題。
第一種pom配置
我們希望在父pom中引入相關依賴,都記錄在<dependencies>
下,子模塊直接繼承父pom的依賴,在子模塊中開發(fā)中就不必再去引入依賴,但在項目中有模塊可能就是單一的工具包,它并不需要springboot
的依賴,這時候啟動就會沖突。可以這樣解決,在父pom中定義springboot版本號,子模塊作為項目啟動的模塊配置springboot插件依賴,普通的dao,serivce,common不必引入。如下配置文件,文件中只列舉個別依賴包,重在說明問題:
父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> <!--這兩個依賴都將被子模塊繼承--> <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> <!-- 是否默認 true表示默認--> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> <profile> <!-- 測試環(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>
啟動類子模塊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> <!--引入子模塊相關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> <!--重要 如果不設置resource 會導致application.yaml中的@@找不到pom文件中的配置--> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> <plugins> <!--僅在啟動項目中引入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> <!-- 是否默認 true表示默認--> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> <profile> <!-- 測試環(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>
啟動類子模塊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服務 </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不會生效 --> </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òu)建項目中總結(jié)出來的,可供參考,重在理解。
到此這篇關于maven父子工程中的依賴引用的實現(xiàn)的文章就介紹到這了,更多相關maven父子工程依賴引用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Elasticsearch中FST與前綴搜索應用實戰(zhàn)解析
這篇文章主要為大家介紹了Elasticsearch中FST與前綴搜索應用實戰(zhàn)解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-08-08