1、创建父项目
cmd进入项目工作目录,执行以下代码:
mvn archetype:generate
创建成功,接着修改pom.xml如下:
<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>testparentgroupid</groupId> <artifactId>testparentartifactId</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging> <name>testparentartifactId</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
注:修改packageing打包方式为pom,父项目的packageing设置为pom,才能去管理其他工程.
2、父项目创建子项目
cmd进入父项目pom.xml所在的目录,执行构建代码如下:
mvn archetype:generate
子项目创建成功,此时Maven修改了父项目的pom.xml内容如下:
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <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>testparentgroupid</groupId> <artifactId>testparentartifactId</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging> <name>testparentartifactId</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <modules> <module>testchild1artifactId</module> </modules> </project>
新增了modles节点,将子项目包含到了里面.建立了父项目与子项目的关联.通过modles节点,子项目的pom.xml内容如下:
<?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>testparentgroupid</groupId> <artifactId>testparentartifactId</artifactId> <version>1.0-SNAPSHOT</version> </parent> <groupId>testchild1groupId</groupId> <artifactId>testchild1artifactId</artifactId> <version>1.0-SNAPSHOT</version> <name>testchild1artifactId</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
可以看到新生成了一个parent节点,Maven自动建立了子项目父项目的关联通过parent节点.
注:如果子项目和父项目的groupId和version一样,一般情况下是一样的.这两个属性可以删除,只保留artifactId,所以修改子项目pom如下:
<?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>testparentgroupid</groupId> <artifactId>testparentartifactId</artifactId> <version>1.0-SNAPSHOT</version> </parent> <artifactId>testchild1artifactId</artifactId> <name>testchild1artifactId</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
到这里,父子项目已经建立了关联
3、父项目管理所有依赖信息
修改父项目的pom.xml,如下:
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <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>testparentgroupid</groupId> <artifactId>testparentartifactId</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging> <name>testparentartifactId</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> </dependencyManagement> <modules> <module>testchild1artifactId</module> </modules> </project>
父项目通过dependencyManagement节点引入了一系列的sping框架依赖.注意这里只是一个列表而已,不会真正的去下载这些依赖.可以通过cmd进入父项目的pom.xml,执行如下代码:
mvn dependency:list
可以发现父项目没有去执行任何下载包的操作
修改子项目的pom.xml如下:
<?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>tpgroupId</groupId> <artifactId>tpartifactId</artifactId> <version>1.0-SNAPSHOT</version> </parent> <groupId>tpchild1groupId</groupId> <artifactId>tpchild1artifactId</artifactId> <version>1.0-SNAPSHOT</version> <name>tpchild1artifactId</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
这里添加了spring-aop的依赖.因为本地maven仓库和相关仓库没有,所以如下图
执行了相关下载.同时在切换为父项目查看依赖,cmd进入父项目的pom.xml目录,执行如下代码:
mvn dependency:list
父项目依然没有依赖.
到这里,可以菜刀父项目提供了一个依赖列表,其提供了依赖默认的版本,可以在子项目中不指定依赖的版本,而采用父项目的版本,因为上面的子项目并没有指定spring-aop的具体版本,但是却下载了父项目中的spring-aop的版本.所以pom项目有一定的版本管理功能,version遵循以下规律:
(1)、父项目已经引入的依赖,子项目可以不用写version ,此时子项目采用父项目的version
(2)、如果子项目写了version和父项目的version不一致,采用子项目的version
4、查看子项目的依赖层级
cmd进入指定子项目的pom.xml,执行如下代码:
mvn dependency:tree
5、父项目升级管理的所有依赖项的版本
修改父项目的pom.xml如下:
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <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>tpgroupId</groupId> <artifactId>tpartifactId</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging> <name>tpartifactId</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.1.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.1.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>4.1.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.1.0.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> </dependencyManagement> <modules> <module>tpchild1artifactId</module> </modules> </project>
升级spring相关项目的版本到4.1.0,接着cmd进入子项目的pom.xml所在的目录执行如下代码:
mvn dependency:list
可以看到下载了4.1.0相关的包,依赖解析结果也显示升级到了4.1.0相关的版本,再看下本地Maven仓库
4.0.0和4.1.0都存在于仓库中
6、父子项目添加自定义属性,控制pom中的相关配置
修改父项目pom.xml如下:
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <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>tpgroupId</groupId> <artifactId>tpartifactId</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging> <name>tpartifactId</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <custome.spring.version>4.2.0.RELEASE</custome.spring.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${custome.spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${custome.spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${custome.spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>${custome.spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${custome.spring.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> </dependencyManagement> <modules> <module>tpchild1artifactId</module> </modules> </project>
这里在properties节点下新增了custome.spring.version节点,并修改了依赖列表中所有spring相关项目的version节点为 <version>${custome.spring.version}</version>
接着cmd定位到父项目所在的pom.xml,执行如下代码:
mvn dependency:list
可以看到子项目开始下载4.2.0版本的相关包.说明自定义属性配置成功,这里也可以在子项目的pom.xml中配置自定义属性.并不是只能在父项目中配置
标签:父子,项目,spring,springframework,Maven,pom,子项目,org,junit From: https://www.cnblogs.com/GreenLeaves/p/17429055.html