Android 发布项目到私服Nexus仓库

部分参考自:http://blog.csdn.net/H_Zhang/article/details/51558800

发布到私服的话一般都是是使用Nexus(http://www.sonatype.org/nexus/go/),

一般公司内部会自己搭建私有Maven仓库,就把包发布到私有Maven仓库当中,以方便别人直接使用。


1.下载和配置Nexus软件


首先去官网下载Nexus软件,启动Nexus,在浏览器输入:http://localhost:8081/nexus/ 即可看到Maven仓库管理界面。


然后点击右上角进行登录,默认帐号:admin,密码:admin123。密码可以进行修改。

然后点击repositories,进入到仓库列表界面:

这里的仓库分了四种类型

       1.hosted(宿主仓库):用来部署自己,第三方或者公共仓库的构件

       2.proxy(代理仓库):代理远程仓库

       3.virtual(虚拟仓库):默认提供了一个 Central M1虚拟仓库 用来将maven 2适配为maven 1

       4.group(仓库组):统一管理多个仓库

我们可以再简历自己的hosted,也可以使用现在默认有的。一般我们发布版会在Releases下面,快照版发布在sonatype下。

比如现在我需要发布在releases下面,点击releases站点:


看到上面的界面,点击config界面,设置deployment policy为Allow Redeploy。


接下来,就有很多处理方法了,有的人说点击Artrifact Upload进行上传,上传可以选择pom方式和GAV方式,都是些什么鬼,太复杂麻烦了。

还是直接使用代码上传吧!


2.配置 module build.gragle


一般我们是上传第三方库到私服中,假设现在你有一个库需要上传到私服中。

module的build.gradle的脚本代码:

 
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin:'maven-publish'
apply plugin:'maven'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.0"

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    lintOptions {
        checkReleaseBuilds false
        abortOnError false
    }

    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/ASL2.0'
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:26.+'
    testCompile 'junit:junit:4.12'
    compile "org.jetbrains.kotlin:kotlin-stdlib:1.1.2"
    compile "org.jetbrains.anko:anko-common:0.8.2"
}

// 上传到私服
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
uploadArchives {
    repositories {
        mavenDeployer {
            repository(url: properties.getProperty("POM_URL")) {
                authentication(userName: properties.getProperty("nexus.user"), password: properties.getProperty("nexus.password"))
            }

            pom.groupId = properties.getProperty("POM_GROUP_ID")
            pom.artifactId = properties.getProperty("POM_ATRIFACT_ID")
            pom.version = properties.getProperty("POM_VERSION")

            pom.project {
                licenses {
                    license {
                        name 'The Apache Software License, Version 2.0'
                        url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                    }
                }
            }
        }
    }
}

可以看到,基本上都是Android Studio自动生成的代码,在头部引用了两个插件:

apply plugin:'maven-publish'
apply plugin:'maven'

用于上传用的,然后就是uploadArchives 的配置了,我把对应的参数配置都藏在了local.poperties中,主要是6个参数的配置:

nexus.user=admin
nexus.password=admin123

POM_URL=http://localhost:8081/nexus/content/repositories/releases/
POM_GROUP_ID=com.demo.library
POM_ATRIFACT_ID=libgif
POM_VERSION=1.0.1

nexus.user是帐号,nexus.password是密码,然后URL是你要上传到那个站点下的路由,

先看下我们这个库到时候应该是这么引用的:compile 'com.demo.library:libgif:1.0.1' 。所以:

GROUP_ID = com.demo.library

ATRIFACT_ID = libgif

VERSION = 1.0.1

这么看总该理解了吧。其他的地方按着写就可以了。

由于我的库是用Kotlin语言写的,所以在开头有两行关于kotlin的插件引用。

apply plugin:'maven-publish'
apply plugin:'maven'

是用于上传需要引用的插件。


3.上传


接着就是上传了,在Android Studio的Terminal面板执行如下命令:

gradlew uploadArchives

成功后的界面:


然后我们可以在nexus的界面看到我们上传的库:



4.使用


成功后怎么使用呢?

首先在项目根目录下的build.gradle文件添加仓库url地址。

build.gradle

allprojects {
    repositories {
        jcenter()
        maven {
            url "http://localhost:8081/nexus/content/repositories/releases"
        }
    }
}
添加好我们的地址,然后在引用到该库的build.gradle中配置:

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:26.+'
    testCompile 'junit:junit:4.12'
    compile 'org.jetbrains.kotlin:kotlin-stdlib:1.1.2'
    compile 'org.jetbrains.anko:anko-common:0.8.2'
    compile 'com.demo.library:libgif:1.0.1'
}
然后运行项目就Ok了!



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值