我这里取的文件名是:publish-mavencentral.gradle
apply plugin: 'maven-publish'apply plugin: 'signing'task androidSourcesJar(type: Jar) { classifier = 'sources' from android.sourceSets.main.java.source exclude "**/R.class" //清除`R.class` exclude "**/BuildConfig.class" //清除`BuildConfig.class`}//--- 修改配置一 ---ext {path= PUBLISH_GROUP_ID = '之前注册sonatype时填写的的的groupId域名,假如是github的则是io.github.xxx' PUBLISH_ARTIFACT_ID = '库的名称' PUBLISH_VERSION = '库的版本'}//--- 修改配置二 ---ext["signing.keyId"] = ''ext["signing.password"] = ''ext["signing.secretKeyRingFile"] = ''ext["ossrhUsername"] = ''ext["ossrhPassword"] = ''File secretPropsFile = project.rootProject.file('local.properties')if (secretPropsFile.exists()) { println "Found secret props file, loading props" Properties p = new Properties() p.load(new FileInputStream(secretPropsFile)) p.each { name, value -> ext[name] = value }} else { println "No props file, loading env vars"}publishing { publications { release(MavenPublication) { println("publish-maven Log-------> PUBLISH_GROUP_ID: $PUBLISH_GROUP_ID; PUBLISH_ARTIFACT_ID: $PUBLISH_ARTIFACT_ID; PUBLISH_VERSION: $PUBLISH_VERSION") // The coordinates of the library, being set from variables that // we'll set up in a moment //配置一传入的参数 groupId PUBLISH_GROUP_ID artifactId PUBLISH_ARTIFACT_ID version PUBLISH_VERSION // Two artifacts, the `aar` and the sources artifact("$buildDir/outputs/aar/${project.getName()}-release.aar") artifact androidSourcesJar // Self-explanatory metadata for the most part pom { //--- 修改配置三 --- name = PUBLISH_ARTIFACT_ID description = '上传aar插件至mavencentral,方便使用implementation快速引入' //添加文件形貌 // If your project has a dedicated site, use its URL here url = 'https://github.com/xxx/xxxx' //项目github链接 licenses { license { //协议范例,一样平常默认Apache License2.0的话不消改: name = 'The Apache License, Version 2.0' url = 'http://www.apache.org/licenses/LICENSE-2.0.txt' } } developers { developer { //--- 修改配置四 --- id = '用户ID' //你的sonatype用户ID name = '用户名' //你的sonatype用户名 email = '邮箱' //你的sonatype注册邮箱 } } // Version control info, if you're using GitHub, follow the format as seen here scm { //--- 修改配置五 --- //修改成你的Git地点: connection = 'scm:git:github.com/xxx/xxxx.git' developerConnection = 'scm:git:ssh://github.com/xxx/xxxx.git' //分支地点: url = 'https://github.com/xxx/xxxx/tree/master' } // A slightly hacky fix so that your POM will include any transitive dependencies // that your library builds upon withXml { def dependenciesNode = asNode().appendNode('dependencies') project.configurations.implementation.allDependencies.each { def dependencyNode = dependenciesNode.appendNode('dependency') dependencyNode.appendNode('groupId', it.group) dependencyNode.appendNode('artifactId', it.name) dependencyNode.appendNode('version', it.version) } } } } } repositories { // The repository to publish to, Sonatype/MavenCentral maven { // This is an arbitrary name, you may also use "mavencentral" or // any other name that's descriptive for you //--- 修改配置六 --- name = "项目名称" def releasesRepoUrl = "https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/" def snapshotsRepoUrl = "https://s01.oss.sonatype.org/content/repositories/snapshots/" // You only need this if you want to publish snapshots, otherwise just set the URL // to the release repo directly url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl // The username and password we've fetched earlier credentials { username ossrhUsername password ossrhPassword } } }}signing { sign publishing.publications}针对以上代码,有几处必要修改成自己的配置信息