设为首页 加入收藏

TOP

Nexus私有maven库部署和使用(三)
2023-07-25 21:41:47 】 浏览:105
Tags:Nexus 私有 maven
uot;) } } repositories { maven { //设置仓库地址和账号密码 url = NEXUS_MAVEN_URL credentials { username = NEXUS_USERNAME password = NEXUS_PASSWORD } //下面是动态切换release仓库或snapshot仓库 // def releasesRepoUrl = "$buildDir/repos/releases" // def snapshotsRepoUrl = "$buildDir/repos/snapshots" // url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl } } } }

编写了上述的脚本代码后,及得resync一下仙姑,之后从右侧的Gradle菜单进入到你的模块下,展开task目录,可以找到发布的命令,如下图所示

这里发布的话,步骤有4个:

  1. 修改版本号(上述脚本里的POM_VERSION字段)
  2. 执行clean命令
  3. 执行编译命令
  4. 执行发布命令

具体步骤如下图所示:

gradle如何依赖私有库的话,请看下面的章节说明

多Module发布

如果我们是想要发布多个Module,如果采用上述的方法,就是得在每个Module里都加上脚本,十分繁琐。

研究了下,想实现Jitpack那种可以一键发布所有Module的方法,最终还是不成功,于是就采用了一种稍微折中的方法,就是创建一个公共的gradle脚本,用来发布aar,然后所有的Module去引用此脚本即可,虽然也要对每个Module进行修改,但也算是比较折中的方法了,图省事的话可以用替换功能进行脚本的添加

公共脚本nexus-maven-publish.gradle,放在了项目的根目录,与setting.gradle同级别,脚本代码如下:

apply plugin: 'maven-publish'

def NEXUS_MAVEN_URL = "http://localhost:9888/repository/maven-releases/"
def NEXUS_USERNAME = "admin"
def NEXUS_PASSWORD = "admin"

//与jitpack发布的保持同个组织名
def POM_GROUP_ID = "com.github.TYKYTeam.swan-android-libray"
//版本好
def POM_VERSION = "2.1"
//aar包方式
def POM_PACKAGING = "aar"
//下面三个数值自动读取模块名
def POM_NAME = ""
def POM_ARTIFACT_ID = ""
def POM_DESCRIPTION = ""

def depList = parent.getDependencies()
depList.getModules().each {
    println "数据。。" + this.name
    POM_ARTIFACT_ID = this.name
    POM_NAME = this.name
}

aftereva luate {
    publishing {
        publications {
            aar_pub(MavenPublication) {

                //定义group和版本
                group = POM_GROUP_ID
                //定义构造物id
                artifactId = POM_ARTIFACT_ID
                version = POM_VERSION

//            artifact androidSourcesJar//将源码打包进aar,如果不需要可以去掉
//            artifact androidJavadocsJar//将注释打包进aar,如果不需要可以去掉
                artifact("$buildDir/outputs/aar/${project.getName()}-release.aar")

                pom {
                    name = POM_NAME
                    description = POM_DESCRIPTION
                    url = 'http://www.example.com/library'
                    //类型设置为aar
                    packaging = POM_PACKAGING

                    licenses {
                        license {
                            name = 'The Apache License, Version 2.0'
                            url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                        }
                    }
                    developers {
                        developer {
                            id = 'com'
                            name = 'stars-one'
                            email = 'stars-one@example.com'
                        }
                    }
                }

                //带上依赖 ,否则会报错
                pom.withXml {
                    def dependenciesNode = asNode().appendNode('dependencies')

                    def scopes = [configurations.compile]
                    if (configurations.hasProperty("api")) {
                        scopes.add(configurations.api)
                    }
                    if (configurations.hasProperty("implementation")) {
                        scopes.add(configurations.implementation)
                    }
                    if (configurations.hasProperty("debugImplementation")) {
                        scopes.add(configurations.debugImplementation)
                    }
                    if (configurations.hasProperty("releaseImplementation")) {
                        scopes.add(configurations.releaseImplementation)
                    }

                    scopes.each { scope ->
                        scope.allDependencies.each {
                            if (it instanceof ModuleDependency) {
                                boolean isTransitive = ((ModuleDependency) it).transitive
                                if (!isTransitive) {
                                    println "<<<< not transitive dependency: [${it.group}, ${it.name}, ${it.version}]"
                                    return
                                }
                            }

                            if (it.group == "${project.rootProject.name}.libs" || it.version == 'unspecified') {
                                return
                            }

                            if (it.group && it.
首页 上一页 1 2 3 4 5 下一页 尾页 3/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇数据类型学习与拓展 下一篇读Java性能权威指南(第2版)笔记..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目