Android 将Library 发布本地maven仓库

Android 将Library 发布本地maven仓库 (Mac OS环境)

(最近在学习项目模块化开发,写下 Library 项目下如何发布引用相关)

maven安装配置

  1. 下载安装 maven
    从官网https://maven.apache.org/download.cgi下载 Maven 并解压 (下面是maven 官方说明文档)
Installing Maven
  ----------------
  1) Unpack the archive where you would like to store the binaries, e.g.:
    Unix-based operating systems (Linux, Solaris and Mac OS X)
      tar zxvf apache-maven-3.x.y.tar.gz
    Windows
      unzip apache-maven-3.x.y.zip
  2) A directory called "apache-maven-3.x.y" will be created.
  3) Add the bin directory to your PATH, e.g.:
    Unix-based operating systems (Linux, Solaris and Mac OS X)
      export PATH=/usr/local/apache-maven-3.x.y/bin:$PATH
    Windows
      set PATH="c:\program files\apache-maven-3.x.y\bin";%PATH%
  4) Make sure JAVA_HOME is set to the location of your JDK
  5) Run "mvn --version" to verify that it is correctly installed.
  1. 配置maven 和 JDK 环境变量
  • 终端输入
    open -n ~/.bash_profile
#jdk
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home
export PATH=$JAVA_HOME/bin:$PATH 
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar  
#android sdk
export ANDROID_HOME=/Volumes/data/android-sdk-macosx
export PATH=${PATH}:${ANDROID_HOME}/tools
export PATH=${PATH}:${ANDROID_HOME}/platform-tools
#maven
export MAVEN_HOME=/Users/mac/apache-maven-3.6.0
export PATH=$MAVEN_HOME/bin:$PATH
  • 编辑完保存并退出
  • 输入 source ~/.bash_profile 使环境变量生效。
  • 输入 mvn -v 查看 Maven 否安装成功。
Apache Maven 3.6.0 (97c98ec64a1fdfee7767ce5ffb20918da4f719f3; 2018-10-25T02:41:47+08:00)
Maven home: /Users/mac/apache-maven-3.6.0
Java version: 1.8.0, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "mac os x", version: "10.13.2", arch: "x86_64", family: "mac"

发布到本地库

直接通过项目发布到本地

  1. 新建一个Libary项目,在最外层bulid.gradle 里添加
apply plugin: 'com.android.library'
apply plugin: 'maven' //新增
android {
    compileSdkVersion Integer.parseInt(project.ANDROID_BUILD_SDK_VERSION)
    buildToolsVersion project.ANDROID_BUILD_TOOLS_VERSION
    defaultConfig {
        minSdkVersion Integer.parseInt(project.ANDROID_BUILD_MIN_SDK_VERSION)
        targetSdkVersion Integer.parseInt(project.ANDROID_BUILD_TARGET_SDK_VERSION)
        versionCode Integer.parseInt(project.VERSION_CODE)
        versionName project.VERSION_NAME
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    provided 'com.android.support:support-annotations:25.3.1'
    provided 'com.chinaway.android:core:1.2.0'
}
uploadArchives{//新增
    repositories.mavenDeployer{
        // 本地仓库路径(/Users/mac/.m2/repository)路径注意改一下
        repository(url:"file:///Users/mac/.m2/repository/")
        // 唯一标识、包名
        pom.groupId = "com.evinlo.android"
        // 项目名称
        pom.artifactId = "pay"
        // 版本号
        pom.version = "1.1.1"
    }
}

通过gradlew uploadArchives发布到本地
也可以通过android studio 里的 右侧的gradle发布

maven_bulid.png

直接通过命令行发布到本地

  1. 将获取到的 jar 包或者 aar 包
mvn install:install-file -Dfile=xx.jar -DgroupId=xx -DartifactId=xx -Dversion=xx -Dpackaging=jar

-Dfile: jar的所在路径+jar包名+.jar
-DgroupId: 对应maven坐标的groupId
-DartifactId: 对应maven坐标的artifactId
-Dverson: 对应maven坐标的version
  1. 引入依赖文件pom.xml

com.evinlo.android
pay
1.1.1

使用本地仓库

  1. 在Android项目最外层的build.gradle文件中配置路径
buildscript {
    repositories {
        maven {//新增
            url 'file:///Users/mac/.m2/repository/'
        }
        maven { url "https://jitpack.io" }
        google()
        jcenter()
    }
    dependencies {
      `````
    }
}
allprojects {
    repositories {
        maven {//新增
            url 'file:///Users/mac/.m2/repository/'
        }
        maven { url "https://jitpack.io" }
        google()
        jcenter()
    }
}
  1. 在需要引用包的项目里层的 build.gradle添加
dependencies {
    api fileTree(dir: 'libs', include: ['*.jar'])
    api 'com.evinlo.android:pay:1.1.1'  //新增
}

后续补充远程仓库nexus maven

你可能感兴趣的:(Android 将Library 发布本地maven仓库)