首页 > 其他 > 详细

Maven 模块化开发

时间:2018-10-11 10:44:52      阅读:181      评论:0      收藏:0      [点我收藏+]

(1)Maven 模块化开发

在多人协同开发时,特别是规模较大的项目,为了方便日后的代码维护和管理,我们会将每个开发人员的工作细分到具体的功能和模块上。随着项目的不断扩大,模块也会越来越多,后续会更加难以维护和扩展,为了应对这种情况后期我们还会采用微服务架构的方式进行开发。

以当前教程为例,我们可以将模块划分为如下形式:

  • 统一的依赖管理(dependencies)
  • 通用的工具类(commons)
  • 领域模型(domain)
  • 管理后台(admin)
  • 商城前端(ui)
  • 接口模块(api)

整个模块化开发过程主要是在开发思想上稍作了一些转变,只需要按照下面的流程操作即可。

创建根项目(工程)

把之前的my-shop项目修改成my-shop-bak,然后重新创建一个名 my-shop 的工程(在e盘某个目录下创建一个叫my-shop的文件夹,在IDEA打开该文件夹),现在my-shop不叫项目了,叫工程,工程是模块化开发的。

新建工程的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.xue</groupId>
    <artifactId>my-shop</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <modules>

    </modules>
</project>

 

该项目称之为 Root 项目,主要作用是管理整个工程的全部模块,当有新模块加入时需要在 modules 元素下配置对应的模块目录

创建统一的依赖管理

在my-shop工程下创建一个名为 my-shop-dependencies 的项目,在其项目下创建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>
   <!--父类是my-shop工程,之后的项目都依赖当前项目(之后创建的项目都继承当前的项目) -->
    <parent>
        <groupId>com.xue</groupId>
        <artifactId>my-shop</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>
     <!--当前项目继承工程的所有属性和行为,这里继承artifactId、packaging-->
    <artifactId>my-shop-dependencies</artifactId>
    <packaging>pom</packaging>

    <name>my-shop-dependencies</name>
    <description></description>

    <properties>
        <!-- 环境配置的统一管理,告诉Maven 源码、报告及jdk版本指定为utf-8的 -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>

        <!-- 统一的依赖管理,配置依赖的版本号 ,下面直接引用即可-->
        <commons-lang3.version>3.5</commons-lang3.version>
        <jstl.version>1.2</jstl.version>
        <log4j.version>1.2.17</log4j.version>
        <servlet-api.version>3.1.0</servlet-api.version>
        <slf4j.version>1.7.25</slf4j.version>
        <spring.version>4.3.17.RELEASE</spring.version>
    </properties>

    <!--依赖管理,不直接下载依赖,只是管理依赖-->
    <dependencyManagement>
        <!--这里才把依赖装载进来-->
        <dependencies>
            <!-- Spring Begin -->
            <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>
            <!-- Spring End -->

            <!-- Servlet Begin -->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>${servlet-api.version}</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jstl</artifactId>
                <version>${jstl.version}</version>
            </dependency>
            <!-- Servlet End -->

            <!-- Log Begin -->
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>${slf4j.version}</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
                <version>${slf4j.version}</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>jcl-over-slf4j</artifactId>
                <version>${slf4j.version}</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>jul-to-slf4j</artifactId>
                <version>${slf4j.version}</version>
            </dependency>
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>${log4j.version}</version>
            </dependency>
            <!-- Log End -->

            <!-- Commons Begin -->
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-lang3</artifactId>
                <version>${commons-lang3.version}</version>
            </dependency>
            <!-- Commons End -->
        </dependencies>
    </dependencyManagement>
<!--build是构建,编译打包时想干嘛-->
    <build>
        <!--plugins是插件,编译时想用插件来完成想做的事情-->
        <plugins>
            <!-- Compiler 插件, 设定 JDK 版本 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                    <showWarnings>true</showWarnings>
                </configuration>
            </plugin>
        </plugins>

        <!-- 静态资源文件配置 -->
        <resources>
            <resource>
                <!--配置src/main/java目录下的-->
                <directory>src/main/java</directory>
                <!--排除以java结尾的文件-->
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </resource>
            <!--配置src/main/resources目录下的资源文件-->
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
    </build>
