美文网首页Java
将 github 用作 maven 仓库来发布自己的库

将 github 用作 maven 仓库来发布自己的库

作者: 何晓杰Dev | 来源:发表于2020-06-14 12:49 被阅读0次

不知想这么做的人有多少呢,反正我个人是不太乐意租用服务器来搞 maven 的,所以 github 就是一个很好的选择。

先前查找了不少文章,感觉天下文章一大抄,都是抄来抄去的,也没有很明白的说明倒底该怎么用,所以才有了这一篇,我尽可能的把要讲的事情讲清楚吧。


首先要在 github 上新建一个仓库,我新建了一个叫 repository 的仓库,一看就知道是用来干什么的了,然后把它 clone 到本地:

$ git clone git@github.com:rarnu/repository.git

这里有个小坑,尽量不要选择 https 的方式来 clone,而是应该选择 ssh 方式,否则当你要上传一个比较大的 jar 包时,就知道坑在哪了。

然后拿出我们的项目,同样的我以自己的 kotlin-monarch 项,目为例,尝试发布其中的 common-jvm 模块。

由于之前用的是 maven-publish 这个插件,所以可以很轻易的将上传的 url 以及认证方式改了,原代码为:

publishing {
    publications {
        maven(MavenPublication) {
            from components.java
            artifact sourceJar
        }
    }
    repositories {
        maven {
            url nexusUploadUrl
            credentials {
                username nexusAccount
                password nexusPassword
            }
        }
    }
}

修改后为:

publishing {
    publications {
        maven(MavenPublication) {
            from components.java
            artifact sourceJar
        }
    }
    repositories {
        maven {
            url nexusUploadUrl
        }
    }
}

很简单的,把认证的部分去掉,然后再改一下 nexusUploadUrl

nexusUploadUrl=file:///Users/rarnu/Develop/repository

这就是将上传的 url 改为了刚才 clone 下来的目录,让 gradle 把项目发布到这个路径即可。

完成后执行 gradle publish,即可在 repository 目录下找到如下结构的文件:

此时只需要将项目的改动提交就可以了,执行 git push 后,你会发现在 github 仓库内已经有了这些文件。


那接下来就是如何使用的问题了,其实也很简单的,只需要在要用的项目里,加入以下代码就好:

repositories {
    ... ...
    maven { url 'https://raw.githubusercontent.com/rarnu/repository/master' }
}

dependencies {
    ... ...
    implementation "com.rarnu:common-jvm:1.0.0"
}

如果是用的 maven 而不是 gradle,会稍麻烦一些,改一下 settings.xml:

<settings
    xmlns="http://maven.apache.org/SETTINGS/1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
    ... ...
    <profiles>
        <profile>
            <id>rarnu</id>
            <repositories>
                <repository>
                    <id>rarnu</id>
                    <url>https://raw.githubusercontent.com/rarnu/repository/master/</url>
                    <snapshots><enabled>true</enabled></snapshots>
                    <releases><enabled>true</enabled></releases>
                </repository>
            </repositories>
            <pluginRepositories>
                <pluginRepository>
                    <id>rarnu</id>
                    <url>https://raw.githubusercontent.com/rarnu/repository/master/</url>
                    <snapshots><enabled>true</enabled></snapshots>
                    <releases><enabled>true</enabled></releases>
                </pluginRepository>
            </pluginRepositories>
        </profile>
    </profiles>
    <activeProfiles>
        <activeProfile>rarnu</activeProfile>
    </activeProfiles>
</settings>

这样就可以在 maven 管理的项目里也拿到 github 上发布的库了。


这下终于可以把租来搞 maven 的服务器退掉了吧 :)

相关文章

网友评论

    本文标题:将 github 用作 maven 仓库来发布自己的库

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