美文网首页Gradle
Android搭建本地Maven私服 - 上传aar/jar -

Android搭建本地Maven私服 - 上传aar/jar -

作者: ZHDelete | 来源:发表于2018-09-27 15:43 被阅读197次

    1: 下载Nexus 3

    地址如下:

    https://www.sonatype.com

    Nexus_官网下载.jpg

    这里,我下载了windwos 版本

    注: Nexus 3.x 版本的运行,需要jdk1.8

    解压缩后,得到:

    目录.jpg

    说明:

    //Nexus 运行时所需文件,例如:启动脚本等
    nexus-3.13.0-01
    //Nexus 生成的 配置文件,日志文件,仓库文件
    sonatype-work
    

    2: 修改端口

    • [1] 配置nexus3 的本地端口的文件:
    nexus-3.13.0-01-win64\sonatype-work\nexus3\etc\nexus-default.properties
    
    端口配置_1.jpg
    • [2] 配置全局的本地端口的文件:
    nexus-3.13.0-01-win64\nexus-3.13.0-01\etc\nexus-default.properties
    
    端口配置_2.jpg

    3: Nexus3 启动

    配置完成后,打开cmd或者powershell,进入nexus-3.0.1-01\bin下。

    执行:nexus.exe /run
    注意:命令行里的 斜杠

    常用命令
    1.nexus.exe /install 安装nexus为系统服务
    2.nexus.exe /uninstall 卸载nexus为系统服务
    3.nexus.exe /start 启动nexus服务
    4.nexus.exe /stop 停止nexus服务

    等待一段初始化的时间,

    启动成功如下:

    run_success.jpg

    我们浏览器输入地址: http://localhost:9099/

    注意: 这里的端口号要与我们配置的端口号统一

    点击右上角的 Sign in ,即可登录,

    默认用户名admin 密码:admin123

    登陆后如下:

    browse.jpg

    4: 上传jar到库

    我们这里只采用 图形化的方式上传
    首先,将自己的android-library 打包成aar,然后,在如下界面,填写相关参数,进行上传

    上传.jpg

    上传结果:

    上传成功.jpg

    5 在其他项目中依赖我们的开源库

    • 5.1 Project的build.gradle

    在`allprojects - repositories 节点下,新增本地maven库

    allprojects {
        repositories {
            google()
            jcenter()
            maven {
                url 'http://localhost:9099/repository/maven-releases'
            }
        }
    }
    
    • 5.2 app的build.gradle
      dependencies 节点,依赖我们需要的库
    dependencies {
        //依赖maven 私服的 开源库
        implementation 'com.zhdelete_lib_1:lib1jar:1.0.0'
    }
    

    这些操作,就和我们依赖jcenter中的类库,或者jetpack() 的类库是一个道理的

    我们尝试 调用开源类库里的方法,如下:

    调用.png

    可以看到,我们正常的调用到了jar里的方法.

    6: 上传aar到库

    我们使用AS建立的android library 打包之后(build - assembleRelease),生成的是aar包,aar包的依赖较之jar包,是有些区别的

    上传arr包如下:

    上传aar.jpg 上传aar成功.jpg

    7: 使用aar

    • [1] 修改 project的build.gradle

    在`allprojects - repositories 节点下,新增本地maven库

    allprojects {
        repositories {
            google()
            jcenter()
            maven {
                url 'http://localhost:9099/repository/maven-releases'
            }
        }
    }
    
    • [2] 修改 app的build.gradle
    dependencies {
        //依赖maven 私服的 开源库 jar
        implementation 'com.zhdelete_lib_1:lib1jar:1.0.0'
        //依赖maven 私服的 开源库 aar
        implementation 'com.zhdelete.android_lib:libandroid:1.0.0@aar'
    }
    

    这里的组合就是刚才填写的 graoupId:artifactId:version这里注意需要后面跟上@aar,如果不跟上这个默认是去加载.jar的文件,就会出现找不到的情况,怎么样可以不加,可以参见:Android 打包上传AAR文件到本地Maven仓库并且引用

    最终,我们在自己的代码里,尝试调用aar里的内容,如下:

    aar_依赖成功.jpg

    8: 使用gradle任务上传aar

    我们修改androd-library的NexusView,新增一个testMethod方法,以便作为android-library的1.1.0版本

    public class NexusView extends View {
        public NexusView(Context context) {
            super(context);
        }
    
        public NexusView(Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);
        }
    
        public NexusView(Context context,@Nullable AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        public void testMethod() {
            Log.d("NexusView", "testMethod invoke");
        }
    }
    

    然后,在androd-library 这个module下面的build.gradle,添加如下修改:

    增加maven插件

    apply plugin: 'maven'
    

    增加上传aar 的gradle task

    uploadArchives {
        configuration = configurations.archives
        repositories {
            mavenDeployer {
                repository(url: 'http://127.0.0.1:9099/repository/maven-releases') {
                    authentication(userName: 'admin', password: 'admin123')
                }
    
                pom.project {
                    //版本名称
                    version '1.1.0'
                    //groupId 和 artifactId 要与服务器上面统一,方能实现版本更新
                    artifactId 'libandroid'
                    groupId 'com.zhdelete.android_lib'
                    packaging 'aar'
                    //更新描述
                    description 'update version 1.1.0 add new method'
                }
            }
        }
    }
    

    最终,module的build.gradle如下:

    apply plugin: 'com.android.library'
    apply plugin: 'kotlin-android'
    
    
    apply plugin: 'maven'
    
    
    uploadArchives {
        configuration = configurations.archives
        repositories {
            mavenDeployer {
                repository(url: 'http://127.0.0.1:9099/repository/maven-releases') {
                    authentication(userName: 'admin', password: 'admin123')
                }
    
                pom.project {
                    //版本名称
                    version '1.1.0'
                    //groupId 和 artifactId 要与服务器上面统一,方能实现版本更新
                    artifactId 'libandroid'
                    groupId 'com.zhdelete.android_lib'
                    packaging 'aar'
                    //更新描述
                    description 'update version 1.1.0 add new method'
                }
            }
        }
    }
    
    android {
        compileSdkVersion 28
    
        defaultConfig {
            minSdkVersion 22
            targetSdkVersion 28
            versionCode 1
            versionName "1.0"
    
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    
        }
    
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    
    }
    
    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
    
        implementation 'com.android.support:appcompat-v7:28.0.0'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'com.android.support.test:runner:1.0.2'
        androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    }
    repositories {
        mavenCentral()
    }
    

    接着,我们重新同步一下项目,就会在gradle tab下发现新的uploadArchives 这个task了

    aar_maven_task_upload.jpg

    我们点击运行这个task,就会成功的把新版本 上传到maven库了:

    maven_插件上传成功.jpg

    更多关于gradle maven插件 详情查看 Maven Plugin

    然后,我们去引用这个库的项目的build.gradle,修改依赖为

    dependencies {
        //依赖maven 私服的 开源库 jar
        implementation 'com.zhdelete_lib_1:lib1jar:1.0.0'
        //依赖maven 私服的 开源库 aar
        implementation 'com.zhdelete.android_lib:libandroid:1.1.0'
    }
    
    

    此时,我们就不需要添加@aar也能成功使用了,我们同步一下项目看看结果:

    maven_插件上传_引用成功.jpg

    小结:

    简要的步骤如下:

    1.安装maven管理的一个工具nexus
    2.制作AAR
    3.网站上传AAR
    4.代码上传AAR
    

    本文参考:

    1: 使用 Sonatype Nexus 3 搭建 Maven 私服、本地私有 Maven 仓库,Gradle 打包 jar 、arr 后自动上传到 Nexus Maven 仓库配置

    2: maven使用nexus3.3在windows下搭建私服

    3: Android 打包上传AAR文件到本地Maven仓库并且引用

    相关文章

      网友评论

        本文标题:Android搭建本地Maven私服 - 上传aar/jar -

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