</project>

 

PS:别忘记在 my-shop 工程的 pom.xml 中增加 <module>my-shop-dependencies</module> 配置

<!--项目/模块-->
    <modules>
      <module>my-shop-dependencies</module>
    </modules>

 

创建通用的工具类

在my-shop工程下创建一个名为 my-shop-commons 的项目,用于存放所有的工具类的,在其项目下创建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>
    <!--父类是my-shop-dependencies项目 -->
    <parent>
        <groupId>com.xue</groupId>
        <artifactId>my-shop-dependencies</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <relativePath>../my-shop-dependencies/pom.xml</relativePath>
    </parent>
    <!--当前项目继承my-shop-dependencies项目的所有属性和行为,这里packaging是一个jar包,因为当前项目是一个类库-->
    <artifactId>my-shop-commons</artifactId>
    <packaging>jar</packaging>

    <name>my-shop-commons</name>
    <description></description>
   
    <dependencies>
     <!--当前项目只需要依赖commons-lang3,这里不需要版本号了,因为父项目已经帮管理版本号了-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>
        <!--日志依赖,这里不需要版本号了,因为父项目已经帮管理版本号了-->
        <!-- Log Begin -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jul-to-slf4j</artifactId>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
        </dependency>
        <!-- Log End -->
    </dependencies>
    </dependencies>

</project>

 

PS:别忘记在 my-shop 工程的 pom.xml 中增加 <module>my-shop-commons</module> 配置

<!--项目/模块-->
    <modules>
      <module>my-shop-dependencies</module>
      <module>my-shop-commons</module>
    </modules>

 

创建领域模型

在my-shop工程下创建一个名为 my-shop-domain 的项目,用于存放所有的实体类的,在其项目下创建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>
    <!--父类是my-shop-dependencies项目 -->
    <parent>
        <groupId>com.xue</groupId>
        <artifactId>my-shop-dependencies</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <relativePath>../my-shop-dependencies/pom.xml</relativePath>
    </parent>
    <!--当前项目继承my-shop-dependencies项目的所有属性和行为,这里packaging是一个jar包,因为当前项目是一个类库-->
    <artifactId>my-shop-domain</artifactId>
    <packaging>jar</packaging>

    <name>my-shop-domain</name>
    <description></description>

</project>

 

PS:别忘记在 my-shop 工程的 pom.xml 中增加 <module>my-shop-web-admin</module> 配置

<!--项目/模块-->
    <modules>
      <module>my-shop-dependencies</module>
      <module>my-shop-commons</module>
        <module>my-shop-domain</module>
    </modules>

 

创建管理后台

在my-shop工程下创建一个名为 my-shop-web-admin 的项目,用于存放所有的后台的相关代码的,在其项目下创建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>
    <!--父类是my-shop-dependencies项目 -->
    <parent>
        <groupId>com.xue</groupId>
        <artifactId>my-shop-dependencies</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <relativePath>../my-shop-dependencies/pom.xml</relativePath>
    </parent>
    <!--当前项目继承my-shop-dependencies项目的所有属性和行为,这里packaging是一个war包-->
    <artifactId>my-shop-web-admin</artifactId>
    <packaging>war</packaging>

    <name>my-shop-web-admin</name>
    <description></description>
 
    <dependencies>
      <!--因为后台会有实体类,所以要依赖领域模型(my-shop-domain)-->
        <dependency>
            <groupId>com.xue</groupId>
            <artifactId>my-shop-domain</artifactId>
            <!--引用项目父类的版本号-->
            <version>${project.parent.version}</version>
        </dependency>
         <!--依赖工具类-->
        <dependency>
            <groupId>com.xue</groupId>
            <artifactId>my-shop-commons</artifactId>
            <version>${project.parent.version}</version>
        </dependency>
        <!--依赖spring,不需要版本号了,因为工程的pom已经统一管理版本号了-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <!--依赖Spring MVC,不需要版本号了,因为工程的pom已经统一管理版本号了-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>
                <!--Servlet 的依赖 Begin-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <!--Servlet End-->
    </dependencies>
