美文网首页
Git仓库拆分迁移

Git仓库拆分迁移

作者: 蜀山_竹君子 | 来源:发表于2020-08-23 13:18 被阅读0次

背景

项目是Spring Cloud 架构,搭建初始是所有应用在一个Git仓库 Group内,随着应用逐渐增加,组件之间依赖管理复杂,构建应用时需要全量构建所有公共组件,导致应用镜像较大,同时也不利于项目版本管理,因为团队觉得将项目按应用和组件拆分为多个独立Git仓库,同时公共组件包使用远程Maven私有仓库来统一进行管理。

1.  拆分项目时需要完整保留代码提交历史;
2.  公共组件由原有pom方式依赖切换为远程Maven私有仓库管理

Git仓库迁移

使用git push --mirror进行仓库迁移,完成迁移后删除多余代码实现应用拆分的目的。

# 克隆一份源项目裸版本库
git clone --bare source_project.git
# 克隆的裸版本库,镜像到目标仓库。
git push --mirror target_project.git

操作步骤

#step1 创建target_project
#step2 从原地址克隆一份裸版本库
git clone --bare source_project.git
# step3 以镜像推送的方式上传代码到 目标仓库服务器上。
cd source_project.git
git push --mirror target_project.git
# step 4 删除本地代码(可忽略)
cd ..
rm source_project.git
# step5 从新仓库克隆代码到本地
git clone  target_project.git
# step6 删除target应用多余代码并推送到远程仓库
cd target_project
删除多余代码
git add -A
git commit -m '代码仓库迁移拆分'
git push

这种方法迁移仓库保留了源版本仓库所有提交历史记录。

注意事项

  1. 迁移前需要创建目标仓库,且为空
  2. 需要事先去除仓库的protect权限,否则git push 会报错

公共组件以及Feign API包 统一管理

按照上述步骤完成了项目代码的拆分后,需要解决公共组件以及Feign API 包的依赖管理,建立顶层pom包,实现第三方组件依赖版本统一管理,将公共组件中dto拆分到每个应用的独立Feign API包中,实现依赖引入的最小闭环。

maven私有仓库nexus搭建及使用

环境信息

  1. 安装机器ubuntu16
  2. nexus 2.x
  3. maven 3.6.3 jdk 1.8

安装及运行nexus

  1. 打开/etc/profile,新增一行export RUN_AS_USER=root;
  2. 执行命令source /etc/profile,使刚才的配置生效;
  3. 从官网下载的是nexus-2.14.5-02-bundle.tar.gz,执行tar -zxvf nexus-2.14.5-02-bundle.tar.gz解压后,里面有两个目录:nexus-2.14.5-02和sonatype-work;
  4. 进入nexus-2.14.5-02/bin,执行./nexus start启动nexus;
  5. 进入nexus-2.14.5-02/logs,执行tail -f wrapper.log观察日志滚动信息,看到如下信息时nexus启动成功:

登录nexus

