zoukankan      html  css  js  c++  java
  • Android依赖管理与私服搭建

    *本篇文章已授权微信公众号 guolin_blog (郭霖)独家发布

    1.Android引入依赖的多种方式
    2.多平台私服搭建
    3.创建和依赖自己的代码仓库

    1、Android引入依赖的多种方式
    引入方式一

    AS切换project视图>新建libs目录(高版本AS默认存在libs目录)>copy jar 包>add as library

    下面以我开源的支付宝以及微信app支付项目为例引入相关的jar

    方式一:add as library

    直接使用Gradle-Sync Now

    引入方式二(module>aar)

    import module>Project Structure>选择对应的Module>module dependent
    我们知道jar包是不能引入资源文件的,如果已module模式引入资源文件同样可以使用。

    打开Project Structure-方式1

    打开Project Structure-方式2

    Project Structure

    Lib Module 生成aar

    New Module >Android Library>实现包功能并生成aar文件

    构建生成aar

    项目中引用aar文件

    引用aar文件

    以上两种引入jar依赖弊端就是jar包升级之后需要Copy替换换新的jar并修改对应的配置文件。

    引入方式三(推荐)

    从仓库中下载引用
    找到jar对应的build.gradle>compile(jcentermavencentermavenlocal)>通过Nexue搭建私服

    https://bintray.com/bintray/jcenter
    http://search.maven.org
    http://www.sonatype.org

    部分网站访问需要科学上网,你懂的哈

    Android Studio最新版默认使用的是jcenter,好处就是jar包升级自需要修改版本号,构建项目会自动从仓库中下载(只会下载一次,下载以后会从本地引入)。

    AS默认使用的是jcenter

    如何引入仓库中的jar

    下面以引入retrofit为例

    引入仓库中的jar-retrofit

    或者直接在对应项目Module的build.gradledependencies节点添加

    compile 'com.squareup.retrofit2:retrofit:2.1.0'
    

    最终配置

    相互依赖的包也会下载下来,retrofit依赖okhttp依赖ikoi

    相互依赖的包也会下载下来

    2、多平台Nexus私服搭建

    http://www.sonatype.org

    http://books.sonatype.com/nexus-book/3.0/reference/index.html

    Note: The GUI installer distribution development is suspended and is no longer an install option until further notice.

    Nexus Repository Manager 3.1.0-04 开始并没有提供GUI的安装程序。建议JDK使用1.8版本。

    官网首页

    选择最新版本Nexus3

    下载

    安装介绍资料
    http://books.sonatype.com/nexus-book/3.0/reference/install.html#installation-archive

    解压下载的nexus
    $ tar xvzf nexus-3.0.2-02-mac.tgz
    $ tar xvzf nexus-3.0.2-02-unix.tar.gz

    The bin folder contains the generic startup scripts for Unix-like platforms called nexus. The Windows platform equivalent is called nexus.exe. To start the repository manager from the bin folder on a Unix-like platform like Linux use
    ./nexus run
    The equivalent invocation on Windows requires a / in front of the run and any other commands.
    nexus.exe /run

    简单解释就是Mac、Linux平台使用./nexus run Window平台使用 nexus.exe /run 运行Nexus Repository Manager

    Mac

    OSX archive下载解压

    Linux

    Unix archive解压

    bin目录,执行 ./nexus run

    运行Nexus服务

    Nexus Repository Manager依赖的是Java开发环境,最新版JDK得1.8或者以上

    提示设置INSTALL4J_JAVA_HOME_OVERRIDE

    在nexus的bin目录修改nexus,设置INSTALL4J_JAVA_HOME_OVERRIDE

    修改nexus1

    配置默认的JAVA_HOME

    启动Nexus

    关闭防火墙 默认端口8081
    systemctl stop firewalld

    参考质料:
    http://books.sonatype.com/nexus-book/3.0/reference/install.html#installation-java

    2.1、创建自己的仓库

    1、创建用户
    首先使用管理员密码登陆到Nexus私服并添加用户

    登录

    创建用户

    使用新创建的用户登录Nexus并创建仓库

    创建仓库1

    创建仓库2

    创建仓库3

    创建完成

    2.2、上传自己的Module到仓库

    第一 在项目级别的build.gradle中的allprojects下repositories节点添加mavenLocal()

    配置本地maven引用

    第二 在Lib Module级别的build.gradle中添加maven插件apply plugin: 'maven'

    添加maven插件

    第三 在Lib Module级别的build.gradle中android节点添加上传行为

    定义上传行为

    解析:
    compile 'com.android.support:appcompat-v7:24.2.1'
    pom.version = "24.2.1"
    pom.artifactId = "appcompat-v7"
    pom.groupId ="com.android.support"

    //定义上传行为
        uploadArchives {
            //仓库基于maven
            repositories.mavenDeployer {
                //配置上传的url
                repository(url: "http://192.168.1.100:8081/repository/com.javen205/") {
                    authentication(userName: "javen", password: "javen205")
                }
                pom.version = "0.0.1"
                pom.artifactId = "JPay"
                pom.groupId = "com.javen205"
            }
        }
    

    第四 使用Gradle插件上传aar到Maven私服

    点击uploadArchives自动上传

    上传aar到服务器

    刷新页面重新查看

    2.2、引用私服中的Module

    在需要依赖Module的build.gradle中添加如下节点,其中URL就是上文中创建仓库的url

    allprojects {
        repositories {
            maven {
                url "http://192.168.1.100:8081/repository/com.javen205/"
            }
        }
    }
    
        compile 'com.javen205:JPay:0.0.1'
    

    查看 ** External Libraries**

    查看上面上传的aar应用成功

    记录学习的点滴,以此勉励不断奋斗的自己✌️✌️✌️ 如果对你有帮助记得点喜欢告诉我

  • 相关阅读:
    MySQL
    LeetCode
    数据结构
    我的编程幻想曲,更新中
    快速排序
    C与C++的区别
    必须要使用列表初始化的几种情况
    析构中delete this
    指向自身类型的成员指针的初始化,this不属于类对象的一部分
    构造函数
  • 原文地址:https://www.cnblogs.com/zyw-205520/p/6502183.html
Copyright © 2011-2022 走看看