maven插件切换到maven-publish插件

192 阅读1分钟

Maven插件

apply plugin: 'maven'

// 设置上传的库包信息
final String GROUP_ID = "com.xxx.light"
final String ARTIFACT_ID = "m-smart-xwalk-hybrid"
final String MODULE_VERSION = "0.0.1-SNAPSHOT"

// 设置上传源代
task androidJavadocs(type: Javadoc) {
    source = android.sourceSets.main.java.srcDirs
    classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}

// 设置上传源代
task androidSourcesJar(type: Jar) {
    classifier = 'sources'
    from android.sourceSets.main.java.srcDirs
}

artifacts {
    archives androidSourcesJar
}

def isReleaseBuild(version) {
    return version.toUpperCase().contains("SNAPSHOT") == false
}

def getRepositoryUrl(version) {
    return isReleaseBuild(version) ? RELEASE_REPOSITORY : SNAPSHOT_REPOSITORY
}

uploadArchives {
    repositories {
        mavenDeployer {
            //这里的url是nexus中maven-releases的路径,可以点击copy按钮查看复制
            repository(url: getRepositoryUrl(MODULE_VERSION)) {
                // nexus账号的用户名和密码,我这里没用默认的admin
                authentication(userName: USER_NAME, password: USER_PASSWORD)
            }

            // 下面这三项pom参数,在调用的时候是这个样子 : compile 'com.jcking.jbottomtabbar:jbottomtabbar:0.0.1'
            // library的包名
            pom.groupId = GROUP_ID
            // library的项目名
            pom.artifactId = ARTIFACT_ID
            // library的版本号
            pom.version = MODULE_VERSION

            pom.project {
                licenses {
                    license {
                        name 'The Apache Software License, Version 2.0'
                        url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                    }
                }
            }
        }
    }
}




Maven-Publish插件



// maven-publish配置,参考文章:https://blog.csdn.net/lyabc123456/article/details/133849824#:~:text=%E5%9C%A8%E6%97%A5%E5%B8%B8%E5%BC%80%E5%8F%91%E4%B8%AD%EF%BC%8C%E4%B8%8D%E5%8F%AF%E9%81%BF
// 具体细节与调整可翻阅文档

apply plugin: 'maven-publish'
apply plugin: 'com.android.library'

def _groupId = "com.xxx.light"
def _artifactId = "m-smart-upgrade"
def _version = "0.1.6-SNAPSHOT"

tasks.register('sourceJar', Jar) {
    from android.sourceSets.main.java.getSrcDirs()
    archiveClassifier = "sources"
}

publishing {
    publications {
        aar(MavenPublication) {
            // #指定maven依赖产物aar
            afterEvaluate { artifact(tasks.getByName("bundleReleaseAar")) }
            // #增加上传源码的task,方便查阅代码注释
            artifact sourceJar

            // #指定库版本信息
            groupId = _groupId
            artifactId = _artifactId
            version = _version

            // #指定POM信息
            pom {
                name.set("lib-xxx-basic")
                description.set("基础通用库,提供一些列方便开发者使用的工具类")
                developers {
                    developer {
                        id.set("weinp1")
                        name.set("weinp1")
                        email.set("weinp1@xxx.com")
                    }
                }
            }

            // 编写当前库依赖的第三方库
            // gradle 7.4
            // configurations.implementation.allDependencies 只包含[Implementation、api]
            // 不包含例如[compileOnly、debugImplementation、releaseImplementation、....等等]
            pom.withXml {
                def dependenciesNode = asNode().appendNode('dependencies')
                def libraryName = "${_groupId}:${_artifactId}:${_version}"
                println "库名: ${libraryName} 的依赖库"
                configurations.implementation.allDependencies.each {
                    if (it.group != null && (it.name != null && "unspecified" != it.name) && it.version != null) {
                        println "库: ${it.group}:${it.name}:${it.version}"
                        def dependencyNode = dependenciesNode.appendNode('dependency')
                        dependencyNode.appendNode('groupId', it.group)
                        dependencyNode.appendNode('artifactId', it.name)
                        dependencyNode.appendNode('version', it.version)
                        dependencyNode.appendNode('scope', 'implementation')
                    }
                }
            }

        }
    }

    repositories {
        // #指定本地仓库
        mavenLocal()
        // #指定远程仓库(debug)
        maven {
            name = 'debugRemote'
            url = SNAPSHOT_REPOSITORY

            credentials {
                username = USER_NAME
                password = USER_PASSWORD
            }
        }
        // #指定远程仓库(release)
        maven {
            name = 'releaseRemote'
            url = RELEASE_REPOSITORY

            credentials {
                username = USER_NAME
                password = USER_PASSWORD
            }
        }
    }
}


具体说明,代码上有解释。 从maven切换到maven-publish难度不大。maven-publish支持更多的配置,更加灵活