Maven中dependency和plugins的繼承與約束
Maven的父子項目
父子項目核心點是在于通過將一個大項目拆分為若干子模塊,每個模塊以子項目的形式存在,不同的子項目共享父項目的設置與約束。
所以,父項目承擔的角色是建立各個子項目的約束和一致的基礎。
父項目配置
<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值設置為pom,表示其定義為定義和描述項目的結構,而非真實的項目。
子項目定義
<?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ī)定的版本號。當然在子項目中也可以覆蓋父項目中的版本約定,自行指定所需要的版本號。
總結
在父子項目結構中,子項目以modules的形式在父項目中注冊,子項目實施具體的實現功能。
對于不同的子項目共享父項目中的設置與約束,方便團隊開發(fā)。
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Java Mybatis框架Dao層的實現與映射文件以及核心配置文件詳解分析
MyBatis 是一款優(yōu)秀的持久層框架,它支持自定義 SQL、存儲過程以及高級映射。MyBatis 免除了幾乎所有的 JDBC 代碼以及設置參數和獲取結果集的工作。MyBatis 可以通過簡單的 XML 或注解來配置和映射原始類型、接口和 Java POJO為數據庫中的記錄2021-10-10
Spring Boot @Scheduled定時任務代碼實例解析
這篇文章主要介紹了Spring Boot @Scheduled定時任務代碼實例解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-06-06

