Maven 综合案例

1. 项目需求和结构分析

Maven 综合案例_第1张图片

需求案例:搭建一个电商平台项目,该平台包括用户服务、订单服务、通用工具模块等。

项目架构:

  1. 用户服务:负责处理用户相关的逻辑,例如用户信息的管理、用户注册、登录等。
    • spring-context 6.0.6
    • spring-core 6.0.6
    • spring-beans 6.0.6
    • common-service
  2. 订单服务:负责处理订单相关的逻辑,例如订单的创建、订单支付、退货、订单查看等。
    • spring-context 6.0.6
    • spring-core 6.0.6
    • spring-beans 6.0.6
    • spring-security 6.0.6
    • common-service
  3. 通用模块:负责存储其他服务需要通用工具类,其他服务依赖此模块。
    • commons-io 2.11.0
    • junit 5.9.2

2. 项目搭建和统一构建

① 父模块 (micro-shop)

创建工程:
Maven 综合案例_第2张图片

pom.xml 配置:(打包方式 pom 包,properties 标签下前几个 xxx.version 用于控制版本)


<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.0modelVersion>

    <groupId>com.atguigugroupId>
    <artifactId>micro-shopartifactId>
    <version>1.0-SNAPSHOTversion>
    
    <packaging>pompackaging>

    <properties>
        <spring.version>6.0.6spring.version>
        <jackson.version>2.15.0jackson.version>
        <commons.version>2.11.0commons.version>
        <junit.version>5.9.2junit.version>
        <maven.compiler.source>17maven.compiler.source>
        <maven.compiler.target>17maven.compiler.target>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    properties>

    
    <dependencyManagement>
        <dependencies>
            
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-contextartifactId>
                <version>${spring.version}version>
            dependency>

            
            <dependency>
                <groupId>com.fasterxml.jackson.coregroupId>
                <artifactId>jackson-databindartifactId>
                <version>${jackson.version}version>
            dependency>

            
            <dependency>
                <groupId>commons-iogroupId>
                <artifactId>commons-ioartifactId>
                <version>${commons.version}version>
            dependency>

            
            <dependency>
                <groupId>org.junit.jupitergroupId>
                <artifactId>junit-jupiter-apiartifactId>
                <version>${junit.version}version>
                <scope>testscope>
            dependency>

        dependencies>
    dependencyManagement>

    
    <build>
        
        <plugins>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-war-pluginartifactId>
                <version>3.2.2version>
            plugin>
        plugins>
    build>

project>

可选操作: 删除 src 目录

② 通用模块 (common-service)

创建工程:

Maven 综合案例_第3张图片

Maven 综合案例_第4张图片

pom.xml 配置:(打包为 jar 包,parent 标签为父工程的位置,dependencies 中各 jar 包版本继承父工程的)


<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">
    <parent>
        <artifactId>micro-shopartifactId>
        <groupId>com.atguigugroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>common-serviceartifactId>
    
    <packaging>jarpackaging>

    <properties>
        <maven.compiler.source>17maven.compiler.source>
        <maven.compiler.target>17maven.compiler.target>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    properties>

    <dependencies>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-contextartifactId>
        dependency>
        
        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-databindartifactId>
        dependency>
        
        <dependency>
            <groupId>commons-iogroupId>
            <artifactId>commons-ioartifactId>
        dependency>
        
        <dependency>
            <groupId>org.junit.jupitergroupId>
            <artifactId>junit-jupiter-apiartifactId>
            <scope>testscope>
        dependency>
    dependencies>

project>

③ 用户模块 (user-service)

创建工程:
Maven 综合案例_第5张图片
Maven 综合案例_第6张图片

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">
    <parent>
        <artifactId>micro-shopartifactId>
        <groupId>com.atguigugroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>user-serviceartifactId>
    
    <packaging>warpackaging>

    <properties>
        <maven.compiler.source>17maven.compiler.source>
        <maven.compiler.target>17maven.compiler.target>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    properties>

    <dependencies>
        
        <dependency>
            <groupId>com.atguigugroupId>
            <artifactId>common-serviceartifactId>
            <version>1.0-SNAPSHOTversion>
        dependency>
    dependencies>

project>

依赖传递结果:
Maven 综合案例_第7张图片

④ 订单模块 (order-service)

创建工程,并使用插件转为web工程:
Maven 综合案例_第8张图片

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">
    <parent>
        <artifactId>micro-shopartifactId>
        <groupId>com.atguigugroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>order-serviceartifactId>
    
    <packaging>warpackaging>

    <properties>
        <maven.compiler.source>17maven.compiler.source>
        <maven.compiler.target>17maven.compiler.target>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    properties>

    <dependencies>
        
        <dependency>
            <groupId>com.atguigugroupId>
            <artifactId>common-serviceartifactId>
            <version>1.0-SNAPSHOTversion>
        dependency>
    dependencies>

project>

此时,查看父工程的pom.xml,会发现其中已经自动聚合了子工程:

<modules>
    <module>common-servicemodule>
    <module>user-servicemodule>
    <module>order-servicemodule>
modules>

你可能感兴趣的:(JAVA进阶之路,maven,数据库,java)