zoukankan      html  css  js  c++  java
  • maven 多模块项目

    原文:http://www.blogjava.net/fancydeepin/archive/2015/06/27/maven-modules.html

    项目结构

    proj
      |
      |— proj-model
      |      |
      |      |— src
      |      |— pom (jar)
      |      |
      |
      |— proj-dao
      |      |
      |      |— src
      |      |— pom (jar)
      |      |
      |
      |— proj-service
      |      |
      |      |— src
      |      |— pom (jar)
      |      |
      |
      |— proj-web
      |      |
      |      |— src
      |      |— pom (war)
      |      |
      |
      |— pom.xml (pom)
      |

    这里的 proj 项目分了 4 个模块,它们分别是:proj-model(数据模型层)、proj-dao(数据库访问层)、proj-service(业务逻辑层)、
    proj-web(页面表现层)。下面我们来依次的来创建这几个项目(确保你的 maven 环境已经配置好,以下是 window 环境的 cmd 终端操作) 。

    创建模块项目

    01.创建 proj 项目
    mvn archetype:create -DgroupId=org.fanlychie -DartifactId=proj
     
    02.创建 proj-model 项目
    mvn archetype:create -DgroupId=org.fanlychie -DartifactId=proj-model
     
    03.创建 proj-dao 项目
    mvn archetype:create -DgroupId=org.fanlychie -DartifactId=proj-dao
     
    04.创建 proj-service 项目
    mvn archetype:create -DgroupId=org.fanlychie -DartifactId=proj-service
     
    05.创建 proj-web 项目
    mvn archetype:create -DgroupId=org.fanlychie -DartifactId=proj-web -DarchetypeArtifactId=maven-archetype-webapp
     
    06.将 proj-model、proj-dao、proj-service、proj-web 移动到 proj 目录里
    for /r /d %i in (proj-*) do move %i proj
     
    07.删除 proj/src 文件夹(proj 作为父模块,不需要 src)
    rd /S /Q projsrc
     
    08.为 proj-web 创建 src/main/java 目录
    mkdir projproj-websrcmainjava
     
    09.为 proj-web 创建 src/test/java 目录
    mkdir projproj-websrc estjava
     
    10.删除 proj-model、proj-dao、proj-service、proj-web 项目 src/main/java 的源文件(忽略错误)
    for /r /d %i in (projproj-*) do rd /S /Q %isrcmainjavaorg
     
    11.删除 proj-model、proj-dao、proj-service、proj-web 项目 src/test/java 的源文件(忽略错误)
    for /r /d %i in (projproj-*) do rd /S /Q %isrc estjavaorg

    父模块 proj/pom.xml 配置

    作为父模块,packaging 类型必须是 pom
    <groupId>org.fanlychie</groupId>
    <artifactId>proj</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>
    声明子模块 
    <modules>
      <module>proj-model</module>
      <module>proj-dao</module>
      <module>proj-service</module>
      <module>proj-web</module>
    </modules>
    在父模块中可以通过 dependencyManagement 对依赖的包进行统一的管理 
    <properties>
      <spring.version>4.1.5.RELEASE</spring.version>
    </properties>
    <dependencyManagement>
      <dependencies>
        <dependency>
          <groupId>org.fanlychie</groupId>
          <artifactId>proj-dao</artifactId>
          <version>${project.version}</version>
        </dependency>
        <dependency>
          <groupId>org.fanlychie</groupId>
          <artifactId>proj-model</artifactId>
          <version>${project.version}</version>
        </dependency>
        <dependency>
          <groupId>org.fanlychie</groupId>
          <artifactId>proj-service</artifactId>
          <version>${project.version}</version>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>${spring.version}</version>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>${spring.version}</version>
        </dependency>
      </dependencies>
    </dependencyManagement>
    配置完成之后,父模块以及父模块的子模块不会引入任何的构件依赖,这只是为了让子模块能够继承得到这些配置,这样做的好处是,在子模块中只
    需要声明 groupId 和 artifactId 不需要声明 version 就能准确的获取得到依赖的构件。这样一来,不仅能够使子模块的依赖配置变得简单,
    还能使项目范围内所有依赖的构件的版本能够得到统一,它们可以在父模块中统一的来管理,这样可以避免模块之间由于版本不一致而引发的异常。
    你还可以在父模块中通过 pluginManagement 来对依赖的插件进行统一的管理 
    <build>
      <pluginManagement>
        <plugins>
          <plugin>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-maven-plugin</artifactId>
            <version>9.2.8.v20150217</version>
          </plugin>
        </plugins>
      </pluginManagement>
    </build>
    proj/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>org.fanlychie</groupId>
      <artifactId>proj</artifactId>
      <version>1.0-SNAPSHOT</version>
      <packaging>pom</packaging>
      <name>proj</name>
      <url>http://maven.apache.org</url>
      <properties>
        <spring.version>4.1.5.RELEASE</spring.version>
      </properties>
      <modules>
        <module>proj-model</module>
        <module>proj-dao</module>
        <module>proj-service</module>
        <module>proj-web</module>
      </modules>
      <dependencyManagement>
        <dependencies>
          <dependency>
            <groupId>org.fanlychie</groupId>
            <artifactId>proj-dao</artifactId>
            <version>${project.version}</version>
          </dependency>
          <dependency>
            <groupId>org.fanlychie</groupId>
            <artifactId>proj-model</artifactId>
            <version>${project.version}</version>
          </dependency>
          <dependency>
            <groupId>org.fanlychie</groupId>
            <artifactId>proj-service</artifactId>
            <version>${project.version}</version>
          </dependency>
          <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
          </dependency>
          <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
          </dependency>
        </dependencies>
      </dependencyManagement>
      <build>
        <pluginManagement>
          <plugins>
            <plugin>
              <groupId>org.eclipse.jetty</groupId>
              <artifactId>jetty-maven-plugin</artifactId>
              <version>9.2.8.v20150217</version>
            </plugin>
          </plugins>
        </pluginManagement>
      </build>
    </project>

    子模块 proj-model/pom.xml 配置

    作为子模块,需要在 pom.xml 中声明其父模块 
    <parent>
      <groupId>org.fanlychie</groupId>
      <artifactId>proj</artifactId>
      <version>1.0-SNAPSHOT</version>
    </parent>
    GAV(groupId、artifactId、version)参考父模块。
    maven 默认认为父模块是在当前项目 pom.xml 所在的目录的上一级目录中,这里的 proj 项目结构设计符合这个规则。如果你的项目结构并不是
    这样,你必须通过 <relativepath> 节点来指定你的父模块 pom.xml 所在的路径(默认是 <relativepath>../</relativepath>)。 
    <parent>
      <groupId>org.fanlychie</groupId>
      <artifactId>proj</artifactId>
      <version>1.0-SNAPSHOT</version>
      <relativePath>../</relativePath>
    </parent>
    proj-model/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>
        <groupId>org.fanlychie</groupId>
        <artifactId>proj</artifactId>
        <version>1.0-SNAPSHOT</version>
      </parent>
      <artifactId>proj-model</artifactId>
      <packaging>jar</packaging>
      <name>proj-model project</name>
      <url>http://maven.apache.org</url>
    </project>
    groupId 和 version 会从父模块中继承,在子模块中不需要再配置。

    子模块 proj-dao/pom.xml 配置

    为该模块声明 proj-model 的依赖,因为该模块需要使用 proj-model 模块中的数据模型 
    <dependencies>
      <dependency>
        <groupId>org.fanlychie</groupId>
        <artifactId>proj-model</artifactId>
      </dependency>
    </dependencies>
    proj-dao/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>
        <groupId>org.fanlychie</groupId>
        <artifactId>proj</artifactId>
        <version>1.0-SNAPSHOT</version>
      </parent>
      <artifactId>proj-dao</artifactId>
      <packaging>jar</packaging>
      <name>proj-dao project</name>
      <url>http://maven.apache.org</url>
      <dependencies>
        <dependency>
          <groupId>org.fanlychie</groupId>
          <artifactId>proj-model</artifactId>
        </dependency>
      </dependencies>
    </project>

    子模块 proj-service/pom.xml 配置

    为该模块声明 proj-model、proj-dao 模块的依赖,因为该模块需要使用 proj-model 中的数据模型和 proj-dao 中的数据库访问接口 
    <dependencies>
      <dependency>
        <groupId>org.fanlychie</groupId>
        <artifactId>proj-model</artifactId>
      </dependency>
      <dependency>
        <groupId>org.fanlychie</groupId>
        <artifactId>proj-dao</artifactId>
      </dependency>
    </dependencies>
    proj-service/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>
        <groupId>org.fanlychie</groupId>
        <artifactId>proj</artifactId>
        <version>1.0-SNAPSHOT</version>
      </parent>
      <artifactId>proj-service</artifactId>
      <packaging>jar</packaging>
      <name>proj-service project</name>
      <url>http://maven.apache.org</url>
      <dependencies>
        <dependency>
          <groupId>org.fanlychie</groupId>
          <artifactId>proj-model</artifactId>
        </dependency>
        <dependency>
          <groupId>org.fanlychie</groupId>
          <artifactId>proj-dao</artifactId>
        </dependency>
      </dependencies>
    </project>

    子模块 proj-web/pom.xml 配置

    为该模块声明 proj-model、proj-service 的依赖,因为 proj-web 需要使用 proj-model 中的数据模型和 proj-service 中的业务逻辑 
    <dependencies>
      <dependency>
        <groupId>org.fanlychie</groupId>
        <artifactId>proj-model</artifactId>
      </dependency>
      <dependency>
        <groupId>org.fanlychie</groupId>
        <artifactId>proj-service</artifactId>
      </dependency>
    </dependencies>
    proj-web/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/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <parent>
        <groupId>org.fanlychie</groupId>
        <artifactId>proj</artifactId>
        <version>1.0-SNAPSHOT</version>
      </parent>
      <artifactId>proj-web</artifactId>
      <packaging>war</packaging>
      <name>proj-web project</name>
      <url>http://maven.apache.org</url>
      <dependencies>
        <dependency>
          <groupId>org.fanlychie</groupId>
          <artifactId>proj-model</artifactId>
        </dependency>
        <dependency>
          <groupId>org.fanlychie</groupId>
          <artifactId>proj-service</artifactId>
        </dependency>
      </dependencies>
      <build>
        <finalName>proj-web</finalName>
      </build>
    </project>

    导入项目到 eclipse

    配置完成之后,接下来可以将项目导入到 eclipse,在 eclipse 中右键 --> Import --> Maven --> Existing Maven Projects,将 proj
    项目所在的路径复制粘贴到 Root Directory 栏并敲回车,点击 Finish 按钮即可。 

    导入后的项目结构 

    示例源码下载:proj.zip
    示例使用 jetty 作为 web 服务器,选中 proj-web --> 右键 --> Run As --> Maven build... --> Goals 栏输入 jetty:run

    记得把 Resolve Workspace artifacts 选项勾选上,否则启动时会报错。访问方式:http://localhost/info

    打包

    选中父模块项目 proj --> 右键 --> Run As --> Maven build... --> Goals 栏输入 clean package 并执行。
    [INFO] Scanning for projects...
    [INFO] ------------------------------------------------------------------------
    [INFO] Reactor Build Order:
    [INFO] 
    [INFO] proj
    [INFO] proj-model project
    [INFO] proj-dao project
    [INFO] proj-service project
    [INFO] proj-web project
    [INFO]                                                                         
    [INFO] ------------------------------------------------------------------------
    . . . . . .
    [INFO] ------------------------------------------------------------------------
    [INFO] Reactor Summary:
    [INFO] 
    [INFO] proj ............................................... SUCCESS [  0.002 s]
    [INFO] proj-model project ................................. SUCCESS [  1.100 s]
    [INFO] proj-dao project ................................... SUCCESS [  0.102 s]
    [INFO] proj-service project ............................... SUCCESS [  0.069 s]
    [INFO] proj-web project ................................... SUCCESS [  0.552 s]
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
     
    可以看到,maven 在 build 的时候,会根据模块之间的依赖关系,整理出一个 build 的顺序,然后再按顺序来 build。
    执行完 package 后,各个子模块的 target 目录下会输出相应的 JAR 或 WAR 包。



      

  • 相关阅读:
    这可能是全网最全的流媒体知识科普文章
    JavaCV实战专栏文章目录(JavaCV速查手册)
    JavaCV复杂滤镜filter特效处理入门教程和常用案例汇总
    【PC桌面软件的末日,手机移动端App称王】写在windows11支持安卓,macOS支持ios,龙芯支持x86和arm指令翻译
    如何在龙芯架构和国产化操作系统平台上运行javacv
    如何在国产龙芯架构平台上运行c/c++、java、nodejs等编程语言
    JavaCV开发详解之35:使用filter滤镜实现画中画,以屏幕画面叠加摄像头画面为例
    JavaCV开发详解之34:使用filter滤镜实现字符滚动和无限循环滚动字符叠加,跑马灯特效制作
    JavaCV开发详解之33:使用filter滤镜实现动态日期时间叠加
    JavaCV开发详解之32:使用filter滤镜实现中文字符叠加
  • 原文地址:https://www.cnblogs.com/shihaiming/p/6290593.html
Copyright © 2011-2022 走看看