IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

Maven学习总结(五)——聚合与继承

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

一、聚合

  如果我们想一次构建多个项目模块,那我们就需要对多个项目模块进行聚合

1.1、聚合配置代码

 <modules>
       <module>模块一</module>
       <module>模块二</module>
       <module>模块三</module>
 </modules>

  例如:对项目的Hello、HelloFriend、MakeFriends这三个模块进行聚合

 <modules>
       <module>../Hello</module>  
       <module>../HelloFriend</module>        
       <module>../MakeFriends</module>
 </modules>

  其中module的路径为相对路径。

二、继承

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

2.1、继承配置代码

 <parent>  
          <groupId>me.gacl.maven</groupId>
          <artifactId>ParentProject</artifactId>
          <version>0.0.1-SNAPSHOT</version>
          <relativePath>../ParentProject/pom.xml</relativePath>  
 </parent>

2.2、继承代码中定义属性

  继承代码过程中,可以定义属性,例如:

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

2.3、父模块用dependencyManagement进行管理

 <dependencyManagement>
     <dependencies>
     <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>${junit.version}</version>
         <scope>test</scope>
     </dependency>    
     <dependency>
             <groupId>cn.itcast.maven</groupId>
             <artifactId>HelloFriend</artifactId>
             <version>${maven.version}</version>
             <type>jar</type>
             <scope>compile</scope>
        </dependency>
      </dependencies>
 </dependencyManagement>

  这样的好处是子模块可以有选择行的继承,而不需要全部继承。

三、聚合与继承的关系

  聚合主要为了快速构建项目,继承主要为了消除重复

