|
| 1 | +//教程:https://juejin.cn/post/6951209116521988132 |
| 2 | +apply plugin: 'maven-publish' |
| 3 | +apply plugin: 'signing' |
| 4 | + |
| 5 | +task androidSourcesJar(type: Jar) { |
| 6 | + archiveClassifier.set("sources") |
| 7 | + from android.sourceSets.main.java.source |
| 8 | + exclude "**/R.class" |
| 9 | + exclude "**/BuildConfig.class" |
| 10 | +} |
| 11 | + |
| 12 | +publishing { |
| 13 | + // 定义发布什么 |
| 14 | + publications { |
| 15 | + mavenJava(MavenPublication) { |
| 16 | + // group id,发布后引用的依赖的 group id |
| 17 | + groupId 'io.github.fantastic' |
| 18 | + // 发布后引用的依赖的 artifact id |
| 19 | + artifactId 'lib_common_dialog' |
| 20 | + // 发布的版本 |
| 21 | + version '1.2.0' |
| 22 | + // 发布的 arr 的文件和源码文件 |
| 23 | + artifact("$buildDir/outputs/aar/${project.getName()}-release.aar") |
| 24 | + artifact androidSourcesJar |
| 25 | + pom { |
| 26 | + // 构件名称,可以自定义 |
| 27 | + name = 'dialog-common' |
| 28 | + // 构件描述 |
| 29 | + description = 'A common dialog library' |
| 30 | + // 构件主页 |
| 31 | + url = 'https://github.com/crazyqiang/AndroidStudy/tree/master/lib_dialog' |
| 32 | + // 许可证名称和地址 |
| 33 | + licenses { |
| 34 | + license { |
| 35 | + name = 'The Apache License, Version 2.0' |
| 36 | + url = 'http://www.apache.org/licenses/LICENSE-2.0.txt' |
| 37 | + } |
| 38 | + } |
| 39 | + // 开发者信息 |
| 40 | + developers { |
| 41 | + developer { |
| 42 | + name = 'MaQiang' |
| 43 | + email = 'mqcoder90@gmail.com' |
| 44 | + } |
| 45 | + } |
| 46 | + // 版本控制仓库地址 |
| 47 | + scm { |
| 48 | + url = 'https://github.com/crazyqiang/AndroidStudy' |
| 49 | + connection = 'scm:https://github.com/crazyqiang/AndroidStudy.git' |
| 50 | + developerConnection = 'scm:https://github.com/crazyqiang/AndroidStudy.git' |
| 51 | + } |
| 52 | + // 解决依赖关系 |
| 53 | + withXml { |
| 54 | + def dependenciesNode = asNode().appendNode('dependencies') |
| 55 | + project.configurations.all { configuration -> |
| 56 | + def name = configuration.name |
| 57 | + if (name != "implementation" && name != "compile" && name != "api") { |
| 58 | + return |
| 59 | + } |
| 60 | + println(configuration) |
| 61 | + configuration.dependencies.each { |
| 62 | + println(it) |
| 63 | + if (it.name == "unspecified") { |
| 64 | + // 忽略无法识别的 |
| 65 | + return |
| 66 | + } |
| 67 | + def dependencyNode = dependenciesNode.appendNode('dependency') |
| 68 | + dependencyNode.appendNode('groupId', it.group) |
| 69 | + dependencyNode.appendNode('artifactId', it.name) |
| 70 | + dependencyNode.appendNode('version', it.version) |
| 71 | + if (name == "api" || name == "compile") { |
| 72 | + dependencyNode.appendNode("scope", "compile") |
| 73 | + } else { |
| 74 | + // implementation |
| 75 | + dependencyNode.appendNode("scope", "runtime") |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + // 定义发布到哪里 |
| 84 | + repositories { |
| 85 | + maven { |
| 86 | + // 发布的位置,这里根据发布的版本区分了 SNAPSHOT 和最终版本两种情况 |
| 87 | + def releasesRepoUrl = "https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/" |
| 88 | + def snapshotsRepoUrl = "https://s01.oss.sonatype.org/content/repositories/snapshots/" |
| 89 | + url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl |
| 90 | + credentials { |
| 91 | + // 这里就是之前在 issues.sonatype.org 注册的账号 |
| 92 | + username ossrhUsername |
| 93 | + password ossrhPassword |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +signing { |
| 100 | + sign publishing.publications |
| 101 | +} |
| 102 | + |
| 103 | + |
0 commit comments