publish.gradle 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. apply plugin: 'maven-publish'
  2. apply plugin: 'signing'
  3. Properties rootLocalProperties = new Properties()
  4. def rootLocalPropertiesFile = project.rootProject.file('local.properties')
  5. if (rootLocalPropertiesFile != null && rootLocalPropertiesFile.exists()) {
  6. rootLocalProperties.load(rootLocalPropertiesFile.newDataInputStream())
  7. rootLocalProperties.each { name, value ->
  8. project.ext[name] = value
  9. }
  10. }
  11. Properties localProperties = new Properties()
  12. def localPropertiesFile = project.file('local.properties');
  13. if (localPropertiesFile != null && localPropertiesFile.exists()) {
  14. localProperties.load(localPropertiesFile.newDataInputStream())
  15. localProperties.each { name, value ->
  16. project.ext[name] = value
  17. }
  18. }
  19. def getPropertyValue(key) {
  20. return project.ext.has(key) ? project.ext.getProperty(key) : (project.rootProject.ext.has(key)?project.rootProject.ext.getProperty(key):null)
  21. }
  22. def mavenUsername = getPropertyValue("MAVEN_USER_NAME")
  23. def mavenPassword = getPropertyValue("MAVEN_PASSWORD")
  24. def mavenMail = getPropertyValue("MAVEN_LIB_USER_MAIL")
  25. def projectGroupId = getPropertyValue('MAVEN_LIB_GROUP')
  26. def projectArtifactId = getPropertyValue("MAVEN_LIB_ARTIFACTID")
  27. def projectVersionName = getPropertyValue("MAVEN_LIB_VERSION")
  28. def projectDescription = getPropertyValue("MAVEN_LIB_DESCRIPTION")
  29. def projectGitUrl = getPropertyValue("MAVEN_LIB_GIT_URL")
  30. def projectLicense = getPropertyValue("MAVEN_LIB_LICENSE")
  31. def projectLicenseUrl = projectLicense ? "https://opensource.org/licenses/${projectLicense.toString().replace(" ", "-")} " : null
  32. if (!mavenUsername) {
  33. println('missing parameter MAVEN_USER_NAME')
  34. return
  35. }
  36. if (!mavenPassword) {
  37. println('missing parameter MAVEN_PASSWORD')
  38. return
  39. }
  40. if (!projectGroupId) {
  41. println('missing parameter MAVEN_LIB_GROUP')
  42. return
  43. }
  44. if (!projectArtifactId) {
  45. println('missing parameter MAVEN_LIB_ARTIFACTID')
  46. return
  47. }
  48. if (!projectVersionName) {
  49. println('missing parameter MAVEN_LIB_VERSION')
  50. return
  51. }
  52. def isAndroidProject = project.hasProperty('android')
  53. if (isAndroidProject) {
  54. task androidJavadocs(type: Javadoc) {
  55. source = android.sourceSets.main.java.srcDirs
  56. classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
  57. }
  58. task javadocsJar(type: Jar, dependsOn: androidJavadocs) {
  59. archiveClassifier.set("javadoc")
  60. from androidJavadocs.destinationDir
  61. }
  62. task sourcesJar(type: Jar) {
  63. archiveClassifier.set("sources")
  64. from android.sourceSets.main.java.srcDirs
  65. }
  66. afterEvaluate { project ->
  67. tasks.all { Task task ->
  68. if (task.name.equalsIgnoreCase('publish')) {
  69. task.dependsOn tasks.getByName('assemble')
  70. }
  71. }
  72. }
  73. } else {
  74. task javadocsJar(type: Jar, dependsOn: javadoc) {
  75. archiveClassifier.set("javadoc")
  76. from javadoc.destinationDir
  77. }
  78. task sourcesJar(type: Jar) {
  79. archiveClassifier.set("sources")
  80. from sourceSets.main.allJava
  81. }
  82. }
  83. tasks.withType(Javadoc).all {
  84. options {
  85. encoding "UTF-8"
  86. charSet 'UTF-8'
  87. author true
  88. version true
  89. links "http://docs.oracle.com/javase/8/docs/api"
  90. if (isAndroidProject) {
  91. linksOffline "http://d.android.com/reference", "${android.sdkDirectory}/docs/reference"
  92. }
  93. failOnError = false
  94. }
  95. enabled = false
  96. }
  97. artifacts {
  98. archives javadocsJar, sourcesJar
  99. }
  100. afterEvaluate {
  101. publishing {
  102. publications {
  103. aar(MavenPublication) {
  104. groupId = projectGroupId
  105. artifactId = projectArtifactId
  106. version = projectVersionName
  107. if (isAndroidProject) {
  108. artifact(tasks.getByName("bundleReleaseAar"))
  109. } else {
  110. artifact("$buildDir/libs/${project.getName()}.jar")
  111. }
  112. artifact javadocsJar
  113. artifact sourcesJar
  114. pom {
  115. name = projectArtifactId
  116. description = projectDescription
  117. // If your project has a dedicated site, use its URL here
  118. url = projectGitUrl
  119. licenses {
  120. license {
  121. name = projectLicense
  122. url = projectLicenseUrl
  123. }
  124. }
  125. developers {
  126. developer {
  127. id = mavenUsername
  128. name = mavenUsername
  129. email = mavenMail
  130. }
  131. }
  132. // Version control info, if you're using GitHub, follow the format as seen here
  133. scm {
  134. connection = "scm:git:${projectGitUrl}"
  135. developerConnection = "scm:git:${projectGitUrl}"
  136. url = projectGitUrl
  137. }
  138. withXml {
  139. def dependenciesNode = asNode().appendNode('dependencies')
  140. def scopes = [configurations.compile]
  141. if (configurations.hasProperty("api")) {
  142. scopes.add(configurations.api)
  143. }
  144. if (configurations.hasProperty("implementation")) {
  145. scopes.add(configurations.implementation)
  146. }
  147. if (configurations.hasProperty("debugImplementation")) {
  148. scopes.add(configurations.debugImplementation)
  149. }
  150. if (configurations.hasProperty("releaseImplementation")) {
  151. scopes.add(configurations.releaseImplementation)
  152. }
  153. scopes.each { scope ->
  154. scope.allDependencies.each {
  155. if (it instanceof ModuleDependency) {
  156. boolean isTransitive = ((ModuleDependency) it).transitive
  157. if (!isTransitive) {
  158. return
  159. }
  160. }
  161. if (it.group == "unspecified" || it.version == 'unspecified') {
  162. return
  163. }
  164. if (it.group && it.name && it.version) {
  165. def dependencyNode = dependenciesNode.appendNode('dependency')
  166. dependencyNode.appendNode('groupId', it.group)
  167. dependencyNode.appendNode('artifactId', it.name)
  168. dependencyNode.appendNode('version', it.version)
  169. dependencyNode.appendNode('scope', scope.name)
  170. }
  171. }
  172. }
  173. }
  174. }
  175. }
  176. }
  177. repositories {
  178. maven {
  179. name = projectArtifactId
  180. def releasesRepoUrl = "https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/"
  181. def snapshotsRepoUrl = "https://s01.oss.sonatype.org/content/repositories/snapshots/"
  182. url = projectVersionName.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
  183. credentials {
  184. username mavenUsername
  185. password mavenPassword
  186. }
  187. }
  188. }
  189. }
  190. }
  191. signing {
  192. def signingKeyId = getPropertyValue("MAVEN_SIGNING_KEY_ID")
  193. def signingKey = getPropertyValue("MAVEN_SIGNING_KEY")
  194. def signingPassword = getPropertyValue("MAVEN_SIGNING_PASSWORD")
  195. if (!signingKeyId) {
  196. println('missing parameter MAVEN_SIGNING_KEY_ID')
  197. return
  198. }
  199. if (!signingKey) {
  200. println('missing parameter MAVEN_SIGNING_KEY')
  201. return
  202. }
  203. if (!signingPassword) {
  204. println('missing parameter MAVEN_SIGNING_PASSWORD')
  205. return
  206. }
  207. useInMemoryPgpKeys(signingKeyId, signingKey, signingPassword)
  208. sign publishing.publications
  209. }