1.聚合和继承的概念

聚合:将多个项目放到一起运行

新建父项目,在pom.xml中添加

<modules>
        <module>SIP-utils</module>
        <module>SIP-web</module>
        <module>SIP-sql</module>
        <module>SIP-core</module>
    </modules>

这些就是被聚合的项目,这样mvn clean install这个pro的时候同时会将上面项目打包

继承:比如过个项目都用到junit,那么可以向java一样创建一个jutint父类

那么我们就可以在项目中继承一个父项目,在pom.xml进行如下配置

<parent>
        <groupId>com.learnPro</groupId>
        <artifactId>SIP-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

之后在父模块的pom中加入junit

<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

可以看到子模块项目会导入相应的jar包,父模块中的依赖会被子模块继承

2.通过命令行构建子模块

比如我们需要构建的模块结构如下:

2.1 构建父模块pro-parent

mvn org.apache.maven.plugins:maven-archetype-plugin:2.2:create -DgroupId=com.learnpro -DartifactId=pro-parent 
-DarchetypeArtifactId=maven-archetype-quickstart

这边碰到过个问题,之前用如下命令,但是构建不通过

mvn archetype:create -DgroupId=com.learnpro -DartifactId=pro-parent -DarchetypeArtifactId=maven-archetype-quickstart

解决方法,请参照:http://josh-persistence.iteye.com/blog/2258213

通过上面命令可以看到我们的项目已创建成功
这个时候,我们可以将src文件夹删除(因为这个父项目只是个容器,不需要任何代码)
修改pom.xml文件,将jar修改为pom,pom表示它是一个被继承的模块,修改后的内容如下

<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.learnpro</groupId>
  <artifactId>pro-parent</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging><!--从jar改为pom-->

  <name>pro-parent</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>

2.2 构建子模块dao

在之前的文件夹中运行如下命令

cd pro-parent

mvn org.apache.maven.plugins:maven-archetype-plugin:2.2:create -DartifactId=pro-dao -DarchetypeArtifactId=maven-archetype-quickstart

编译成功后,在pro-parent的pom.xml中会自动增加如下内容

<modules>
    <module>pro-dao</module>
  </modules>

让我们再来看看pro-dao的pom文件

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://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>com.learnpro</groupId>
    <artifactId>pro-parent</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <groupId>com.learnpro</groupId>
  <artifactId>pro-dao</artifactId>
  <version>1.0-SNAPSHOT</version>
  <name>pro-dao</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>,我们需要把<groupId>com.learnpro</groupId>和<version>1.0-SNAPSHOT</version>去掉,并加上<packaging>jar</packaging>,因为我们已经有一个父模块了groupId和version会继承父模块中的groupId和version,最后将packaging打包方式设置为jar

3.eclipse插件构建项目

3.1 构建父项目

右键->new->other->搜索maven,选择Maven Project

maven 子模块 maven 子模块继承profile_maven 子模块


maven 子模块 maven 子模块继承profile_继承_02

3.2 构建子模块

右键->new->other->搜索maven,选择Maven Module

maven 子模块 maven 子模块继承profile_maven_03

这时候next,然后选择Maven项目模型,就可以创建对应的子模块
同时,我们可以发现pro-parent的pom文件中也增加了如下内容

<modules>
    <module>pro-children</module>
  </modules>

这时候,我们也需要把<groupId>com.learnpro</groupId>和<version>1.0-SNAPSHOT</version>去掉,并加上<packaging>jar</packaging>

3.3 导入Maven项目

右键->Import->搜索maven,选择Existing Maven Project

选我们之前搭建的pro-parent

maven 子模块 maven 子模块继承profile_apache_04