Maven中dependency和plugins的繼承與約束
Maven的父子項(xiàng)目
父子項(xiàng)目核心點(diǎn)是在于通過將一個(gè)大項(xiàng)目拆分為若干子模塊,每個(gè)模塊以子項(xiàng)目的形式存在,不同的子項(xiàng)目共享父項(xiàng)目的設(shè)置與約束。
所以,父項(xiàng)目承擔(dān)的角色是建立各個(gè)子項(xiàng)目的約束和一致的基礎(chǔ)。
父項(xiàng)目配置
<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.parent.project</groupId> <artifactId>project-artifiact</artifactId> <version>0.0.1.7-SNAPSHOT</version> <packaging>pom</packaging> <!-- dependency in inheritance --> <depdencies> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.8.1</version> </dependency> </dependencies> <!-- depdency constraints --> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>junit</groupId> <artifactid>junit</artifactId> <version>4.8.2</version> <scope>test</scope> </dependency> <dependency> <groupId>log4j</groupId> <artifactid>log4j</artifactId> <version>1.2.16</version> </dependency> </dependencies> </dependencyManagement> <!-- plugin constraints --> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>2.1</version> <configuration> <attach>true</attach> </configuration> <executions> <execution> <phase>compile</phase> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> </plugins> </pluginManagement> <modules> <module>A</module> <module>B</module> </modules>
在父項(xiàng)目中,其packaging值設(shè)置為pom,表示其定義為定義和描述項(xiàng)目的結(jié)構(gòu),而非真實(shí)的項(xiàng)目。
子項(xiàng)目定義
<?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> <parent> <artifactId>parent-project</artifactId> <groupId>com.parent.project</groupId> <version>0.0.1.7-SNAPSHOT</version> </parent> <artifactId>child-project</artifactId> <version>0.0.1.7-SNAPSHOT</version> <packaging>jar</packaging> ...........
dependencies
在上述示例中,定義了dependencies的節(jié)點(diǎn),這個(gè)節(jié)點(diǎn)中定義的dependency將被其子項(xiàng)目繼承,可以在子項(xiàng)目默認(rèn)加載進(jìn)來。
dependencyManagement
在此節(jié)點(diǎn)中定義的dependency對于子項(xiàng)目而言,有版本上的約束,在子項(xiàng)目中,如果有指定版本,則默認(rèn)使用父項(xiàng)目中約定的版本。
示例:
? <dependency> ? ? ?<groupId>junit</groupId> ? ? ?<artifactid>junit</artifactId> </dependency> <dependency> ? ? ?<groupId>log4j</groupId> ? ? ?<artifactid>log4j</artifactId> </dependency>
其版本號默認(rèn)使用父項(xiàng)目的版本號
pluginManagement
在此節(jié)點(diǎn)中定義的dependency對于子項(xiàng)目而言,有版本上的約束,在子項(xiàng)目中,如果有指定版本,則默認(rèn)使用父項(xiàng)目中約定的版本。
示例:
<plugins> ? ? <plugin> ? ? ? ? <groupId>org.apache.maven.plugins</groupId> ? ? ? ? <artifactId>maven-source-plugin</artifactId> ? ? </plugin> </plugins>
其默認(rèn)使用父項(xiàng)目中規(guī)定的版本號。當(dāng)然在子項(xiàng)目中也可以覆蓋父項(xiàng)目中的版本約定,自行指定所需要的版本號。
總結(jié)
在父子項(xiàng)目結(jié)構(gòu)中,子項(xiàng)目以modules的形式在父項(xiàng)目中注冊,子項(xiàng)目實(shí)施具體的實(shí)現(xiàn)功能。
對于不同的子項(xiàng)目共享父項(xiàng)目中的設(shè)置與約束,方便團(tuán)隊(duì)開發(fā)。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java設(shè)計(jì)模式之解釋器模式(Interpreter模式)介紹
這篇文章主要介紹了Java設(shè)計(jì)模式之解釋器模式(Interpreter模式)介紹,Interpreter定義:定義語言的文法,并且建立一個(gè)解釋器來解釋該語言中的句子,需要的朋友可以參考下2015-03-03基于Java代碼實(shí)現(xiàn)數(shù)字在數(shù)組中出現(xiàn)次數(shù)超過一半
這篇文章主要介紹了基于Java代碼實(shí)現(xiàn)數(shù)字在數(shù)組中出現(xiàn)次數(shù)超過一半的相關(guān)資料,需要的朋友可以參考下2016-02-02Java設(shè)計(jì)模式之簡單工廠 工廠方法 抽象工廠深度總結(jié)
設(shè)計(jì)模式(Design Pattern)是前輩們對代碼開發(fā)經(jīng)驗(yàn)的總結(jié),是解決特定問題的一系列套路。它不是語法規(guī)定,而是一套用來提高代碼可復(fù)用性、可維護(hù)性、可讀性、穩(wěn)健性以及安全性的解決方案2021-09-09Java 基于UDP協(xié)議實(shí)現(xiàn)消息發(fā)送
這篇文章主要介紹了Java 基于UDP協(xié)議實(shí)現(xiàn)消息發(fā)送,幫助大家更好的理解和學(xué)習(xí)Java網(wǎng)絡(luò)編程,感興趣的朋友可以了解下2020-11-11Java 調(diào)用Restful API接口的幾種方式(HTTPS)
這篇文章主要介紹了Java 調(diào)用Restful API接口的幾種方式(HTTPS),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-02-02