publish.gradle 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. apply plugin: 'maven-publish'
  2. Properties rootLocalProperties = new Properties()
  3. def rootLocalPropertiesFile = project.rootProject.file('local.properties')
  4. if (rootLocalPropertiesFile != null && rootLocalPropertiesFile.exists()) {
  5. rootLocalProperties.load(rootLocalPropertiesFile.newDataInputStream())
  6. rootLocalProperties.each { name, value ->
  7. project.ext[name] = value
  8. }
  9. }
  10. Properties localProperties = new Properties()
  11. def localPropertiesFile = project.file('local.properties');
  12. if (localPropertiesFile != null && localPropertiesFile.exists()) {
  13. localProperties.load(localPropertiesFile.newDataInputStream())
  14. localProperties.each { name, value ->
  15. project.ext[name] = value
  16. }
  17. }
  18. def getPropertyValue(key) {
  19. return project.ext.has(key) ? project.ext.getProperty(key) : (project.rootProject.ext.has(key)?project.rootProject.ext.getProperty(key):null)
  20. }
  21. def projectGroupId = getPropertyValue('MAVEN_LIB_GROUP')
  22. def projectArtifactId = getPropertyValue("MAVEN_LIB_ARTIFACTID")
  23. def projectVersionName = getPropertyValue("MAVEN_LIB_VERSION")
  24. def projectDescription = getPropertyValue("MAVEN_LIB_DESCRIPTION")
  25. if (!projectGroupId) {
  26. println('missing parameter MAVEN_LIB_GROUP')
  27. return
  28. }
  29. if (!projectArtifactId) {
  30. println('missing parameter MAVEN_LIB_ARTIFACTID')
  31. return
  32. }
  33. if (!projectVersionName) {
  34. println('missing parameter MAVEN_LIB_VERSION')
  35. return
  36. }
  37. def isAndroidProject = project.hasProperty('android')
  38. if (isAndroidProject) {
  39. task androidJavadocs(type: Javadoc) {
  40. source = android.sourceSets.main.java.srcDirs
  41. classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
  42. }
  43. task javadocsJar(type: Jar, dependsOn: androidJavadocs) {
  44. archiveClassifier.set("javadoc")
  45. from androidJavadocs.destinationDir
  46. }
  47. task sourcesJar(type: Jar) {
  48. archiveClassifier.set("sources")
  49. from android.sourceSets.main.java.srcDirs
  50. }
  51. afterEvaluate { project ->
  52. tasks.all { Task task ->
  53. if (task.name.equalsIgnoreCase('publish')) {
  54. task.dependsOn tasks.getByName('assemble')
  55. }
  56. }
  57. }
  58. } else {
  59. task javadocsJar(type: Jar, dependsOn: javadoc) {
  60. archiveClassifier.set("javadoc")
  61. from javadoc.destinationDir
  62. }
  63. task sourcesJar(type: Jar) {
  64. archiveClassifier.set("sources")
  65. from sourceSets.main.allJava
  66. }
  67. }
  68. tasks.withType(Javadoc).all {
  69. options {
  70. encoding "UTF-8"
  71. charSet 'UTF-8'
  72. author true
  73. version true
  74. links "http://docs.oracle.com/javase/8/docs/api"
  75. if (isAndroidProject) {
  76. linksOffline "http://d.android.com/reference", "${android.sdkDirectory}/docs/reference"
  77. }
  78. failOnError = false
  79. }
  80. enabled = false
  81. }
  82. artifacts {
  83. archives javadocsJar, sourcesJar
  84. }
  85. afterEvaluate {
  86. publishing {
  87. publications {
  88. aar(MavenPublication) {
  89. groupId = projectGroupId
  90. artifactId = projectArtifactId
  91. version = projectVersionName
  92. if (isAndroidProject) {
  93. artifact(tasks.getByName("bundleReleaseAar"))
  94. } else {
  95. artifact("$buildDir/libs/${project.getName()}.jar")
  96. }
  97. // artifact javadocsJar
  98. // artifact sourcesJar
  99. pom {
  100. name = projectArtifactId
  101. description = projectDescription
  102. // If your project has a dedicated site, use its URL here
  103. withXml {
  104. def dependenciesNode = asNode().appendNode('dependencies')
  105. def scopes = []
  106. if (configurations.hasProperty("compile")) {
  107. scopes.add(configurations.compile)
  108. }
  109. if (configurations.hasProperty("api")) {
  110. scopes.add(configurations.api)
  111. }
  112. if (configurations.hasProperty("implementation")) {
  113. scopes.add(configurations.implementation)
  114. }
  115. if (configurations.hasProperty("debugImplementation")) {
  116. scopes.add(configurations.debugImplementation)
  117. }
  118. if (configurations.hasProperty("releaseImplementation")) {
  119. scopes.add(configurations.releaseImplementation)
  120. }
  121. scopes.each { scope ->
  122. scope.allDependencies.each {
  123. if (it instanceof ModuleDependency) {
  124. boolean isTransitive = ((ModuleDependency) it).transitive
  125. if (!isTransitive) {
  126. return
  127. }
  128. }
  129. if (it.group == "unspecified" || it.version == 'unspecified') {
  130. return
  131. }
  132. if (it.group && it.name && it.version) {
  133. def dependencyNode = dependenciesNode.appendNode('dependency')
  134. dependencyNode.appendNode('groupId', it.group)
  135. dependencyNode.appendNode('artifactId', it.name)
  136. dependencyNode.appendNode('version', it.version)
  137. dependencyNode.appendNode('scope', scope.name)
  138. }
  139. }
  140. }
  141. }
  142. }
  143. }
  144. }
  145. repositories {
  146. maven {
  147. name = projectArtifactId
  148. url = uri("${rootProject.projectDir}/local-repo")
  149. }
  150. }
  151. }
  152. }