0
点赞
收藏
分享

微信扫一扫

Maven学习笔记 标签以及多模块

半秋L 2022-05-01 阅读 42

文章目录

前言

本篇博客是介绍Maven的标签以及多模块,若文章中出现相关问题,请指出!

所有博客文件目录索引:博客目录索引(持续更新)

Maven相关资源

Maven中央仓库

Maven搜索网页

一、Maven插件

1.1、jdk1.8

编译插件jdk1.8

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
    </plugins>
</build>


二、pom.xml中的标签

pom.xml中<scope>

关于maven依赖中的scope的作用和用法:https://blog.csdn.net/qq_36874292/article/details/81072717

compile(默认):compile表示被依赖项目需要参与当前项目的编译,当然后续的测试,运行周期也参与其中,是一个比较强的依赖。打包的时候通常需要包含进去。默认的scope,在部署的时候将会打包到lib目录下,项目在编译测试运行阶段都需要

test:表示依赖项目仅仅参与测试相关的工作,在编译和运行环境下都不会被使用,更别说打包了

provided:适合在编译和测试的环境,他和compile很接近,但是provide仅仅需要在编译和测试阶段,同样provide将不会被打包到lib目录下。



Pom.xml中相关标签

<!-- 使用分发管理将本项目打成jar包,直接上传到指定服务器 -->
<distributionManagement>
    <!--正式版本-->
    <repository>
        <!-- nexus服务器中用户名:在settings.xml中<server>的id-->
        <id>yang</id>
        <!-- 这个名称自己定义 -->
        <name>Release repository</name>
        <url>http://192.168.1.105:8081/repository/yang/</url>
    </repository>
    <!--快照
    <snapshotRepository>
        <id>nexus-snapshots</id>
        <name>Snapshots repository</name>
        <url>http://192.168.1.105/repository/yang/</url>
    </snapshotRepository>-->
</distributionManagement>

< repository >节点下的< id >对应setting.xml文件中的server的id

在settings.xml中设置对应的登录用户名与密码:

<!--maven连接nexus需要验证用户名和密码-->
<server>
    <id>yang</id>
    <username>admin</username>
    <password>admin123</password>
</server>

注意点:上传正式版本,pom.xml文件version中不能有SNAPSHOT,快照版本才有。

如:

<groupId>com.yang</groupId>
<artifactId>shade-plugin</artifactId>
<version>0.0.1</version>


三、Maven多模块开发

3.1、Pom.xml多模块结构

image-20210727152959942

Maven配置文件:

<?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>
    
    <!-- 设置父模块   -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.10.RELEASE</version>
        <relativePath />
    </parent>

    <!-- 定义本项目的工程坐标   -->
    <groupId>com.changlu</groupId>
    <artifactId>springboot-mall</artifactId>
    <version>1.0.0</version>
    <!-- 工程中仅有一个pom.xml时需要声明,表示其是一个管理包   -->
    <packaging>pom</packaging>

    <!-- 声明子模块   -->
    <modules>
        <module>mall-admin</module>
        <module>mall-common</module>
        <module>mall-controller</module>
        <module>mall-service</module>
        <module>mall-admin</module>
    </modules>
    
    <!-- 定义版本   -->
    <properties>
        <java.version>1.8</java.version>
        <mybatis-plus.version>3.4.0</mybatis-plus.version>
    </properties>
    
    <!-- 设置依赖   -->
    <dependencies>
		<dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>${mybatis-plus.version}</version>
        </dependency>
    </dependencies>
</project>

modules:管理模块。

parent:定义父模块。

properties:定义属性名,用于进行版本管理。



3.2、聚合与继承的关系

1、聚合主要是为了方便快速构建项目,继承主要是为了消除重复配置

2、对于聚合模块而言,它知道有哪些被聚合的模块,但那些被聚合的模块不知道这个聚合模块的存在;对于继承的父pom 而言,它不知道有哪些子模块继承它,但那些子模块都必须知道自己的父pom 是什么

3、聚合pom 与继承中的父pom 的 packaging 都必须是 pom;同时,聚合模块与继承中的父模块除了 pom 外,都没有实际的内容。



3.3、多模块实操

3.3.1、pom.xml介绍

在子模块中最好都不要出现版本,统一由父工程来进行管理版本。

image-20210802212806313

其中oa-weboa-admin为web工程,其他都是可以进行复用的公共模块。

  • 对于公共模块,直接新建普通的maven工程,选择moduleexer作为父工程即可!

