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

