|
| 1 | +/* |
| 2 | + * Copyright 2013 Chris Banes |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + * |
| 17 | + * Based on: https://github.com/mcxiaoke/gradle-mvn-push/blob/master/gradle-mvn-push.gradle. |
| 18 | + * |
| 19 | + * To install in a local maven repo: |
| 20 | + * 1. In the project you want to test (not Glide), add mavenLocal() to the repositories list. |
| 21 | + * 2. In Glide, run: ./gradlew uploadArchives -PLOCAL |
| 22 | + * |
| 23 | + * For faster runs add: -x check when building Glide. |
| 24 | + */ |
| 25 | + |
| 26 | +apply plugin: 'maven' |
| 27 | +apply plugin: 'signing' |
| 28 | + |
| 29 | +version = VERSION_NAME |
| 30 | +group = GROUP |
| 31 | + |
| 32 | +static def localMavenRepo() { |
| 33 | + 'file://' + new File(System.getProperty('user.home'), '.m2/repository').absolutePath |
| 34 | +} |
| 35 | + |
| 36 | +@SuppressWarnings("GrMethodMayBeStatic") |
| 37 | +def isReleaseBuild() { |
| 38 | + return !VERSION_NAME.contains("SNAPSHOT") |
| 39 | +} |
| 40 | + |
| 41 | +def getReleaseRepositoryUrl() { |
| 42 | + return hasProperty('LOCAL') ? localMavenRepo() |
| 43 | + : hasProperty('RELEASE_REPOSITORY_URL') ? RELEASE_REPOSITORY_URL |
| 44 | + //: 'https://oss.sonatype.org/service/local/staging/deploy/maven2/' |
| 45 | + : 'https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/' |
| 46 | +} |
| 47 | + |
| 48 | +def getSnapshotRepositoryUrl() { |
| 49 | + return hasProperty('LOCAL') ? localMavenRepo() |
| 50 | + : hasProperty('SNAPSHOT_REPOSITORY_URL') ? SNAPSHOT_REPOSITORY_URL |
| 51 | + //: 'https://oss.sonatype.org/content/repositories/snapshots/' |
| 52 | + : 'https://s01.oss.sonatype.org/content/repositories/snapshots/' |
| 53 | +} |
| 54 | + |
| 55 | +def getRepositoryUsername() { |
| 56 | + return hasProperty('USERNAME') ? USERNAME : (hasProperty('NEXUS_USERNAME') ? NEXUS_USERNAME : '') |
| 57 | +} |
| 58 | + |
| 59 | +def getRepositoryPassword() { |
| 60 | + return hasProperty('PASSWORD') ? PASSWORD : (hasProperty('NEXUS_PASSWORD') ? NEXUS_PASSWORD : '') |
| 61 | +} |
| 62 | + |
| 63 | +afterEvaluate { project -> |
| 64 | + def isAndroidProject = project.plugins.hasPlugin('com.android.application') || project.plugins.hasPlugin('com.android.library') |
| 65 | + // To avoid uploading the default empty jar artifact in the project root directory, we use a custom |
| 66 | + // configuration to specify which artifacts we want to upload. |
| 67 | + uploadArchives { |
| 68 | + repositories { |
| 69 | + mavenDeployer { |
| 70 | + // allow uploading through FTP protocol with the following command: |
| 71 | + // gradle uploadArchives -PSNAPSHOT_REPOSITORY_URL=ftp://host/repo/path -PUSERNAME=uname -PPASSWORD=passwd |
| 72 | + configuration = configurations.create('deployerJars') |
| 73 | + configuration.dependencies.add dependencies.create('org.apache.maven.wagon:wagon-ftp:2.2') |
| 74 | + |
| 75 | + beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) } |
| 76 | + |
| 77 | + pom.groupId = GROUP |
| 78 | + pom.artifactId = POM_ARTIFACT_ID |
| 79 | + pom.version = VERSION_NAME |
| 80 | + |
| 81 | + repository(url: getReleaseRepositoryUrl()) { |
| 82 | + authentication(userName: getRepositoryUsername(), password: getRepositoryPassword()) |
| 83 | + } |
| 84 | + snapshotRepository(url: getSnapshotRepositoryUrl()) { |
| 85 | + authentication(userName: getRepositoryUsername(), password: getRepositoryPassword()) |
| 86 | + } |
| 87 | + |
| 88 | + pom.whenConfigured { pom -> |
| 89 | + pom.packaging = POM_PACKAGING |
| 90 | + } |
| 91 | + |
| 92 | + // Dependencies are only automatically included by the release plugin if the release |
| 93 | + // variant is built. Since we've disabled the release variant to improve build |
| 94 | + // times, we need to add the dependencies to the pom file explicitly. |
| 95 | + if (isAndroidProject) { |
| 96 | + pom.withXml { |
| 97 | + def dependenciesNode = asNode().appendNode('dependencies') |
| 98 | + |
| 99 | + project.configurations.implementation.allDependencies.each { |
| 100 | + def groupId = it.group |
| 101 | + def artifactId = it.name |
| 102 | + // If we specify an artifact id that differs from the project name, it won't |
| 103 | + // match. To avoid that, we look up the artifact id (and group) by property |
| 104 | + // for any project dependencies. |
| 105 | + // TODO: there must be a neater way to do this. |
| 106 | + if (it instanceof ProjectDependency) { |
| 107 | + def properties = it.getDependencyProject().getProperties() |
| 108 | + groupId = properties.get("GROUP") |
| 109 | + artifactId = properties.get("POM_ARTIFACT_ID") |
| 110 | + } |
| 111 | + def dependencyNode = dependenciesNode.appendNode('dependency') |
| 112 | + dependencyNode.appendNode('groupId', groupId) |
| 113 | + dependencyNode.appendNode('artifactId', artifactId) |
| 114 | + dependencyNode.appendNode('version', it.version) |
| 115 | + dependencyNode.appendNode('scope', 'compile') |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + pom.project { |
| 121 | + name = POM_NAME |
| 122 | + description = POM_DESCRIPTION |
| 123 | + url = POM_URL |
| 124 | + |
| 125 | + scm { |
| 126 | + url POM_SCM_URL |
| 127 | + connection POM_SCM_CONNECTION |
| 128 | + developerConnection POM_SCM_DEV_CONNECTION |
| 129 | + } |
| 130 | + |
| 131 | + licenses { |
| 132 | + license { |
| 133 | + name = 'Simplified BSD License' |
| 134 | + url = 'http://www.opensource.org/licenses/bsd-license' |
| 135 | + distribution = 'repo' |
| 136 | + } |
| 137 | + license { |
| 138 | + name = 'The Apache Software License, Version 2.0' |
| 139 | + url = 'http://www.apache.org/licenses/LICENSE-2.0.txt' |
| 140 | + distribution = 'repo' |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + developers { |
| 145 | + developer { |
| 146 | + id = POM_DEVELOPER_ID |
| 147 | + name = POM_DEVELOPER_NAME |
| 148 | + email = POM_DEVELOPER_EMAIL |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + signing { |
| 157 | + required { isReleaseBuild() && gradle.taskGraph.hasTask('uploadArchives') } |
| 158 | + sign configurations.archives |
| 159 | + } |
| 160 | + |
| 161 | + |
| 162 | + if (isAndroidProject) { |
| 163 | + def variants = project.android.libraryVariants.findAll { |
| 164 | + it.buildType.name.equalsIgnoreCase('debug') |
| 165 | + } |
| 166 | + |
| 167 | + def getAndroidSdkDirectory = project.android.sdkDirectory |
| 168 | + |
| 169 | + def getAndroidJar = "${getAndroidSdkDirectory}/platforms/${project.android.compileSdkVersion}/android.jar" |
| 170 | + |
| 171 | + task androidJavadocs(type: Javadoc, dependsOn: assembleDebug) { |
| 172 | + source = variants.collect { it.getJavaCompileProvider().get().source } |
| 173 | + classpath = files( |
| 174 | + getAndroidJar, |
| 175 | + project.file("build/intermediates/classes/debug") |
| 176 | + ) |
| 177 | + doFirst { |
| 178 | + classpath += files(variants.collect { it.javaCompile.classpath.files }) |
| 179 | + } |
| 180 | + options { |
| 181 | + links("http://docs.oracle.com/javase/7/docs/api/") |
| 182 | + linksOffline("http://d.android.com/reference", |
| 183 | + "${getAndroidSdkDirectory}/docs/reference") |
| 184 | + } |
| 185 | + |
| 186 | + exclude '**/R.java' |
| 187 | + } |
| 188 | + |
| 189 | + def cleanJavadocTask = task("cleanJavadocTask", type: Delete) { |
| 190 | + delete androidJavadocs.destinationDir |
| 191 | + } as Task |
| 192 | + project.clean.dependsOn(cleanJavadocTask) |
| 193 | + |
| 194 | + task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) { |
| 195 | + classifier = 'javadoc' |
| 196 | + from androidJavadocs.destinationDir |
| 197 | + baseName "${JAR_PREFIX}${project.name}${JAR_POSTFIX}" |
| 198 | + } |
| 199 | + |
| 200 | + task androidSourcesJar(type: Jar) { |
| 201 | + classifier = 'sources' |
| 202 | + from project.android.sourceSets.main.java.source |
| 203 | + baseName "${JAR_PREFIX}${project.name}${JAR_POSTFIX}" |
| 204 | + } |
| 205 | + |
| 206 | + task androidLibraryJar(type: Jar, dependsOn: compileDebugJavaWithJavac /* == variant.javaCompile */) { |
| 207 | + from compileDebugJavaWithJavac.destinationDir |
| 208 | + exclude '**/R.class' |
| 209 | + exclude '**/R$*.class' |
| 210 | + baseName "${JAR_PREFIX}${project.name}${JAR_POSTFIX}" |
| 211 | + } |
| 212 | + |
| 213 | + artifacts { |
| 214 | + archives androidLibraryJar |
| 215 | + archives androidSourcesJar |
| 216 | + archives androidJavadocsJar |
| 217 | + // This is unnecessary with a release variant because by default the release variant |
| 218 | + // includes the release aar in archives. Since we've disabled our release variants and |
| 219 | + // want to include an aar, we need to manually specify the task that produces the aar |
| 220 | + // here. |
| 221 | + archives project.tasks.bundleDebugAar |
| 222 | + } |
| 223 | + } else if (project.plugins.hasPlugin('java')) { |
| 224 | + task sourcesJar(type: Jar, dependsOn: classes) { |
| 225 | + classifier = 'sources' |
| 226 | + from sourceSets.main.allSource |
| 227 | + } |
| 228 | + |
| 229 | + task javadocsJar(type: Jar, dependsOn: javadoc) { |
| 230 | + classifier = 'javadoc' |
| 231 | + from javadoc.destinationDir |
| 232 | + } |
| 233 | + |
| 234 | + artifacts { |
| 235 | + archives sourcesJar |
| 236 | + archives javadocsJar |
| 237 | + } |
| 238 | + } |
| 239 | + logger.info("Published artifacts in ${configurations.archives}:") |
| 240 | + configurations.archives.artifacts.files.files.each { logger.info("\t$it") } |
| 241 | +} |
0 commit comments