码迷,mamicode.com
首页 > 其他好文 > 详细

Maven中聚合与继承

时间:2016-04-02 16:09:28      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:

何为继承?
--?继承为了消除重复,我们把很多相同的配置提取出来
--?例如: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中聚合与继承

标签:

原文地址:http://www.cnblogs.com/winner-0715/p/5347595.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!