android 上传nexus_Android Studio上使用Nexus搭建Maven私服并上传JCenter

一、Nexus的环境搭建

在网上找资料的时候,发现各版本的Nexus的文件结构差异很大,本人使用的是Nexus 3.4,是截止到2017年7月8日的最新版。

1.下载

NEXUS REPOSITORY MANAGER OSS [开源软件——免费]

NEXUS REPOSITORY MANAGER -FREE TRIAL [专业版本——收费]

所以我们选择OSS即可

nexus下载.png

选择相应的平台,我这里是Windows

nexus选择平台.png

2.配置

2.1 将下载的ZIP解压缩,得到两个文件夹nexus-3.4.0-02和sonatype-work,其中第一个文件夹根据下载的版本不同而不同。

2.2 将nexus-3.4.0-02目录下的bin添加到环境变量的Path中

nexus的bin目录.png

2.3 打开bin文件夹下的nexus.vmoptions,配置Dkaraf.data和Djava.io.tmpdir两项,图中E:/Program Files/Nexus是我的zip解压路径,注意斜杠方向。其他的配置项我还没有研究,可自行研究下。

nexus配置文件.png

另外在文件夹etc下还有一个配置文件nexus-default.properties,有需要的也可以自行修改nexus-context-path这个参数,这个是nexus启动时的根路径,默认是“/”。

nexus配置文件2.png

3.启动

打开CMD,接下来执行命令:nexus.exe /run。 如果出现以下界面,说明启动成功

nexus启动成功.png

启动成功后,访问:localhost:8081。

默认的用户名和密码:admin/admin123

nexus登录成功.png

component name的一些说明:

maven-central:maven中央库,默认从https://repo1.maven.org/maven2/拉取jar

maven-releases:私库发行版jar

maven-snapshots:私库快照(调试版本)jar

maven-public:仓库分组,把上面三个仓库组合在一起对外提供服务,在本地maven基础配置settings.xml中使用。

component type的一些说明:

hosted:类型的仓库,内部项目的发布仓库

releases:内部的模块中release模块的发布仓库

snapshots:发布内部的SNAPSHOT模块的仓库

3rd party:第三方依赖的仓库,这个数据通常是由内部人员自行下载之后发布上去

proxy:类型的仓库,从远程中央仓库中寻找数据的仓库

二、Android Studio 的Module上传到Maven私服

我要操作的项目有两个Module:

jbottomtabbar:功能Module,要上传到maven作为公共仓库的就是它

app:默认Module,用来调用jbottomtabbar

1.编写脚本

首先在目标module(我这里就是jbottomtabbar)目录下创建一个用于上传的gradle文件,我这里叫nexus-push.gradle。

apply plugin: 'maven'

task androidJavadocs(type: Javadoc) {

source = android.sourceSets.main.java.srcDirs

classpath += project.files(android.getBootClasspath().join(File.pathSeparator))

}

task androidSourcesJar(type: Jar) {

classifier = 'sources'

from android.sourceSets.main.java.srcDirs

}

artifacts {

archives androidSourcesJar

}

//任务名

uploadArchives {

repositories {

mavenDeployer {

//这里的url是nexus中maven-releases的路径,可以点击copy按钮查看复制

repository(url: "http://localhost:8081/repository/maven-releases/") {

// nexus账号的用户名和密码,我这里没用默认的admin

authentication(userName: "jcking", password: "jcking123")

}

// 下面这三项pom参数,在调用的时候是这个样子 : compile 'com.jcking.jbottomtabbar:jbottomtabbar:0.0.1'

// library的包名

pom.groupId = 'com.jcking.jbottomtabbar'

// library的项目名

pom.artifactId = 'jbottomtabbar'

// library的版本号

pom.version = '0.0.1'

pom.project {

licenses {

license {

name 'The Apache Software License, Version 2.0'

url 'http://www.apache.org/licenses/LICENSE-2.0.txt'

}

}

}

}

}

}

nexus仓库路径.png

然后在目标mudule的build.gradle上进行调用

apply from: './nexus-push.gradle'

2.上传项目

好了,脚本基本完成了,下面可以运行一下了。记得上面的任务名么?uploadArchives,当然你想自己重命名也是可以的。在Android Studio的Terminal面板执行如下命令:

// windows

gradlew uploadArchives

// macOS

./gradlew uploadArchives

上传成功你会看到类似:

maven私服上传成功.png

然后我们就可以在nexus看到我们上传的项目了

maven私服项目.png

3.调用依赖

既然已经上传到maven私库了,那么我们就可以在本地项目调用了。首先在项目根目录下的build.gradle文件添加仓库url地址。

