Maven、Jcenter发布library流程记录

我们知道,开发过程中都会有一种托管项目的仓库。常见的有:Gitlab/Oschina/GitHub(私有库是收费的)数据代码管理控制工具

  • Maven属于jar/aar的版本管理工具[]
  • Nexus用于管理maven私服

那么下面我们就一一介绍:


1. 下载、启动

注意 : 此时的nexus服务都是安装包默认的数据存储路径、端口、IP、访问根目录、内存大小,当然也是可以修改的

2、修改数据存储路径,文件目录

  • 文件目录【路径就在bin/nexus.vmoptions】 

3、修改IP、端口、访问根目录,文件目录

  • 修改IP、端口、访问根目录,文件目录

4、查看 http://localhost:8081/

5、create repository

  • 创建一个maven2(host)仓库

6、Maven 仓库配置gradle

 

  • 项目的maven上传配置【project根目录下gradle.properties添加如下内容】

  •  
  • 下面是内网模式的配置

# properties for maven2 repository
# nexus服务器登录时候的用户名/密码
NEXUS_USERNAME=admin
NEXUS_PASSWORD=admin123
# 在POM文件中使用的group ID
POM_GROUP_ID=com.SyShare.maven
# POM文件中指向你网站的地址
POM_URL=http://www.baidu.com/
# SCM是指版本管理工具,以下是他的相关信息
POM_SCM_URL=http://www.baidu.com/
POM_SCM_CONNECTION=http://www.baidu.com/
POM_SCM_DEV_CONNECTION=http://www.baidu.com/
# 你的开放协议相关信息
POM_LICENCE_NAME= Apache License Version 2.0
POM_LICENCE_URL= 
POM_LICENCE_DIST=Apache License Version 2.0
# 开发者的相关信息
POM_DEVELOPER_ID=SyShare
POM_DEVELOPER_NAME=SyShare
  • 配置上传.gradle【本例以maven_push.gradle为准】

project根目录下新建maven_push.gradle(复制)
2,	// The Maven plugin adds support for deploying artifacts to Maven repositories.
// 一个可以让你把库上传到maven仓库的插件
apply plugin: 'maven'
// The signing plugin adds the ability to digitally sign built files and artifacts. These digital signatures can then be used to prove who built the artifact the signature is attached to as well as other information such as when the signature was generated.
// 对库文件进行数字签名的插件,可以通过签名知道谁创建了这个库文件,签名的时间等等信息
apply plugin: 'signing'

// 声明变量记录maven库地址
def mavenRepositoryUrl
// 判断是发布到正式库,还是snapshots库
if (isReleaseBuild()) {
    println 'RELEASE BUILD'
    // 下面的库地址指向的是我们私有仓库的Releases 仓库
    mavenRepositoryUrl = hasProperty('RELEASE_REPOSITORY_URL') ? RELEASE_REPOSITORY_URL
            : "上传的网址/releases/"
} else {
    println 'SNAPSHOTS BUILD'
    // 下面的库地址指向的是我们私有仓库的snapshots 仓库
    mavenRepositoryUrl = hasProperty('SNAPSHOT_REPOSITORY_URL') ? SNAPSHOT_REPOSITORY_URL
            : "上传的网址/snapshots/"
}
// NEXUS_USERNAME等变量在我们主项目的gradle.properties中可以找到
def getRepositoryUsername() {
    return hasProperty('NEXUS_USERNAME') ? NEXUS_USERNAME : ""
}

def getRepositoryPassword() {
    return hasProperty('NEXUS_PASSWORD') ? NEXUS_PASSWORD : ""
}
// 根据我们在likelib下gradle.properties中声明的版本名称,来分辨是Release版本还是 snapshots版本
def isReleaseBuild() {
    return !VERSION_NAME.contains("SNAPSHOT");
}

//"afterEvaluate是什么鸟?你可以理解为在配置阶段要结束,项目评估完会走到这一步。" 引用自http://jiajixin.cn/2015/08/07/gradle-android/
afterEvaluate { project ->
    // 我们声明我们要执行的上传到maven的task
    uploadArchives {
        repositories {
            mavenDeployer {
                beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }
                // 我们类比下compile com.squareup.okhttp:okhttp:2.7.0
                // artifactId 对应com.squareup.okhttp; groupId 对应okhttp;version对应2.7.0
                // 这样就类似坐标的方式定位到了制定的库文件
                pom.artifactId = POM_ARTIFACT_ID
                pom.groupId = POM_GROUP_ID
                pom.version = VERSION_NAME

                // 授权验证,这里也就是你登录搭建的私服服务器时候的用户名\密码
                repository(url: mavenRepositoryUrl) {
                    authentication(userName: getRepositoryUsername(), password: getRepositoryPassword())
                }

                // 这里是配置我们maven库需要的pom.xml文件的各个内容,具体意思我们在主目录gradle.properties中解释
                pom.project {
                    name POM_NAME
                    packaging POM_PACKAGING
                    description POM_DESCRIPTION
                    url POM_URL

                    scm {
                        url POM_SCM_URL
                        connection POM_SCM_CONNECTION
                        developerConnection POM_SCM_DEV_CONNECTION
                    }

                    licenses {
                        license {
                            name POM_LICENCE_NAME
                            url POM_LICENCE_URL
                            distribution POM_LICENCE_DIST
                        }
                    }

                    developers {
                        developer {
                            id POM_DEVELOPER_ID
                            name POM_DEVELOPER_NAME
                        }
                    }
                }
            }
        }
    }

    // 进行数字签名
    signing {
        required { isReleaseBuild() && gradle.taskGraph.hasTask("uploadArchives") }
        sign configurations.archives
    }

    // type显示指定任务类型或任务, 这里指定要执行Javadoc这个task,这个task在gradle中已经定义
    task androidJavadocs(type: Javadoc) {
        // 设置源码所在的位置
        source = android.sourceSets.main.java.sourceFiles
    }

//    // 生成javadoc.jar
//    task androidJavadocsJar(type: Jar) {
//        // 指定文档名称
//        classifier = 'javadoc'
//        from androidJavadocs.destinationDir
//    }

//    // 生成sources.jar
//    task androidSourcesJar(type: Jar) {
//        classifier = 'sources'
//        from android.sourceSets.main.java.sourceFiles
//    }

    // 产生相关配置文件的任务
    artifacts {
//        archives androidSourcesJar
//        archives androidJavadocsJar
    }
}

 

  • 必须配置的是:

// 下面的库地址指向的是我们私有仓库的Releases 仓库
mavenRepositoryUrl = hasProperty('RELEASE_REPOSITORY_URL') ? RELEASE_REPOSITORY_URL
        : "http://上传的网址/releases/"
// 下面的库地址指向的是我们私有仓库的snapshots 仓库
mavenRepositoryUrl = hasProperty('SNAPSHOT_REPOSITORY_URL') ? SNAPSHOT_REPOSITORY_URL
        : "http:/上传的网址/snapshots/"

 

  • 对应的网址 如下图所示:

 

  • lib module目录下添加gradle.proerties,并添加如下内容 图片

 

  • 最后在你的lib module的build.gradle添加一行  : apply from: '../maven_push.gradle'

     感谢 : @少将博客@键盘男

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值