好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Git和Maven的子模块简单实践

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

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

创建子模块项目

打开 git bash,创建一个空目录并进入:

?

1

2

$ mkdir erp-submodules

$ cd erp-submodules/

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

?

1

$ git init

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

$ 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,内容如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

<?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 服务器。

?

1

2

3

4

5

6

7

8

# 添加所有

$ git add -all

# 提交

$ git commit -m 'first commit'

# 添加远程仓库地址

$ git remote add origin 创建好的仓库地址

# 推送

$ git push origin master

检出导入项目

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

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

$ 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.

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

?

1

2

3

4

# 进入 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

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接

原文链接:https://blog.csdn.net/isea533/article/details/85065086

查看更多关于Git和Maven的子模块简单实践的详细内容...

  阅读:15次