从netty项目单位入门maven的多modules模块配置

从netty项目组织入门maven的多modules模块配置

Netty是一套提供异步的、事件驱动的网络应用程序框架,同时也是工具包。我们可以将它作为项目的核心框架,同时也可以用他提供的部分功能来对项目进行支持,因此项目功能的模块化就显得很重要,这也是我们平时做项目所追求的可重用性。
netty一个项目整体的组织如下:
从netty项目单位入门maven的多modules模块配置
这里不说项目的各个子模块的功能,只是看如何实现项目模块的分离和组织。


在我们平时的开发中可能会有一个这样子的结构:
从netty项目单位入门maven的多modules模块配置
如果是一个小项目这样做也不错,至少做到了比较低层次的“高内聚,低耦合”。但是如果有10个这样的小项目,每个都会用到其中的util包,那么就会比较难过了,单是每个项目去同步util的状态就会耗很多精力。
如果用maven的模块化来进行管理会优美许多:
从netty项目单位入门maven的多modules模块配置
来看一下项目的构成:
首先tiegr-parent项目是我们真正的主项目,他包含了3个子模块:tiger-all,tiger-modules1和tiger-util 3个子模块。每个模块可以作为单独的项目进行开发和管理。并且这样做最直接的表现就是可重用性的提高以及模块间耦合度的降低。项目间的依赖完全有配置文件进行管理。
下面是模拟的项目间的依赖关系及配置:

<!-- tiger-parent/pom.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<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.tiger</groupId>  
  <artifactId>app-parent</artifactId>  
  <packaging>pom</packaging>  
  <version>1.0</version>  
  <modules>  
      <module>tiger-modules1</module>
      <module>tiger-util</module>
      <module>tiger-all</module>
  </modules> 

</project>
<!-- tiger-modules1/pom.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.tiger</groupId>
    <artifactId>app-parent</artifactId>
    <version>1.0</version>
  </parent>

  <artifactId>tiger-modules1</artifactId>
  <packaging>jar</packaging>

  <name>tiger-modules1</name>

  <dependencies>

    <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>tiger-util</artifactId>
        <version>${project.version}</version>
    </dependency>

  </dependencies>

</project>
<!-- tiger-util/pom.xml -->
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.tiger</groupId>
    <artifactId>app-parent</artifactId>
    <version>1.0</version>
  </parent>

  <artifactId>tiger-util</artifactId>
  <packaging>jar</packaging>

  <name>tiger-util</name>
  <url>http://maven.apache.org</url>

  <dependencies>
    <!-- JUnit testing framework -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.10</version>
        <scope>test</scope>
    </dependency>
  </dependencies>

</project>

从这里看出我们的项目间通过依赖关系进行管理,将每个模块或者说功能作为单独的项目进行引入。不仅关系很清晰,而且还可以为每个模块做到最大化的“瘦身”。
代码都这样管理突然觉得整个人心情都好了很多,有没有~^_^