6.Maven聚合和继承,相关案例配置

来源:互联网 发布:cctv中国网络电视台 编辑:程序博客网 时间:2024/06/04 19:19


1有时候一个项目中有很多个模块,要想各个模块之间影响较小,可以将每个模块拿出来作为一个项目模块,对每个项目模块进行独立的开发。

 

2在这些过程中会遇到关于聚合和继承的问题。

 

3何为聚合?

A如果我们想一次构建多个项目模块,那我们就需要对多个项目模块进行聚合,也就是说当我们点击一个pom的时候,同时运行多个项目模块。这时候要用到以下的标签,将各个项目模块整合在一起。

<modules>

<module>…</module>

</modules>

 

4何为继承

A继承是为了消除重复,我们把很多相同的配置提取出来。(这样的好处是:统一管理jar包,方便升级)

B例如:groupId,version等。

 

5聚合与继承的关系:

A聚合主要是为了快速构建项目

B继承主要是为了消除重复。

案例(先简单概括,后全部列出):

一:聚合配置代码(注意,下面这些代码在父项目模块中)

<modules>

  <module>../Hello</module>

  <module>../HelloFriend</module>

  <module>../MakeFriends</module>

</modules>

注意:其中module的路径为相对路径。

 

二:继承配置代码(下面配置在各个子项目模块中)

<parent> 

   <groupId>cn.toto.maven</groupId>

   <artifactId>Project</artifactId>

  <version>0.0.1-SNAPSHOT</version>

   <!相对父pom的位置,也就是说通过下面的配置找到父pom的位置-->

   <relativePath>../parent/pom.xml</relativePath>

</parent>

 

三、继承代码过程中,可以定义属性

<properties>

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

   <junit.version>4.9</junit.version>

   <maven.version>0.0.1-SNAPSHOT</maven.version>

</properties>

访问属性的方式为${junit.version},例如:

<dependency>

   <groupId>junit</groupId>

   <artifactId>junit</artifactId>

   <version>${junit.version}</version>

   <scope>test</scope>

</dependency>

 

四:在父模块中用dependencyManagement进行管理(通过下面的配置可以知道每个模块是什么,同时:这样的好处是子模块可以有选择行的继承,而不需要全部继承)

<dependencyManagement>

   <dependencies>

      <dependency>

         <groupId>junit</groupId>

         <artifactId>junit</artifactId>

         <version>${junit.version}</version>

         <scope>test</scope>

      </dependency>

      <dependency>

         <groupId>cn.toto.maven</groupId>

         <artifactId>HelloFriend</artifactId>

         <version>${maven.version}</version>

         <type>jar</type>

         <scope>compile</scope>

      </dependency>

    </dependencies>

 </dependencyManagement>

 

 

6实际案例进行说明

模块结构:

一:父模块(Parent):

模块结构:

<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>cn.toto.maven</groupId>

 <artifactId>Parent</artifactId>

 <version>0.0.1-SNAPSHOT</version>

  <packaging>pom</packaging> <!---注意,这里要写成pom-->

 

 <name>Parent</name>

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

 

<!—为实现聚合,点击这个pom.xml的时候四个同时运行--->

<modules>

     <module>../Hello</module> 

     <module>../HelloFriend</module>         

     <module>../MakeFriends</module>

     <module>../web</module>

</modules>

 

 <!—属性配置--à

 <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>cn.itcast.maven</groupId>

       <artifactId>HelloFriend</artifactId>

     <version>0.0.1-SNAPSHOT</version>

     <scope>compile</scope>

   </dependency>

   <dependency>

                           <groupId>cn.itcast.maven</groupId>

                           <artifactId>Hello</artifactId>

                           <version>0.0.1-SNAPSHOT</version>

                           <scope>compile</scope>

                  </dependency>

                  <dependency>

                           <groupId>cn.itcast.maven</groupId>

                           <artifactId>MakeFriends</artifactId>

                           <version>0.0.1-SNAPSHOT</version>

                           <scope>compile</scope>

                  </dependency>

 </dependencies>

</dependencyManagement>

<distributionManagement>

        <repository>

           <id>releases</id>

           <name>Internal Releases</name>

           <url>http://localhost:8080/nexus-2.1.2/content/repositories/releases/</url>

        </repository>

        <snapshotRepository>

           <id>snapshots</id>

           <name>Internal Snapshots</name>

           <url>http://localhost:8080/nexus-2.1.2/content/repositories/snapshots/</url>

        </snapshotRepository>

 </distributionManagement>

 

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

 <!---此处去掉了些公共的配置,但是在Parent中的pom.xml中有相关的配置-->

 <artifactId>Hello</artifactId>

 <name>Hello</name>

 

 <parent> 

         <groupId>cn.itcast.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>

   </dependencies>

</project>

相关java代码略

 

三:HelloFriendpom.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>cn.toto.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>cn.toto.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>

 

 

 

 <name>MakeFriends</name>

 <parent> 

         <groupId>cn.itcast.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>cn.toto.maven</groupId>

     <artifactId>HelloFriend</artifactId>

   </dependency>

   </dependencies>

</project>

相关java代码略

 

0 0