Maven中聚合与继承

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Maven中聚合与继承相关的知识,希望对你有一定的参考价值。

何为继承?
--?继承为了消除重复,我们把很多相同的配置提取出来
--?例如:grouptId,version等

就像写java程序一样,对于有共性切重复的东西,就提取出来。

如有三个pom.xml配置文件,

Hello/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.tuniu.maven</groupId> <artifactId>Hello</artifactId> <version>0.0.1-SNAPSHOT</version> <name>Hello</name> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.9</version> <scope>test</scope> </dependency> </dependencies> </project>

HelloFriend/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.tuniu.maven</groupId> <artifactId>HelloFriend</artifactId> <version>0.0.1-SNAPSHOT</version> <name>HelloFriend</name> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.9</version> <scope>test</scope> </dependency> <dependency> <groupId>com.tuniu.maven</groupId> <artifactId>Hello</artifactId> <version>0.0.1-SNAPSHOT</version> <scope>compile</scope> </dependency> </dependencies> </project>

MakeFriends/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.tuniu.maven</groupId>
  <artifactId>MakeFriends</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>MakeFriends</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>4.9</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.tuniu.maven</groupId>
          <artifactId>HelloFriend</artifactId>
          <version>0.0.1-SNAPSHOT</version>
          <scope>compile</scope>
    </dependency>
  </dependencies>
</project>

这三个配置文件中有很多重复的代码。

新建一个Maven工程Parent,里面不需要代码,只是配置里面的pom.xml文件。

注意:
1.dependencyManagement中定义的依赖子module不会共享。
2.dependencies中定义的依赖子module可以共享。

Parent/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.tuniu.maven</groupId>
  <artifactId>Parent</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <!-- 父工程的打包方式必须是pom,不能是jar -->
  <packaging>pom</packaging>

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

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  
 <dependencyManagement>

  <dependencies>
    <dependency>
          <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.9</version>
        <scope>test</scope>
    </dependency>
    <dependency>
            <groupId>com.tuniu.maven</groupId>
            <artifactId>Hello</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>com.tuniu.maven</groupId>
          <artifactId>HelloFriend</artifactId>
          <version>0.0.1-SNAPSHOT</version>
          <scope>compile</scope>
    </dependency>    
  </dependencies>
  </dependencyManagement>
</project>

Hello/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> <artifactId>Hello</artifactId> <name>Hello</name> <parent> <groupId>com.tuniu.maven</groupId> <artifactId>Parent</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath>../Parent/pom.xml</relativePath> </parent> <dependencies> <!-- 相比以前不用写版本了,版本号写在父pom中,统一管理 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> </dependencies> </project>

HelloFriend/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> <artifactId>HelloFriend</artifactId> <name>HelloFriend</name> <parent> <groupId>com.tuniu.maven</groupId> <artifactId>Parent</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath>../Parent/pom.xml</relativePath> </parent> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> <dependency> <groupId>com.tuniu.maven</groupId> <artifactId>Hello</artifactId> </dependency> </dependencies> </project>

MakeFriends/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>

  <artifactId>MakeFriends</artifactId>
  <packaging>jar</packaging>

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

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

     <parent>  
          <groupId>com.tuniu.maven</groupId>
         <artifactId>Parent</artifactId>
          <version>0.0.1-SNAPSHOT</version>
        <relativePath>../Parent/pom.xml</relativePath>  
    </parent>
    
      <dependencies>
    <dependency>
          <groupId>junit</groupId>
        <artifactId>junit</artifactId>
    </dependency>
    <dependency>
        <groupId>com.tuniu.maven</groupId>
          <artifactId>HelloFriend</artifactId>
    </dependency>    
  </dependencies>
</project>
何为聚合?
?--如果我们想一次构建多个项目模块,那我们就需要对多个项目模块进行聚合
?--<modules>
  <module>…</module>
 </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.tuniu.maven</groupId>
  <artifactId>Parent</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <!-- 父工程的打包方式必须是pom,不能是jar -->
  <packaging>pom</packaging>

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

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  
<modules>
      <module>../Hello</module>  
      <module>../HelloFriend</module>        
      <module>../MakeFriends</module>
</modules>
  
 <dependencyManagement>

  <dependencies>
    <dependency>
          <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.9</version>
        <scope>test</scope>
    </dependency>
    <dependency>
            <groupId>com.tuniu.maven</groupId>
            <artifactId>Hello</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>com.tuniu.maven</groupId>
          <artifactId>HelloFriend</artifactId>
          <version>0.0.1-SNAPSHOT</version>
          <scope>compile</scope>
    </dependency>    
  </dependencies>
  </dependencyManagement>
</project>

 

以上是关于Maven中聚合与继承的主要内容,如果未能解决你的问题,请参考以下文章

maven聚合与继承

maven 聚合与继承

Maven中聚合与继承

maven-聚合与继承

011.maven 继承与聚合

Maven:Maven_02:依赖管理与冲突解决及项目继承聚合