四、聚合与继承实战演练

  创建四个Maven项目,如下图所示:

  77_1.png

  这四个项目放在同一个目录下,方便后面进行聚合和继承

  77_2.png

  Parent项目是其它三个项目的父项目,主要是用来配置一些公共的配置,其它三个项目再通过继承的方式拥有Parent项目中的配置,首先配置Parent项目的pom.xml,添加对项目的Hello、HelloFriend、MakeFriends这三个模块进行聚合以及jar包依赖,pom.xml的配置信息如下:

  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>me.gacl.maven</groupId>
     <artifactId>Parent</artifactId>
     <version>0.0.1-SNAPSHOT</version>
     <packaging>pom</packaging>

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

     <!-- 对项目的Hello、HelloFriend、MakeFriends这三个模块进行聚合 -->
     <modules>
         <module>../Hello</module>
         <module>../HelloFriend</module>
         <module>../MakeFriends</module>
     </modules>

     <!-- 定义属性 -->
     <properties>
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
         <junit.version>4.9</junit.version>
         <maven.version>0.0.1-SNAPSHOT</maven.version>
     </properties>

     <!-- 用dependencyManagement进行jar包依赖管理 -->
     <dependencyManagement>
         <!-- 配置jar包依赖 -->
         <dependencies>
             <dependency>
                 <groupId>junit</groupId>
                 <artifactId>junit</artifactId>
                 <!-- 访问junit.version属性 -->
                 <version>${junit.version}</version>
                 <scope>test</scope>
             </dependency>
             <dependency>
                 <groupId>me.gacl.maven</groupId>
                 <artifactId>Hello</artifactId>
                 <!-- 访问maven.version属性 -->
                 <version>${maven.version}</version>
                 <scope>compile</scope>
             </dependency>
             <dependency>
                 <groupId>me.gacl.maven</groupId>
                 <artifactId>HelloFriend</artifactId>
                 <!-- 访问maven.version属性 -->
                 <version>${maven.version}</version>
             </dependency>
         </dependencies>
     </dependencyManagement>
 </project>

  在Hello项目的pom.xml中继承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>
   <artifactId>Hello</artifactId>

       <!-- 继承Parent项目中的pom.xml配置 -->
        <parent>  
           <groupId>me.gacl.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>

  在HelloFriend项目的pom.xml中继承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>
     <artifactId>HelloFriend</artifactId>
     <name>HelloFriend</name>

     <!-- 继承Parent项目中的pom.xml配置 -->
     <parent>
         <groupId>me.gacl.maven</groupId>
         <artifactId>Parent</artifactId>
         <version>0.0.1-SNAPSHOT</version>
         <relativePath>../Parent/pom.xml</relativePath>
     </parent>
     <dependencies>
         <dependency>
             <!-- Parent项目的pom.xml文件配置中已经指明了要使用的Junit的版本号,因此在这里添加junit的依赖时,
             可以不指明<version></version>和<scope>test</scope>,会直接从Parent项目的pom.xml继承 -->
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
         </dependency>
         <!-- HelloFriend项目中使用到了Hello项目中的类,因此需要添加对Hello.jar的依赖 
         Hello.jar的<version>和<scope>也已经在Parent项目的pom.xml文件配置中已经指明了
         因此这里也可以省略不写了
         -->
         <dependency>
             <groupId>me.gacl.maven</groupId>
             <artifactId>Hello</artifactId>
         </dependency>
     </dependencies>
 </project>

  在MakeFriends项目的pom.xml中继承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>
     <artifactId>MakeFriends</artifactId>
     <!-- 继承Parent项目中的pom.xml配置 -->
     <parent>
         <groupId>me.gacl.maven</groupId>
         <artifactId>Parent</artifactId>
         <version>0.0.1-SNAPSHOT</version>
         <relativePath>../Parent/pom.xml</relativePath>
     </parent>
     <dependencies>
         <dependency>
         <!-- Parent项目的pom.xml文件配置中已经指明了要使用的Junit的版本号,因此在这里添加junit的依赖时,
             可以不指明<version></version>和<scope>test</scope>,会直接从Parent项目的pom.xml继承 -->
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
         </dependency>
         <dependency>
         <!-- MakeFriends项目中使用到了HelloFriend项目中的类,因此需要添加对HelloFriend.jar的依赖 
         HelloFriend.jar的<version>和<scope>也已经在Parent项目的pom.xml文件配置中已经指明了
         因此这里也可以省略不写了
         -->
             <groupId>me.gacl.maven</groupId>
             <artifactId>HelloFriend</artifactId>
         </dependency>
     </dependencies>
 </project>

  以上的四个项目的pom.xml经过这样的配置之后,就完成了在Parent项目中聚合Hello、HelloFriend、MakeFriends这三个子项目(子模块),而Hello、HelloFriend、MakeFriends这三个子项目(子模块)也继承了Parent项目中的公共配置,这样就可以使用Maven一次性构建所有的项目了,如下图所示:

  77_3.png

  选中Parent项目的pom.xml文件→【Run As】→【Maven install】,这样Maven就会一次性同时构建Parent、Hello、HelloFriend、MakeFriends这四个项目,如下图所示:

  77_4.png

来源:http://dwz.date/2WH

文章永久链接:https://tech.souyunku.com/?p=15870

赞(63) 打赏



未经允许不得转载:搜云库技术团队 » Maven学习总结(五)——聚合与继承

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码
IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

评论 抢沙发

大前端WP主题 更专业 更方便

联系我们联系我们

觉得文章有用就打赏一下文章作者

微信扫一扫打赏

微信扫一扫打赏


Fatal error: Uncaught Exception: Cache directory not writable. Comet Cache needs this directory please: `/data/wangzhan/tech.souyunku.com.wp/wp-content/cache/comet-cache/cache/https/tech-souyunku-com/index.q`. Set permissions to `755` or higher; `777` might be needed in some cases. in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php:367 Stack trace: #0 [internal function]: WebSharks\CometCache\Classes\AdvancedCache->outputBufferCallbackHandler() #1 /data/wangzhan/tech.souyunku.com.wp/wp-includes/functions.php(5109): ob_end_flush() #2 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(303): wp_ob_end_flush_all() #3 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters() #4 /data/wangzhan/tech.souyunku.com.wp/wp-includes/plugin.php(470): WP_Hook->do_action() #5 /data/wangzhan/tech.souyunku.com.wp/wp-includes/load.php(1097): do_action() #6 [internal function]: shutdown_action_hook() #7 {main} thrown in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php on line 367