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的节点,这个节点中定义的dependency将被其子项目继承,可以在子项目默认加载进来。
dependencyManagement
在此节点中定义的dependency对于子项目而言,有版本上的约束,在子项目中,如果有指定版本,则默认使用父项目中约定的版本。
示例:
<dependency> <groupId>junit</groupId> <artifactid>junit</artifactId> </dependency> <dependency> <groupId>log4j</groupId> <artifactid>log4j</artifactId> </dependency>
其版本号默认使用父项目的版本号
pluginManagement
在此节点中定义的dependency对于子项目而言,有版本上的约束,在子项目中,如果有指定版本,则默认使用父项目中约定的版本。
示例:
<plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> </plugin> </plugins>
其默认使用父项目中规定的版本号。当然在子项目中也可以覆盖父项目中的版本约定,自行指定所需要的版本号。
总结
在父子项目结构中,子项目以modules的形式在父项目中注册,子项目实施具体的实现功能。
对于不同的子项目共享父项目中的设置与约束,方便团队开发。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
Java Mybatis框架Dao层的实现与映射文件以及核心配置文件详解分析
MyBatis 是一款优秀的持久层框架,它支持自定义 SQL、存储过程以及高级映射。MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作。MyBatis 可以通过简单的 XML 或注解来配置和映射原始类型、接口和 Java POJO为数据库中的记录2021-10-10
Spring Boot @Scheduled定时任务代码实例解析
这篇文章主要介绍了Spring Boot @Scheduled定时任务代码实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下2020-06-06


最新评论