moduleexer为父模块,用于统一管理内部的模块,首先介绍moduleexer工程中的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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <!--  模块管理  -->
    <modules>
        <module>oa-common</module>
        <module>oa-dao</module>
        <module>oa-common</module>
        <module>oa-service</module>
        <module>oa-web</module>
        <module>oa-admin</module>
        <module>oa-model</module>
    </modules>

    <!-- 将其作为父模块  -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.changlu</groupId>
    <artifactId>moduleexer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <!-- 聚合模块父模块务必设置packaging为pom,默认jar会打成一个jar包  -->
    <packaging>pom</packaging>

    <!--  自定义属性,统一进行版本管理   -->
    <properties>
        <oa-version>0.0.1-SNAPSHOT</oa-version>
        <mybatis-spring-starter-version>2.1.2</mybatis-spring-starter-version>
        <mysql-connector-version>8.0.23</mysql-connector-version>
    </properties>


    <!--  父工程统一进行版本管理   -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.changlu</groupId>
                <artifactId>oa-model</artifactId>
                <version>${oa-version}</version>
            </dependency>
            <dependency>
                <groupId>com.changlu</groupId>
                <artifactId>oa-dao</artifactId>
                <version>${oa-version}</version>
            </dependency>
            <dependency>
                <groupId>com.changlu</groupId>
                <artifactId>oa-service</artifactId>
                <version>${oa-version}</version>
            </dependency>
            <dependency>
                <groupId>com.changlu</groupId>
                <artifactId>oa-common</artifactId>
                <version>${oa-version}</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>${mybatis-spring-starter-version}</version>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql-connector-version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

外部父模块中不需要引入依赖,可以进行依赖版本的管理,其子模块在添加依赖时只要是父模块管理的就可以不用写版本,由父模块来同一进行控制!

接下来我们啦看看对应的公共模块各自的pom.xml:

oa-dao:用于编写公共dao,可以看到其父模块为moduleexer,引入的依赖并没有进行设置版本,由父模块来进行管理!

  • 引入公共模块:oa-commonoa-model
<?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">
    <parent>
        <artifactId>moduleexer</artifactId>
        <groupId>com.changlu</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>oa-dao</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.changlu</groupId>
            <artifactId>oa-model</artifactId>
        </dependency>
        <dependency>
            <groupId>com.changlu</groupId>
            <artifactId>oa-common</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*/xml</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
    </build>

</project>

oa-model:在模型层,我们仅仅只需要引入指定的插件依赖即可

<?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">
    <parent>
        <artifactId>moduleexer</artifactId>
        <groupId>com.changlu</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>oa-model</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

</project>

oa-service:用于编写service层

  • 引入公共模块:oa-dao,由于oa-dao已经引入oa-model,所以不需要重复引入。
...
	<dependencies>
        <dependency>
            <groupId>com.changlu</groupId>
            <artifactId>oa-dao</artifactId>
        </dependency>
    </dependencies>

...

接下来就是web模块了:oa-web,在这里我们就需要引入spring-boot-starter-web模块了

  • 引入公共模块:oa-service,只需要引入其一个模块即可,其他模块也都被间接引入了!
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">
    <parent>
        <artifactId>moduleexer</artifactId>
        <groupId>com.changlu</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>oa-web</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <!--    引入web模块   -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.changlu</groupId>
            <artifactId>oa-service</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <!--用于明确表示编译版本配置有效-->
                    <fork>true</fork>
                </configuration>
            </plugin>
        </plugins>
    </build>


</project>

此时我们就已经把大致内容介绍完了,对于oa-commonoa-admin(后台web模块)就不再展示了。



3.3.2、多模块运行测试

sql语句:创建与插入用户表

DROP TABLE IF EXISTS `user`;
CREATE TABLE `user`  (
  `id` int(11) NOT NULL COMMENT 'id',
  `username` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '用户名',
  `password` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '密码',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES (1, 'changlu', '123456');
INSERT INTO `user` VALUES (2, 'liner', '456789');

image-20210802214425625

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {

    private Integer id;
    private String username;
    private String password;
}

image-20210802214502407

//UserMapper
@Mapper
public interface UserMapper {

    /**
     * 查询所有用户
     * @return 用户集合
     */
    List<User> queryUserList();
}

//UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<!-- 设置指定的namespace -->
<mapper namespace="com.changlu.mapper.UserMapper">

    <!--  根据username获取User对象  -->
    <select id="queryUserList" resultType="User">
        select * from user
    </select>

</mapper>

image-20210802214607383

public interface IUserService {

    List<User> queryUserList();
}

@Service
public class UserServiceImpl implements IUserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public List<User> queryUserList() {
        return userMapper.queryUserList();
    }
}

image-20210802214704452

@RestController
public class UserController {

    @Autowired
    private IUserService userService;

    @GetMapping("/user")
    public  List<User> userList(){
        return  userService.queryUserList();
    }
}
server:
  port: 8088

# 配置mybatis数据源
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/test?setTimezone=UTC&useUnicode=true&characterEncoding=utf-8
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver

# 整合mybatis,实体类包配置和mapper映射地址
mybatis:
  type-aliases-package: com.changlu.entity
  mapper-locations: classpath:mapper/*.xml

此时我们来启动springboot,即oa-web工程,接着使用postman来进行测试:

image-20210802215227462

测试成功。总结一下吧,使用maven多模块能够更加方便的进行模块复用与解耦,例如管理后台,前台以及app这类web工程就能够很好的复用model以及common模块了,并且我们也能够更好的进行版本的管理!



四、deploy module


参考文章

[1]. 十八、在pom.xml中使用distributionManagement将项目打包上传到nexus私服

[2]. Maven pom.xml中的元素modules、parent、properties以及import

举报

相关推荐

0 条评论