zoukankan      html  css  js  c++  java
  • 如何使用GitHub创建Maven私有仓库

    Github上创建仓库】

    首先,在GitHub上创建自己的仓库(mvn-repo):

    clip_image001[6]

     

    【配置本地setting文件】

    找到本地的maven settings文件,配置server

    clip_image002[6]

    有两种选择,可以选择配置usernamepassword,或者选择配置Personal access tokens<OAUTH2TOKEN>(也是填充到password字段)。

    优先使用Personal access tokens,因为有些公司内部可能会对用户名和密码做限制(我也不知道为什么)。

    前者即是GitHub的用户名以及密码,后者需要在GitHub上进行申请,步骤如下:

    clip_image003[6]

     

    clip_image004[6]

     

    clip_image005[6]

     

    选择对应的权限,并标注Note

    clip_image006[6]

    然后点击查看:

    clip_image007[6]

     

    clip_image008[6]

    上图红框即是你的personal access token。

     

     注:token会不断发生变化,每一次查看都会更新,更新后之前的不可用,所以要妥善保存。

     

    【增加本地临时存储库】

    pom文件中增加

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-deploy-plugin</artifactId>
                <version>2.8.2</version>
                <configuration>
                    <!-- altDeploymentRepository :指定替代方案应该部署项目工件的存储库(除了指定的工件)。 -->
                    <altDeploymentRepository>internal.repo::default::file://${project.build.directory}/mvn-repo
                    </altDeploymentRepository>
                </configuration>
            </plugin>
        </plugins>
    </build>

     

    然后执行mvn clean deploy

    clip_image009[6]

    clip_image010[6]

    如下图,版本已经正确地发布到本地指定的存储库中了。

    clip_image011[6]

     

    【配置远程的github服务

    pom文件中增加以下几行:

    <properties>
        <github.global.server>github</github.global.server>
    </properties>

     

    【发布到远程的github指定的仓库

    pom文件中配置以下几行:

     

    <!--源码-->
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-source-plugin</artifactId>
        <version>3.2.1</version>
        <executions>
            <execution>
                <id>attach-sources</id>
                <goals>
                    <goal>jar-no-fork</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    
    <!--github上传插件,用于修改后的发布,执行 mvn clean deploy 自动打包上传到github-->
    <plugin>
        <groupId>com.github.github</groupId>
        <artifactId>site-maven-plugin</artifactId>
        <version>0.12</version>
        <configuration>
            <message>Creating site for ${project.artifactId} ${project.version}</message>
            <noJekyll>true</noJekyll>
            <!--本地jar地址, 对应上面的altDeploymentRepository-->
            <outputDirectory>${project.build.directory}/mvn-repo</outputDirectory>
            <!--分支-->
            <branch>refs/heads/master</branch>
            <merge>true</merge>
    
            <includes>
                <include>**/*</include>
            </includes>
            <!--对应github上创建的仓库名称 name-->
            <repositoryName>mvn-repo</repositoryName>
            <!--github登录账号 对应的密码存在maven的setting.xml文件中-->
            <!--由github组织拥有,则该值将是组织名称,如果由用户拥有,则该值将是用户名-->
            <repositoryOwner>liufarui</repositoryOwner>
        </configuration>
    
        <executions>
            <execution>
                <goals>
                    <goal>site</goal>
                </goals>
                <phase>deploy</phase>
            </execution>
        </executions>
    </plugin>

     

    再次执行mvn clean deploy命令即可发布到GitHubmaven仓库中。

    clip_image012[6]

    clip_image013[6]

    如上图即为成功;

     

    我们可以查看我们的mvn-repo项目,发现内容已经发生了变化:

    clip_image014[6]

     

    clip_image015[6]

     

    【使用依赖包】

    在新项目的pom文件中增加以下行:

    <repositories>
        <repository>
            <id>mvn-repo</id>
            <!-- https://raw.github.com/用户名/仓库名/分支名 -->
            <url>https://raw.github.com/liufarui/mvn-repo/master</url>
            <snapshots>
                <enabled>true</enabled>
                <updatePolicy>always</updatePolicy>
            </snapshots>
        </repository>
    </repositories>

     

    在新项目的pom文件中增加依赖:

    <dependencies>
        <dependency>
            <groupId>com.github.liufarui</groupId>
            <artifactId>demo-maven-github-repo</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

     

    由于我们之前开发的这个是个maven插件工程,所以增加以下行进行使用:

    <build>
        <plugins>
            <plugin>
                <groupId>com.github.liufarui</groupId>
                <artifactId>demo-maven-github-repo</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </plugin>
        </plugins>
    </build>

     

    我们现在可以在右侧Maven中看到我们的插件

    clip_image016[6]

     

    执行看结果:

    clip_image017[6]

     

    demo地址】

    以上,即是整个Github创建maven仓库的内容,为了方便大家查看学习,我把demo项目放到了我的github上,大家可以自行查看,有问题也可以在评论区随时讨论:

    clip_image018[6]

    https://github.com/liufarui/code-demo

     

    【问题】

    [ERROR] Failed to execute goal com.github.github:site-maven-plugin:0.12:site (default) on project path: Error creating commit: Invalid request.

    遇到以上问题是因为没有设置姓名,在setting中进行设置:

    clip_image019[6]

     

    Error creating blob: cannot retry due to server authentication, in streaming mode

    很多人都遇到了此问题,此问题一般是权限问题,有可能是用户名密码不对,也有可能是公司网络做了特殊的限制,还有一些奇怪的原因,一般可以通过配置Personal access tokens去解决,附上网上的讨论链接:

    https://github.com/github/maven-plugins/issues/36

     

     以上,是如何搭建私有的maven仓库,这个仓库必须是有个人账户认证才可以使用的,无法确保大家可以一起用,之后,会再写一篇如何搭建public仓库的博客。

  • 相关阅读:
    visual basic VB.NET实例系列教程第三节(常用循环结构之乘法表与数列)
    visual basic VB.NET实例系列教程第二节(好玩又有趣的龟兔赛跑程序)
    visual basic VB.NET实例系列教程第一节(简单实用抽奖程序)
    对DevSecOps的一点认识
    轻便的一句话反弹shell语句
    ActiveMQ 反序列化漏洞(CVE-2015-5254)复现
    《信息安全风险管理》梗概
    微信服务商分账功能开发(PHP)
    哈夫曼编码—文件的压缩与解压(Java)
    哈夫曼编码—数据压缩与解压(Java)
  • 原文地址:https://www.cnblogs.com/liufarui/p/14019206.html
Copyright © 2011-2022 走看看