Git 和 Maven 的子模块简单实践

栏目: Java · 发布时间: 7年前

内容简介:版权声明:版权归博主所有,转载请带上本文链接!联系方式:abel533@gmail.com https://blog.csdn.net/isea533/article/details/85065086

版权声明:版权归博主所有,转载请带上本文链接!联系方式:abel533@gmail.com https://blog.csdn.net/isea533/article/details/85065086

当一个产品或者项目由大量独立模块组成时,想要从 Git 挨个下载下来导入 IDE 查看并不容易,此时可以结合使用 Git 和 Maven 的子模块来处理这种场景。

通过 Git 子模块可以自动批量下载所有关联的项目,通过 Maven 子模块可以批量导入到 IDE 中,结合这两者可以很容易的管理和查看项目。

创建子模块项目

打开 Git Bash,创建一个空目录并进入:

$ mkdir erp-submodules
$ cd erp-submodules/

把当前目录初始化为 Git 仓库

$ git init

添加所有子模块( 可以一次输入多行命令,注意看最后一行命令是否执行 ):

$ git submodule -b master add http://IP/auto-erp/purchase.git
git submodule -b master add http://IP/auto-erp/checkup.git
git submodule -b master add http://IP/auto-erp/task.git
git submodule -b master add http://IP/auto-erp/sale.git
Cloning into 'purchase'...
remote: Counting objects: 5151, done.
remote: Compressing objects: 100% (86/86), done.
remote: Total 5151 (delta 49), reused 108 (delta 30)
Receiving objects: 100% (5151/5151), 1.12 MiB | 0 bytes/s, done.
Resolving deltas: 100% (2269/2269), done.
Checking connectivity... done.
warning: LF will be replaced by CRLF in .gitmodules.
The file will have its original line endings in your working directory.

等待所有项目下载完成。

此时就创建了所有的子项目,为了方便以 MAVEN 方式导入全部项目,使用子模块配置。

在当前项目下面添加 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.github.abel533</groupId>
    <artifactId>erp-modules</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <modules>
		<!-- 所有子模块 -->
        <module>purchase</module>
		<module>barch</module>
		<module>checkup</module>
		<module>task</module>
		<module>sale</module>
		<module>packing</module>
		<module>logistics</module>
    </modules>
</project>

此时项目已完成,提交本地更改并上传到 git 服务器。

# 添加所有
$ git add -all
# 提交
$ git commit -m 'first commit'
# 添加远程仓库地址
$ git remote add origin 创建好的仓库地址
# 推送
$ git push origin master

检出导入项目

刚刚按照上面步骤操作后,本地是可以用了,但是如果其他成员想下载,就需要 检出

在要检出的目录中,打开 git bash,输入下面的命令检出项目:

$ git clone --recursive 仓库地址
# 以下为部分输出日志
Cloning into 'erp-modules'...
remote: Counting objects: 6, done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 6 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (6/6), done.
Checking connectivity... done.
Submodule 'ERPcrm' (http://IP/auto-erp/ERPcrm.git) registered for path 'ERPcrm'
Submodule 'accountNew' (http://IP/auto-erp/accountNew.git) registered for path 'accountNew'
Submodule 'barch' (http://IP/auto-erp/barch.git) registered for path 'barch'
Submodule 'checkup' (http://IP/auto-erp/checkup.git) registered for path 'checkup'
Submodule 'contract' (http://IP/auto-erp/contract.git) registered for path 'contract'
Cloning into 'ERPcrm'...
remote: Counting objects: 1651, done.
remote: Compressing objects: 100% (274/274), done.
remote: Total 1651 (delta 139), reused 447 (delta 70)
Receiving objects: 100% (1651/1651), 265.91 KiB | 0 bytes/s, done.
Resolving deltas: 100% (494/494), done.
Checking connectivity... done.
Submodule path 'ERPcrm': checked out '26686570bc1f22627f717830599ac77248014b87'
Cloning into 'accountNew'...
remote: Counting objects: 1850, done.
remote: Compressing objects: 100% (689/689), done.
otal 1850 (delta 866), reused 1624 (delta 664)
Receiving objects: 100% (1850/1850), 496.70 KiB | 0 bytes/s, done.
Resolving deltas: 100% (866/866), done.
Checking connectivity... done.

此时所有子模块都自动下载了,但是所有子模块都没有选择分支,如果不选择分支会导致项目混乱,所以下面切换分支,并且更新。

# 进入 clone 下来的目录
$ cd erp-modules/

# 执行下面的命令 git submodule foreach <命令>
$ git submodule foreach git checkout master && git pull origin master

所有子模块都切换到了 master 分支并且进行了更新。可以将项目导入 IDE 了。

在后续使用的时候,要随时注意子模块的分支,防止意外导致的错误。

利用 git submodule foreach <命令> 可以很方便的对子模块批量执行 命令

删除 Git 子模块比较麻烦,可以参考下面地址:

https://gist.github.com/myusuf3/7f645819ded92bda6677

以上所述就是小编给大家介绍的《Git 和 Maven 的子模块简单实践》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

A Philosophy of Software Design

A Philosophy of Software Design

John Ousterhout / Yaknyam Press / 2018-4-6 / GBP 14.21

This book addresses the topic of software design: how to decompose complex software systems into modules (such as classes and methods) that can be implemented relatively independently. The book first ......一起来看看 《A Philosophy of Software Design》 这本书的介绍吧!

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试