2020年6月19日 | Leave a comment 原文: https://www.jianshu.com/p/a64fb4322c55 不知想这么做的人有多少呢,反正我个人是不太乐意租用服务器来搞 maven 的,所以 github 就是一个很好的选择。 先前查找了不少文章,感觉天下文章一大抄,都是抄来抄去的,也没有很明白的说明倒底该怎么用,所以才有了这一篇,我尽可能的把要讲的事情讲清楚吧。 首先要在 github 上新建一个仓库,我新建了一个叫 repository 的仓库,一看就知道是用来干什么的了,然后把它 clone 到本地: $ git clone git@github.com:rarnu/repository.git 12 $ 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 } } } } 123456789101112131415161718 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 } } } 1234567891011121314 publishing { publications { maven(MavenPublication) { from components.java artifact sourceJar } } repositories { maven { url nexusUploadUrl } }} 很简单的,把认证的部分去掉,然后再改一下 nexusUploadUrl: nexusUploadUrl<span class="token operator">=</span>file<span class="token operator">:</span><span class="token operator">/</span><span class="token comment">//Users/rarnu/Develop/repository</span> 12 nexusUploadUrl<span class="token operator">=</span>file<span class="token operator">:</span><span class="token operator">/</span><span class="token comment">//Users/rarnu/Develop/repository</span> 这就是将上传的 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" } 12345678910 repositories { ... ... maven { url 'https://raw.githubusercontent.com/rarnu/repository/master' }} dependencies { ... ... implementation "com.rarnu:common-jvm:1.0.0"} 如果是用的 maven 而不是 gradle,会稍麻烦一些,改一下 settings.xml: <span class="token tag"><span class="token punctuation"><</span>settings <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://maven.apache.org/SETTINGS/1.0.0<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">xmlns:</span>xsi</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://www.w3.org/2001/XMLSchema-instance<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">xsi:</span>schemaLocation</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> ... ... <span class="token tag"><span class="token punctuation"><</span>profiles<span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"><</span>profile<span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"><</span>id<span class="token punctuation">></span></span>rarnu<span class="token tag"><span class="token punctuation"></</span>id<span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"><</span>repositories<span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"><</span>repository<span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"><</span>id<span class="token punctuation">></span></span>rarnu<span class="token tag"><span class="token punctuation"></</span>id<span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"><</span>url<span class="token punctuation">></span></span>https://raw.githubusercontent.com/rarnu/repository/master/<span class="token tag"><span class="token punctuation"></</span>url<span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"><</span>snapshots<span class="token punctuation">></span></span><span class="token tag"><span class="token punctuation"><</span>enabled<span class="token punctuation">></span></span>true<span class="token tag"><span class="token punctuation"></</span>enabled<span class="token punctuation">></span></span><span class="token tag"><span class="token punctuation"></</span>snapshots<span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"><</span>releases<span class="token punctuation">></span></span><span class="token tag"><span class="token punctuation"><</span>enabled<span class="token punctuation">></span></span>true<span class="token tag"><span class="token punctuation"></</span>enabled<span class="token punctuation">></span></span><span class="token tag"><span class="token punctuation"></</span>releases<span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"></</span>repository<span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"></</span>repositories<span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"><</span>pluginRepositories<span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"><</span>pluginRepository<span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"><</span>id<span class="token punctuation">></span></span>rarnu<span class="token tag"><span class="token punctuation"></</span>id<span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"><</span>url<span class="token punctuation">></span></span>https://raw.githubusercontent.com/rarnu/repository/master/<span class="token tag"><span class="token punctuation"></</span>url<span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"><</span>snapshots<span class="token punctuation">></span></span><span class="token tag"><span class="token punctuation"><</span>enabled<span class="token punctuation">></span></span>true<span class="token tag"><span class="token punctuation"></</span>enabled<span class="token punctuation">></span></span><span class="token tag"><span class="token punctuation"></</span>snapshots<span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"><</span>releases<span class="token punctuation">></span></span><span class="token tag"><span class="token punctuation"><</span>enabled<span class="token punctuation">></span></span>true<span class="token tag"><span class="token punctuation"></</span>enabled<span class="token punctuation">></span></span><span class="token tag"><span class="token punctuation"></</span>releases<span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"></</span>pluginRepository<span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"></</span>pluginRepositories<span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"></</span>profile<span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"></</span>profiles<span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"><</span>activeProfiles<span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"><</span>activeProfile<span class="token punctuation">></span></span>rarnu<span class="token tag"><span class="token punctuation"></</span>activeProfile<span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"></</span>activeProfiles<span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"></</span>settings<span class="token punctuation">></span></span> 12345678910111213141516171819202122232425262728293031 <span class="token tag"><span class="token punctuation"><</span>settings <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://maven.apache.org/SETTINGS/1.0.0<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">xmlns:</span>xsi</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://www.w3.org/2001/XMLSchema-instance<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">xsi:</span>schemaLocation</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> ... ... <span class="token tag"><span class="token punctuation"><</span>profiles<span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"><</span>profile<span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"><</span>id<span class="token punctuation">></span></span>rarnu<span class="token tag"><span class="token punctuation"></</span>id<span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"><</span>repositories<span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"><</span>repository<span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"><</span>id<span class="token punctuation">></span></span>rarnu<span class="token tag"><span class="token punctuation"></</span>id<span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"><</span>url<span class="token punctuation">></span></span>https://raw.githubusercontent.com/rarnu/repository/master/<span class="token tag"><span class="token punctuation"></</span>url<span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"><</span>snapshots<span class="token punctuation">></span></span><span class="token tag"><span class="token punctuation"><</span>enabled<span class="token punctuation">></span></span>true<span class="token tag"><span class="token punctuation"></</span>enabled<span class="token punctuation">></span></span><span class="token tag"><span class="token punctuation"></</span>snapshots<span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"><</span>releases<span class="token punctuation">></span></span><span class="token tag"><span class="token punctuation"><</span>enabled<span class="token punctuation">></span></span>true<span class="token tag"><span class="token punctuation"></</span>enabled<span class="token punctuation">></span></span><span class="token tag"><span class="token punctuation"></</span>releases<span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"></</span>repository<span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"></</span>repositories<span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"><</span>pluginRepositories<span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"><</span>pluginRepository<span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"><</span>id<span class="token punctuation">></span></span>rarnu<span class="token tag"><span class="token punctuation"></</span>id<span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"><</span>url<span class="token punctuation">></span></span>https://raw.githubusercontent.com/rarnu/repository/master/<span class="token tag"><span class="token punctuation"></</span>url<span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"><</span>snapshots<span class="token punctuation">></span></span><span class="token tag"><span class="token punctuation"><</span>enabled<span class="token punctuation">></span></span>true<span class="token tag"><span class="token punctuation"></</span>enabled<span class="token punctuation">></span></span><span class="token tag"><span class="token punctuation"></</span>snapshots<span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"><</span>releases<span class="token punctuation">></span></span><span class="token tag"><span class="token punctuation"><</span>enabled<span class="token punctuation">></span></span>true<span class="token tag"><span class="token punctuation"></</span>enabled<span class="token punctuation">></span></span><span class="token tag"><span class="token punctuation"></</span>releases<span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"></</span>pluginRepository<span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"></</span>pluginRepositories<span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"></</span>profile<span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"></</span>profiles<span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"><</span>activeProfiles<span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"><</span>activeProfile<span class="token punctuation">></span></span>rarnu<span class="token tag"><span class="token punctuation"></</span>activeProfile<span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"></</span>activeProfiles<span class="token punctuation">></span></span><span class="token tag"><span class="token punctuation"></</span>settings<span class="token punctuation">></span></span> 这样就可以在 maven 管理的项目里也拿到 github 上发布的库了。 这下终于可以把租来搞 maven 的服务器退掉了吧 🙂 作者:何晓杰Dev 链接:https://www.jianshu.com/p/a64fb4322c55 来源:简书 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。