mvn 依赖仓库管理脚本. 其中有一些简化的方式.
什么是父子项目
-
在目录结构上一般体现为parent为外层,child为内层.用idea创建一个mvn项目,New->Module… 创建一个mvn子项目(groupId必须一致).
-
pom.xml文件的特征为:
//parent 添加 module <modules> <module>son-one</module> </modules> //child 添加 parent <parent> <artifactId>abc</artifactId> <groupId>com.example</groupId> <version>1.0-SNAPSHOT</version> <!-- <relativePath>../pom.xml</relativePath> --> </parent>
子项目自动获取parent 项目依赖
parent项目添加依赖
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis-version}</version>
</dependency>
</dependencies>
child项目编写测试代码
package com.example.son1;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
}
}
编译通过.
如果将 parent mybatis dependency注释掉,SqlSessionFactoryBuilder就找不到了.
父子项目 通过 dependencyManagement 进行版本的统一管理.
dependencyManagement简化child(包括同级)项目版本配置
- parent project 设置 dependencyManagement
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis-version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector-version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit-version}</version>
</dependency>
</dependencies>
</dependencyManagement>
- child project 省略版本设置
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</dependency>
</dependencies>
如果将dependencyManagement注释掉,
Could not find artifact org.mybatis:mybatis:pom:unknown in aliyun (https://maven.aliyun.com/repository/public)
当然, 如果child project使用的version和dependencyManagement不一致需要进行单独设置.
组导入
正常情况下: 要求child project dependency groupId&&artifactId与 dependencyManagement下定义的一致,为了便于处理,可以进行组导入.
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2021.0.5.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
导入nacos配置启动器时,并不需要version.
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
然后 https://central.sonatype.com/ 查看了type指定要导入的类型jar
war\pom. pom就是导入一组配置.
<project
...
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2021.0.5.0</version>
</dependency>
...
spring-cloud-alibaba-dependencies-2021.0.5.0.pom
当然就不需要在写version.
完整代码
- parent 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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>abc</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>son-one</module>
</modules>
<properties>
<mybatis-version>3.5.9</mybatis-version>
<mysql-connector-version>8.0.33</mysql-connector-version>
<junit-version>4.13.2</junit-version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis-version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector-version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit-version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</dependency>
</dependencies>
</project>
- child 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>abc</artifactId>
<groupId>com.example</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>son-one</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</dependency>
</dependencies>
</project>
参考内容 Maven创建父子工程详解