publish.gradle 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 = []
  141. if (configurations.hasProperty("compile")) {
  142. scopes.add(configurations.compile)
  143. }
  144. if (configurations.hasProperty("api")) {
  145. scopes.add(configurations.api)
  146. }
  147. if (configurations.hasProperty("implementation")) {
  148. scopes.add(configurations.implementation)
  149. }
  150. if (configurations.hasProperty("debugImplementation")) {
  151. scopes.add(configurations.debugImplementation)
  152. }
  153. if (configurations.hasProperty("releaseImplementation")) {
  154. scopes.add(configurations.releaseImplementation)
  155. }
  156. scopes.each { scope ->
  157. scope.allDependencies.each {
  158. if (it instanceof ModuleDependency) {
  159. boolean isTransitive = ((ModuleDependency) it).transitive
  160. if (!isTransitive) {
  161. return
  162. }
  163. }
  164. if (it.group == "unspecified" || it.version == 'unspecified') {
  165. return
  166. }
  167. if (it.group && it.name && it.version) {
  168. def dependencyNode = dependenciesNode.appendNode('dependency')
  169. dependencyNode.appendNode('groupId', it.group)
  170. dependencyNode.appendNode('artifactId', it.name)
  171. dependencyNode.appendNode('version', it.version)
  172. dependencyNode.appendNode('scope', scope.name)
  173. }
  174. }
  175. }
  176. }
  177. }
  178. }
  179. }
  180. repositories {
  181. maven {
  182. name = projectArtifactId
  183. def releasesRepoUrl = "https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/"
  184. def snapshotsRepoUrl = "https://s01.oss.sonatype.org/content/repositories/snapshots/"
  185. url = projectVersionName.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
  186. credentials {
  187. username mavenUsername
  188. password mavenPassword
  189. }
  190. }
  191. }
  192. }
  193. }
  194. signing {
  195. def signingKeyId = getPropertyValue("MAVEN_SIGNING_KEY_ID")
  196. def signingKey = getPropertyValue("MAVEN_SIGNING_KEY")
  197. def signingPassword = getPropertyValue("MAVEN_SIGNING_PASSWORD")
  198. if (!signingKeyId) {
  199. println('missing parameter MAVEN_SIGNING_KEY_ID')
  200. return
  201. }
  202. if (!signingKey) {
  203. println('missing parameter MAVEN_SIGNING_KEY')
  204. return
  205. }
  206. if (!signingPassword) {
  207. println('missing parameter MAVEN_SIGNING_PASSWORD')
  208. return
  209. }
  210. useInMemoryPgpKeys(signingKeyId, signingKey, signingPassword)
  211. sign publishing.publications
  212. }