您的位置 首页 java

用GitHub创建自己的Maven私有仓库

【Github上创建仓库】

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

用GitHub创建自己的Maven私有仓库

【配置本地setting文件】

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

用GitHub创建自己的Maven私有仓库

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

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

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

用GitHub创建自己的Maven私有仓库

用GitHub创建自己的Maven私有仓库

用GitHub创建自己的Maven私有仓库

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

用GitHub创建自己的Maven私有仓库

然后点击查看:

用GitHub创建自己的Maven私有仓库

用GitHub创建自己的Maven私有仓库

上图红框即是你的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。

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

【配置远程的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命令即可发布到GitHub的maven仓库中。

如上图即为成功;

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

【使用依赖包】

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

 <repositories>
    <repository>
        <id>mvn-repo</id>
        <!-- 用户名/仓库名/分支名 -->
        <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中看到我们的插件

用GitHub创建自己的Maven私有仓库

执行看结果:

用GitHub创建自己的Maven私有仓库

【demo地址】

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

用GitHub创建自己的Maven私有仓库

【问题】

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

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

用GitHub创建自己的Maven私有仓库

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

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

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

原文链接:

如果觉得本文对你有帮助,可以转发关注支持一下

文章来源:智云一二三科技

文章标题:用GitHub创建自己的Maven私有仓库

文章地址:https://www.zhihuclub.com/176734.shtml

关于作者: 智云科技

热门文章

网站地图