maven的聚合和继承

来源:互联网 发布:sql删除完全重复数据 编辑:程序博客网 时间:2024/06/06 01:08

关于maven的聚合,我们现在假想一个场景:

如果我有三个maven项目,我需要将这三个项目打包发布到本地仓库,按照一般的做法,我会一个一个的右键:run as-->Maven build--->install,然后等待控制台出现build success,那么如果我有8个、10个甚至更多的maven项目需要打包发布,是否我要每个都这样操作呢(估计要累死,而且还不好维护),这个时候就用到了maven聚合。按照这个场景,我们新建三个maven项目,分别为A,B,C,其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>com.cn.maven1</groupId>
  <artifactId>A</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>A</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>

-------------

<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.cn.maven1</groupId>
  <artifactId>B</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>B</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>

------------

<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.cn.maven1</groupId>
  <artifactId>C</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>C</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>
-------

这是我们新建一个统一管理他们的maven项目,作为聚合的载体进行统一打包发布,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>com.cn.maven1</groupId>
  <artifactId>ABC</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <!-- 聚合载体需要打包成pom格式 -->
  <packaging>pom</packaging>

  <name>ABC</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <!-- 添加需要聚合的项目的模块名,这里模块名指的就是项目名 -->
  <modules>
      <module>../A</module>
      <module>../B</module>
      <module>../C</module>
  </modules>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

最后,选中该pom.xml,右键run as-->Maven build--->install。控制台会依次对A,B,C进行打包发布。

-------------------------------------------------

关于maven的继承,这个跟java的继承有点类似,假想在以上A、B、C三个项目中都会引入junit的jar包(实际项目中可能有很多重叠的包),这时我们想要将该包封装一下,作为一个统一让大家来调用,这样一来方便管理,而来不用每次都去找或者由于版本不同而引发冲突等问题。怎么来做呢?

首先新建一个maven项目,我们把它叫做父类maven容器,其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>parent</groupId>
  <artifactId>ABC_parent</artifactId>
  <version>1.0.0-SNAPSHOT</version>
  <packaging>pom</packaging>
  <name>ABC_parent</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <!-- 可将某个标签定义在properties中作为变量来被引用,引用方式支持EL表达式 -->
    <junit-version>3.8.1</junit-version>
  </properties>
  <!--dependencyManagement用来管理所有的依赖关系,不会运行  -->
  <dependencyManagement>
      <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <!-- 利用EL表达式来引用变量    -->
      <version>${junit-version}</version>
      <scope>test</scope>
    </dependency>
   </dependencies>
  </dependencyManagement>  
</project>







原创粉丝点击