</project>

 

PS:别忘记在 my-shop 工程的 pom.xml 中增加 <module>my-shop-web-admin</module> 配置

<!--项目/模块-->
    <modules>
        <!--统一依赖管理的项目-->
        <module>my-shop-dependencies</module>
        <!--通用的工具类的项目-->
        <module>my-shop-commons</module>
        <!--领域模型(实体类)的项目-->
        <module>my-shop-domain</module>
        <!--管理后台的项目-->
        <module>my-shop-web-admin</module>
    </modules>

 

注意

由于我们用的快照版,当工程或者某个项目的某个版本号更改了,这时候我们需要总是更新快照版,修改setting的maven 技术分享图片

创建商城前端

在my-shop工程下创建一个名为 my-shop-web-ui 的项目,用于存放所有的商城前台的相关代码的,在其项目下创建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>
    <!--父类是my-shop-dependencies项目 -->
    <parent>
        <groupId>com.xue</groupId>
        <artifactId>my-shop-dependencies</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <relativePath>../my-shop-dependencies/pom.xml</relativePath>
    </parent>
    <!--当前项目继承my-shop-dependencies项目的所有属性和行为,这里packaging是一个war包-->
    <artifactId>my-shop-web-ui</artifactId>
    <packaging>war</packaging>

    <name>my-shop-web-ui</name>

    <description></description>
    <!--可能会依赖工具类-->
    <dependencies>
        <dependency>
            <groupId>com.xue</groupId>
            <artifactId>my-shop-commons</artifactId>
            <version>${project.parent.version}</version>
        </dependency>
    </dependencies>
</project>

 

PS:别忘记在 my-shop 工程的 pom.xml 中增加 <module>my-shop-web-ui</module> 配置

 <!--项目/模块-->
    <modules>
        <!--统一依赖管理的项目-->
        <module>my-shop-dependencies</module>
        <!--通用的工具类的项目-->
        <module>my-shop-commons</module>
        <!--领域模型(实体类)的项目-->
        <module>my-shop-domain</module>
        <!--管理后台的项目-->
        <module>my-shop-web-admin</module>
        <!--管理前台的项目-->
        <module>my-shop-web-ui</module>
    </modules>

 

创建接口模块

在my-shop工程下创建一个名为 my-shop-web-api 的项目,用于存放用到的应用程序接口及api的,并不是我们创建的interface,在其项目下创建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>
    <!--父类是my-shop-dependencies项目 -->
    <parent>
        <groupId>com.xue</groupId>
        <artifactId>my-shop-dependencies</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <relativePath>../my-shop-dependencies/pom.xml</relativePath>
    </parent>
    <!--当前项目继承my-shop-dependencies项目的所有属性和行为,这里packaging是一个war包-->
    <artifactId>my-shop-web-api</artifactId>
    <packaging>war</packaging>

    <name>my-shop-web-api</name>

    <description></description>
    <!--可能会依赖工具类-->
    <dependencies>
        <dependency>
            <groupId>com.xue</groupId>
            <artifactId>my-shop-commons</artifactId>
            <version>${project.parent.version}</version>
        </dependency>
    </dependencies>
</project>

 

PS:别忘记在 my-shop 工程的 pom.xml 中增加 <module>my-shop-web-api</module> 配置

 <!--项目/模块-->
    <modules>
        <!--统一依赖管理的项目-->
        <module>my-shop-dependencies</module>
        <!--通用的工具类的项目-->
        <module>my-shop-commons</module>
        <!--领域模型(实体类)的项目-->
        <module>my-shop-domain</module>
        <!--管理后台的项目-->
        <module>my-shop-web-admin</module>
        <!--管理前台的项目-->
        <module>my-shop-web-ui</module>
        <!--管理应用程序接口的项目-->
        <module>my-shop-web-api</module>
    </modules>

 

好了,到此基本的架构已经搭建好了

技术分享图片

 

Maven 模块化开发

原文:https://www.cnblogs.com/yaoyaoling/p/9771033.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!