allprojects {

repositories {

jcenter()

maven {

url "http://localhost:8081/repository/maven-releases/"

}

}

}

然后在需要的module(我这里是app)的build.gradle文件添加依赖

compile 'com.jcking.jbottomtabbar:jbottomtabbar:0.0.1'

三、将项目发布到JCenter

JCenter仓库是由bintray提供并维护,这个仓库是类似Maven中央仓库。只要你能连上Internet,你就可以通过Gradle或者Maven去下载仓库中的依赖包到你自己的项目中。JCenter现在是Android Studio中repositories的默认节点了,之前是Maven的,不过JCenter是兼容Maven的,所以放心使用。

1.申请Bintray账号

到Bintray官网去注册一个就好,也可以用github账号登录。注意,这里有大坑,Bintray注册有两种类型:

Sign up to a Free Trial(收费专业版,30天免费试用)

Sign up to an Open Source account(开源免费版)

通常用的都是免费版,当然如果有需求可以选第一种。当然我是用的第二种,第一种的坑我没有填上。具体请看这篇文章,我遇到了完全同样的坑,这篇文章帮我解决的,在此谢过。

bintray注册1.png

bintray注册2.png

2.编写脚本

首先在项目的build.gradle中增加bintray需要的依赖。

classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.3.1'

classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'

在目标module的目录下创建一个用于上传的gradle文件,我这里叫jcenter-push.gradle。

apply plugin: 'com.jfrog.bintray'

apply plugin: 'com.github.dcendents.android-maven'

def siteUrl = 'https://github.com/aoshiwenrou/NewsWithKotlin' // 项目的主页

def gitUrl = 'https://github.com/aoshiwenrou/NewsWithKotlin.git' // Git仓库的url

group = 'com.jcking.jbottomtabbar' // 一般写唯一的包名

version = '0.0.1' // 版本号

install {

repositories.mavenInstaller {

pom {

project {

packaging 'aar'

name 'bottom tabbar for android' //项目描述

url siteUrl

licenses {

license {

name 'The Apache Software License, Version 2.0'

url 'http://www.apache.org/licenses/LICENSE-2.0.txt'

}

}

developers {

developer { //填写的一些基本信息

id 'jcking'

name 'Jcking.Wang'

email '380488695@qq.com'

}

}

scm {

connection gitUrl

developerConnection gitUrl

url siteUrl

}

}

}

}

}

task androidJavadocs(type: Javadoc) {

source = android.sourceSets.main.java.srcDirs

classpath += project.files(android.getBootClasspath().join(File.pathSeparator))

}

task androidSourcesJar(type: Jar) {

classifier = 'sources'

from android.sourceSets.main.java.srcDirs

}

artifacts {

archives androidSourcesJar

}

Properties properties = new Properties()

properties.load(project.rootProject.file('local.properties').newDataInputStream())

bintray {

user = properties.getProperty("bintray.user")

key = properties.getProperty("bintray.apikey")

configurations = ['archives']

pkg {

version {

name = '0.0.1'

desc = 'bottom tabbar bar 0.0.1'

}

repo = 'maven' // bintray上创建的仓库名,我这用的maven

name = 'bottom-tabbar' //发布到JCenter上的项目名字

licenses = ['Apache-2.0']

vcsUrl = gitUrl

publish = true

}

}

配置好上述后需要在你的项目的根目录上的local.properties文件里(一般这文件需gitignore,防止泄露账户信息)配置你的bintray账号信息,bintray.user为你的用户名,bintray.apikey为你的账户的apikey,可以点击进入你的账户信息里再点击Edit即有查看API Key的选项,把他复制下来。

然后在目标mudule的build.gradle上进行调用。

//apply from: './nexus-push.gradle' 这个可以注掉了,不然会冲突

apply from: './jcenter-push.gradle'

接下来在Android Studio的Terminal面板执行命令:

// windows

gradlew bintrayUpload

// macOS

./gradlew bintrayUpload

成功之后会看到类似这样的

jcenter上传成功.png

成功后,可以在bintray网站上的自己的Maven仓库中查看自己的发布的项目:

bintray仓库.png

3.发布项目

将你的bintray下的Maven仓库中的项目include到JCenter仓库中,就点击一个按钮即可。具体就是,点击进入你刚刚上传项目package的详细页当中,在右下角有一个Add to JCenter按钮,点击它,然后写上一些message,最后点击send按钮,就完事了。

bintray加入jcenter.png

写在最后

到这里,关于在Android Studio上通过Nexus搭建Maven私服并上传JCenter的全部内容基本就到这了。内容不是很多,但是坑真的多,折腾了一整天,我成功的流程全在上面,按照这个流程过,基本没问题了。如果有其他问题,欢迎留言讨论。

参考:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值