zoukankan      html  css  js  c++  java
  • Maven聚合与继承的实例讲解(二)

       继续上一节讲Maven的内容,我们这个节继续讲Maven继承和聚合的其他内容。

       现在我们新建一个实例来测试Maven有关于聚合的部分
       
    测试开始

    一、建立以pom为packaging的项目为,然后再以这一个项目为parent project来聚合其他子项目
           
    • 新建立一个以pom的项目
    • 改写pom文件,依赖web-common,这样就可以将web-common中的jar包都实现过来,这个有点像 java中implement了一个接口后,在实现了吧。我们是packaging为pom的项目,所以也是给准备实现了该maven的project,做好铺垫吧。
      1. <projectxmlns="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>
        <groupId>com.company</groupId>
        <artifactId>web-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        </parent>
        <groupId>com.company</groupId>
        <artifactId>web-test</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>pom</packaging>
        <dependencies>
        <dependency>
        <groupId>com.company</groupId>
        <artifactId>web-common</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        </dependency>
        </dependencies>
        </project>
       
       
    • 所以我们会看到项目的初始文件目录,很简单



     
     
     
     
    二、我们接下来要根据上面建立的项目实现聚合的例子
     
    • 点击web-test这个项目,右键,新建一个maven的module
    • 创建一个来存放pojo的module部分,packaging为jar(不要选择pom或者war,具体看上一篇)

    • 点击finish
    • 这个时候,我们会发现web-test中的pom文件多了下面的内容
      1. <modules>
        <module>web-test-pojo</module>
        </modules>
    • 说明我们把web-test-pojo已经聚合到web-test中去了。
    • 同时web-test-pojo的内容是继承了web-common中的部分内容,而web-test-pojo的pom其实是不用修改的。

     
     
     
    三、同时我们再建立以web-test为基础的web-test-mapper和web-test-service的module,这个和步骤二是一模一样的,唯一有区别的地方就是建立完后要在pom文件中加入每个模块自己单独需要的dependencies。另外一个就是web-test-mapper是依赖于web-test-pojo,所以web-test-mapper中的dependencies是含有web-test-pojo的dependency,而web-test-service则是依赖于web-test-mapper。所以下面将展示web-test-mapper和web-test-service的pom文件。
    • web-test-mapper的pom文件如下:
      1. <projectxmlns="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>
        <groupId>com.company</groupId>
        <artifactId>web-test</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        </parent>
        <artifactId>web-test-mapper</artifactId>
        <!-- 依赖管理 -->
        <dependencies>
        <dependency>
        <groupId>com.company</groupId>
        <artifactId>web-test-pojo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        </dependency>
        <dependency>
        <groupId>com.github.miemiedev</groupId>
        <artifactId>mybatis-paginator</artifactId>
        </dependency>
        <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper</artifactId>
        </dependency>
        <!-- MySql -->
        <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!-- 连接池 -->
        <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        </dependency>
        </dependencies>
        </project>
       
    • web-test-service的pom文件
      1. <projectxmlns="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>
        <groupId>com.company</groupId>
        <artifactId>web-test</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        </parent>
        <artifactId>web-test-service</artifactId>
        <dependencies>
        <dependency>
        <groupId>com.company</groupId>
        <artifactId>web-test-mapper</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <!-- Spring -->
        <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        </dependency>
        <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        </dependency>
        <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        </dependency>
        <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        </dependency>
        <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        </dependency>
        </dependencies>
        </project>
     
     
     
    四、建立以war为基础的Maven项目,我们之前已经建立了三个以web-test为parent的Maven的jar的模块,但是要发布一个web项目,必须是有war包,war中才含有文件夹来存放前端的js,jsp等文件啦。和上面一样,在web-test上面创建module,不同的是,创建的packaging是war
     
    • 创建一个module
    • 选择war
    • 在pom文件中加入自己想要的dependency
      1. <projectxmlns="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>
        <groupId>com.company</groupId>
        <artifactId>web-test</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        </parent>
        <artifactId>web-test-web</artifactId>
        <packaging>war</packaging>
        <!-- 依赖管理 -->
        <dependencies>
        <dependency>
        <groupId>com.company</groupId>
        <artifactId>web-test-service</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <!-- JSP相关 -->
        <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        </dependency>
        <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <scope>provided</scope>
        </dependency>
        <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jsp-api</artifactId>
        <scope>provided</scope>
        </dependency>
        <!-- 文件上传组件 -->
        <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        </dependency>
        </dependencies>
        </project>
     
     
     
    五、此时,我们可以看到web-test其实已经聚合四个module,四个module在web-test的基础建立起来,但是他们又是一个整体,所以,当项目发布的时候,其实以web-test为单位的。
    <projectxmlns="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>
    <groupId>com.company</groupId>
    <artifactId>web-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    </parent>
    <groupId>com.company</groupId>
    <artifactId>web-test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
    <dependencies>
    <dependency>
    <groupId>com.company</groupId>
    <artifactId>web-common</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    </dependency>
    </dependencies>
    <modules>
    <module>web-test-pojo</module>
    <module>web-test-mapper</module>
    <module>web-test-service</module>
    <module>web-test-web</module>
    </modules>
    </project>



     
     
    六、其实到了这里,我们就把一个项目的聚合和模块被聚合的例子讲完了,但是如果要跑这个项目也是可以的。
    • 在web-test中pom的文件加入以下内容,将tomcat的插件配置到web-test中去
    1. <build>
      <!-- 配置插件 -->
      <plugins>
      <plugin>
      <groupId>org.apache.tomcat.maven</groupId>
      <artifactId>tomcat7-maven-plugin</artifactId>
      <configuration>
      <port>8080</port>
      <path>/</path>
      </configuration>
      </plugin>
      </plugins>
      </build>
    • 另外在,web-parent和web-common安装到本地的仓库中去
      在web-parent项目上面右键,然后run as 
    • 然后在goal 填入install
    • 同样的步骤在web-common项目执行
    • 执行后,在web-test这个主项目中run下面的代码  clean tomcat7:run


      最后就可以登陆看web-test中的首页了,嘻嘻

        
     





  • 相关阅读:
    BZOJ 1036 [ZJOI2008]树的统计Count(动态树)
    HDU 4010 Query on The Trees(动态树)
    Bootstrap框架
    【价格谈判】——在生意场合胜出的50个谈判绝招
    导出
    邓_ Php·魔术方法
    邓_tp_笔记
    UI 网页三原色
    邓_ 表单验证
    邓_ ThinkPhp框架
  • 原文地址:https://www.cnblogs.com/kgrdomore/p/5776846.html
Copyright © 2011-2022 走看看