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仓库的博客。

  • 相关阅读:
    Elasticsearch Query DSL 整理总结(三)—— Match Phrase Query 和 Match Phrase Prefix Query
    Elasticsearch Query DSL 整理总结(二)—— 要搞懂 Match Query,看这篇就够了
    Elasticsearch Query DSL 整理总结(一)—— Query DSL 概要,MatchAllQuery,全文查询简述
    Elasticsearch Java Rest Client API 整理总结 (三)——Building Queries
    Elasticsearch date 类型详解
    python 历险记(五)— python 中的模块
    python 历险记(四)— python 中常用的 json 操作
    python 历险记(三)— python 的常用文件操作
    Elasticsearch Java Rest Client API 整理总结 (二) —— SearchAPI
    Elasticsearch Java Rest Client API 整理总结 (一)——Document API
  • 原文地址:https://www.cnblogs.com/liufarui/p/14019206.html
Copyright © 2011-2022 走看看