最近做了maven的一些实验,这一次我们就来创建maven父子项目,首先我们应该建一个普通项目util,一个common项目,common中需要用到util的项目中的类,这时候我们就需要建立父子项目的关系了。spring 扫描jar包的问题,如果jar包没有directory entry的话,则不会扫描,也就是说如果我们的项目中加入注解,然后用spring扫描可能扫描不到,必须在bean中配置,或者是mvn install 的时候就自动选择directory entry,还有一种方式就是install的时候将多个war包打到一个包中
首先建立一个maven quickstart项目util,然后新建common的项目,然后再新建一个maven simple project
选择next并且填写groupId,artifactId,以及packing的形式为pom
创建成功,但是我在某一次的实验的时候创建失败,出现如下的错误
Failed to create project cuparent1 invalid project description,出错的原因是我Use default workSpace location 我选择位置导致出现了错误。
新建maven simple project 的第一个页面不要做额外的操作
接着我们在cuparent中配置聚合,即modules,这样我们install cuparent的时候会自动install子项目
我们在 cuparent配置modules如下
<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.testParent</groupId>
<artifactId>cuparent</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<modules>
<module>../util</module>
<module>../common</module>
</modules>
</project>
在common中的配置如下
<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>
<artifactId>common</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>common</name>
<url> http://maven.apache.org</url>
<parent>
<groupId>com.testParent</groupId>
<artifactId>cuparent</artifactId>
<version>1.0</version>
<relativePath>../cuparent/pom.xml</relativePath>
</parent>
<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>
<dependency>
<groupId>com.testParent</groupId>
<artifactId>util</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project> 在util的项目中配置如下
<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>
<artifactId>util</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>util</name>
<url> http://maven.apache.org</url>
<parent>
<groupId>com.testParent</groupId>
<artifactId>cuparent</artifactId>
<version>1.0</version>
<relativePath>../cuparent/pom.xml</relativePath>
</parent>
<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>
这样一个父子聚合的项目就搭建完成了 over

本文介绍了如何使用Maven创建父子项目,并通过配置modules实现子项目的聚合。在创建过程中,详细讲述了如何设置groupId、artifactId以及packaging,以及解决可能出现的错误,如Invalid project description。此外,还讲解了父项目和子项目pom.xml文件的配置,包括dependency管理和相对路径引用,以确保子项目能正确引用其他模块的类。
1万+

被折叠的 条评论
为什么被折叠?