1. 在浏览器输入地址:[http://nexus远程机器ip:8081/nexus/]
2. 进入nexus首页后,点击右上角的"Log In"按钮,输入用户名"admin",默认密码"admin123";
3. 点击左侧的"Repositories"按钮,看到已有的仓库,如下图:

使用nexus

  1. 配置maven的信息,打开maven安装目录下的conf/settings.xml文件;
  2. 在mirrors节点下增加一个子节点
<mirror>     
    <id>bolingcavalry-nexus-releases</id>     
    <mirrorOf>*</mirrorOf>     
    <url>http://nexus远程机器ip:8081/nexus/content/groups/public</url>     
</mirror>    
<mirror>     
    <id>bolingcavalry-nexus-snapshots</id>     
    <mirrorOf>*</mirrorOf>     
    <url>http://nexus远程机器ip:8081/nexus/content/groups/public-snapshots</url>     
</mirror>
  1. 在profiles节点增加子节点
<profile>
    <id>nexus</id>
    <repositories>
        <repository>  
              <id>bolingcavalry-nexus-releases</id>  
              <url>http://nexus-releases</url>  
              <releases><enabled>true</enabled></releases>  
              <snapshots><enabled>true</enabled></snapshots>
          </repository>
          <repository>  
            <id>bolingcavalry-nexus-snapshots</id>  
            <url>http://nexus-snapshots</url>  
            <releases><enabled>true</enabled></releases>  
             <snapshots><enabled>true</enabled></snapshots>  
            </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>  
              <id>bolingcavalry-nexus-releases</id>  
              <url>http://nexus-releases</url>  
              <releases><enabled>true</enabled></releases>  
              <snapshots><enabled>true</enabled></snapshots>  
            </pluginRepository>  
            <pluginRepository>  
              <id>bolingcavalry-nexus-snapshots</id>  
              <url>http://nexus-snapshots</url>  
              <releases><enabled>true</enabled></releases>  
              <snapshots><enabled>true</enabled></snapshots>  
              </pluginRepository>
    </pluginRepositories>
</profile>

以上新增了远程maven私有仓库中央仓库信息。

  1. 新增activeProfiles节点,该节点和profiles节点一个层次
<activeProfiles>
    <activeProfile>nexus</activeProfile>
</activeProfiles>
  1. 在servers节点新增子节点
<server>   
    <id>bolingcavalry-nexus-releases</id>   
    <username>deployment</username>   
    <password>admin123</password>   
</server>
<server>   
    <id>bolingcavalry-nexus-snapshots</id>   
    <username>deployment</username>   
    <password>admin123</password>   
</server>

以上完成了maven私库配置。

  1. 项目pom文件新增
<!--maven生成jar包和源码包 -->
<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>3.0.1</version>
                <!-- 绑定source插件到Maven的生命周期,并在生命周期后执行绑定的source的goal -->
                <executions>
                    <execution>
                        <!-- 绑定source插件到Maven的生命周期 -->
                        <phase>compile</phase>
                        <!--在生命周期后执行绑定的source插件的goals -->
                        <goals>
                            <goal>jar-no-fork</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skip>false</skip>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

<distributionManagement>
        <repository>
            <id>bolingcavalry-nexus-releases</id>
            <name>Nexus Release Repository</name>
            <url>http://nexus远程机器ip:8081/nexus/content/repositories/releases</url>
        </repository>
        <snapshotRepository>
            <id>bolingcavalry-nexus-snapshots</id>
            <name>Nexus Snapshot Repository</name>
           <url>http://nexus远程机器ip:8081/nexus/content/repositories/snapshots</url>
        </snapshotRepository>
    </distributionManagement>

repository和snapshotRepository定义了deploy的时候用到的服务器信息,id和我们前面在maven配置信息中增加的id一致,使应用支持使用maven上传包到服务器。

  1. 在pom文件所在文件执行命令,上传包到服务器
mvn clean deploy -u
  1. 修改应用引用公共组件依赖方式,由同一group下的pom依赖改为使用私有仓库依赖方式
<dependency>
    <groupId>com.xxxx</groupId>
    <artifactId>common-core</artifactId>
    <version>1.0-SNAPSHOT</version>
  </dependency>
  1. build及运行应用
    至此,Spring Cloud 项目仓库拆分迁移完成,实现应用及组件Git仓库独立管理。

相关文章

  • Git仓库拆分迁移

    背景 项目是Spring Cloud 架构,搭建初始是所有应用在一个Git仓库 Group内,随着应用逐渐增加,组...

  • git仓库迁移和更新远程仓库地址

    git仓库迁移和更新远程仓库地址 一、git仓库迁移 1,从原仓库clone或pull到本地仓库 git clon...

  • 迁移git仓库带来的权限问题--2019-08-22

    迁移git仓库带来的权限问题 今天迁移git仓库 第一步: 克隆旧仓库‘基本’版本 git clone --bar...

  • 使用 git push –mirror 迁移 Git 项目

    git仓库迁移的两种解决方案 Git仓库迁移而不丢失log的方法 要求能保留原先的commit记录,应该如何迁移呢...

  • git仓库的转移

    一、git仓库迁移 1,从原仓库clone或pull到本地仓库 git clone project_name ​【...

  • Git仓库迁移

    迁移Git仓库 使用场景:需要迁移某个git仓库到另一个不同的仓库 需要保留所有分支和历史提交 操作步骤 clon...

  • 迁移GIT仓库并带有历史提交记录

    迁移git仓库 开发在很多时候,会遇到一个问题。GIT仓库的管理,特别是仓库的迁移。我需要保留已有的历史记录,而不...

  • 2020-03-11 git项目迁移遇到的问题

    参考文章Git项目迁移做项目git的项目迁移,由于疏忽,把错误的代码迁移到当前git仓库了解决方法:1、重新找个目...

  • git仓库迁移

    1、从原地址克隆一份裸版本库,比如原本托管于github或oschina,或者是本地的私有仓库 $ git clo...

  • git仓库迁移

    1.上图是我在codingnet上新建的项目然后做的修改测试修改提交 2.下图是我在gitlab上新建的仓库地址项...

网友评论

      本文标题:Git仓库拆分迁移

      本文链接:https://www.haomeiwen.com/subject/atmejktx.html