Aurimas Liutikas | 467bb9a | 2019-07-03 16:21:01 -0700 | [diff] [blame] | 1 | import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar |
| 2 | import org.jetbrains.kotlin.gradle.tasks.KotlinCompile |
| 3 | import java.io.FileInputStream |
| 4 | import java.io.FileNotFoundException |
| 5 | import java.util.Properties |
| 6 | |
| 7 | buildscript { |
| 8 | repositories { |
| 9 | jcenter() |
| 10 | } |
| 11 | dependencies { |
| 12 | classpath("com.github.jengelman.gradle.plugins:shadow:4.0.4") |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | buildDir = getBuildDirectory() |
| 17 | |
Jeff Gaston | 90bd276 | 2019-10-07 18:04:32 -0400 | [diff] [blame] | 18 | defaultTasks = listOf("installDist", "test", "shadowJar", "createArchive", "ktlint") |
Aurimas Liutikas | 467bb9a | 2019-07-03 16:21:01 -0700 | [diff] [blame] | 19 | |
| 20 | repositories { |
| 21 | google() |
| 22 | jcenter() |
Aurimas Liutikas | 6c2e7f8 | 2020-05-05 10:24:57 -0700 | [diff] [blame^] | 23 | val lintRepo = project.findProperty("lintRepo") as String? |
Aurimas Liutikas | 222577c | 2020-05-01 17:21:57 -0700 | [diff] [blame] | 24 | if (lintRepo != null) { |
| 25 | logger.warn("Building using custom $lintRepo maven repository") |
| 26 | maven { |
| 27 | url = uri(lintRepo) |
| 28 | } |
| 29 | } |
Aurimas Liutikas | 467bb9a | 2019-07-03 16:21:01 -0700 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | plugins { |
Aurimas Liutikas | 51d82cc | 2020-04-29 15:01:30 -0700 | [diff] [blame] | 33 | kotlin("jvm") version "1.3.72" |
Aurimas Liutikas | 467bb9a | 2019-07-03 16:21:01 -0700 | [diff] [blame] | 34 | id("application") |
| 35 | id("java") |
| 36 | id("com.github.johnrengelman.shadow") version "4.0.4" |
Aurimas Liutikas | 40d8f83 | 2019-07-26 14:23:58 -0700 | [diff] [blame] | 37 | id("maven-publish") |
Aurimas Liutikas | 467bb9a | 2019-07-03 16:21:01 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | group = "com.android" |
| 41 | version = getMetalavaVersion() |
| 42 | |
| 43 | application { |
| 44 | mainClassName = "com.android.tools.metalava.Driver" |
| 45 | applicationDefaultJvmArgs = listOf("-ea", "-Xms2g", "-Xmx4g") |
| 46 | } |
| 47 | |
| 48 | java { |
| 49 | sourceCompatibility = JavaVersion.VERSION_1_8 |
| 50 | targetCompatibility = JavaVersion.VERSION_1_8 |
| 51 | } |
| 52 | |
| 53 | tasks.withType(KotlinCompile::class.java) { |
| 54 | sourceCompatibility = "1.8" |
| 55 | targetCompatibility = "1.8" |
| 56 | |
| 57 | kotlinOptions { |
| 58 | jvmTarget = "1.8" |
| 59 | apiVersion = "1.3" |
| 60 | languageVersion = "1.3" |
| 61 | } |
| 62 | } |
| 63 | |
Aurimas Liutikas | 6c2e7f8 | 2020-05-05 10:24:57 -0700 | [diff] [blame^] | 64 | val customLintVersion = findProperty("lintVersion") as String? |
Aurimas Liutikas | 222577c | 2020-05-01 17:21:57 -0700 | [diff] [blame] | 65 | val studioVersion: String = if (customLintVersion != null) { |
| 66 | logger.warn("Building using custom $customLintVersion version of Android Lint") |
| 67 | customLintVersion |
| 68 | } else { |
| 69 | "27.1.0-alpha05" |
| 70 | } |
Aurimas Liutikas | 51d82cc | 2020-04-29 15:01:30 -0700 | [diff] [blame] | 71 | val kotlinVersion: String = "1.3.72" |
Aurimas Liutikas | 467bb9a | 2019-07-03 16:21:01 -0700 | [diff] [blame] | 72 | |
| 73 | dependencies { |
| 74 | implementation("com.android.tools.external.org-jetbrains:uast:$studioVersion") |
| 75 | implementation("com.android.tools.external.com-intellij:intellij-core:$studioVersion") |
| 76 | implementation("com.android.tools.lint:lint-api:$studioVersion") |
| 77 | implementation("com.android.tools.lint:lint-checks:$studioVersion") |
| 78 | implementation("com.android.tools.lint:lint-gradle:$studioVersion") |
| 79 | implementation("com.android.tools.lint:lint:$studioVersion") |
| 80 | implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlinVersion") |
| 81 | implementation("org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion") |
| 82 | testImplementation("com.android.tools.lint:lint-tests:$studioVersion") |
| 83 | testImplementation("junit:junit:4.11") |
| 84 | } |
| 85 | |
| 86 | tasks.withType(ShadowJar::class.java) { |
| 87 | archiveBaseName.set("metalava-full-${project.version}") |
| 88 | archiveClassifier.set(null as String?) |
| 89 | archiveVersion.set(null as String?) |
| 90 | setZip64(true) |
| 91 | destinationDirectory.set(getDistributionDirectory()) |
| 92 | } |
| 93 | |
| 94 | tasks.withType(Test::class.java) { |
| 95 | val zipTask = project.tasks.register("zipResultsOf${name.capitalize()}", Zip::class.java) { |
| 96 | destinationDirectory.set(File(getDistributionDirectory(), "host-test-reports")) |
| 97 | archiveFileName.set("metalava-tests.zip") |
| 98 | } |
| 99 | if (isBuildingOnServer()) ignoreFailures = true |
| 100 | finalizedBy(zipTask) |
| 101 | doFirst { |
| 102 | zipTask.configure { |
| 103 | from(reports.junitXml.destination) |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | fun getMetalavaVersion(): Any { |
Aurimas Liutikas | b5d8fc2 | 2019-07-08 10:49:40 -0700 | [diff] [blame] | 109 | val versionPropertyFile = File(projectDir, "src/main/resources/version.properties") |
Aurimas Liutikas | 467bb9a | 2019-07-03 16:21:01 -0700 | [diff] [blame] | 110 | if (versionPropertyFile.canRead()) { |
| 111 | val versionProps = Properties() |
| 112 | versionProps.load(FileInputStream(versionPropertyFile)) |
| 113 | val metalavaVersion = versionProps["metalavaVersion"] |
| 114 | ?: throw IllegalStateException("metalava version was not set in ${versionPropertyFile.absolutePath}") |
| 115 | return if (isBuildingOnServer()) { |
| 116 | metalavaVersion |
| 117 | } else { |
| 118 | // Local builds are not public release candidates. |
| 119 | "$metalavaVersion-SNAPSHOT" |
| 120 | } |
| 121 | } else { |
| 122 | throw FileNotFoundException("Could not read ${versionPropertyFile.absolutePath}") |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | fun getBuildDirectory(): File { |
| 127 | return if (System.getenv("OUT_DIR") != null) { |
| 128 | File(System.getenv("OUT_DIR"), "host/common/metalava") |
| 129 | } else { |
| 130 | File("../../out/host/common") |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * The build server will copy the contents of the distribution directory and make it available for |
| 136 | * download. |
| 137 | */ |
| 138 | fun getDistributionDirectory(): File { |
| 139 | return if (System.getenv("DIST_DIR") != null) { |
| 140 | File(System.getenv("DIST_DIR")) |
| 141 | } else { |
| 142 | File("../../out/dist") |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | fun isBuildingOnServer(): Boolean { |
| 147 | return System.getenv("OUT_DIR") != null && System.getenv("DIST_DIR") != null |
| 148 | } |
| 149 | |
Aurimas Liutikas | 0b1d707 | 2019-08-07 06:26:24 -0700 | [diff] [blame] | 150 | /** |
| 151 | * @return build id string for current build |
| 152 | * |
| 153 | * The build server does not pass the build id so we infer it from the last folder of the |
| 154 | * distribution directory name. |
| 155 | */ |
| 156 | fun getBuildId(): String { |
| 157 | return if (System.getenv("DIST_DIR") != null) File(System.getenv("DIST_DIR")).name else "0" |
| 158 | } |
| 159 | |
Aurimas Liutikas | 467bb9a | 2019-07-03 16:21:01 -0700 | [diff] [blame] | 160 | // KtLint: https://github.com/shyiko/ktlint |
| 161 | |
| 162 | fun Project.getKtlintConfiguration(): Configuration { |
| 163 | return configurations.findByName("ktlint") ?: configurations.create("ktlint") { |
Aurimas Liutikas | 95563a0 | 2019-07-03 16:27:38 -0700 | [diff] [blame] | 164 | val dependency = project.dependencies.create("com.pinterest:ktlint:0.33.0") |
Aurimas Liutikas | 467bb9a | 2019-07-03 16:21:01 -0700 | [diff] [blame] | 165 | dependencies.add(dependency) |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | tasks.register("ktlint", JavaExec::class.java) { |
| 170 | description = "Check Kotlin code style." |
| 171 | group = "Verification" |
| 172 | classpath = getKtlintConfiguration() |
Aurimas Liutikas | 95563a0 | 2019-07-03 16:27:38 -0700 | [diff] [blame] | 173 | main = "com.pinterest.ktlint.Main" |
| 174 | args = listOf("src/**/*.kt", "build.gradle.kts") |
Aurimas Liutikas | 467bb9a | 2019-07-03 16:21:01 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | tasks.register("ktlintFormat", JavaExec::class.java) { |
| 178 | description = "Fix Kotlin code style deviations." |
| 179 | group = "formatting" |
| 180 | classpath = getKtlintConfiguration() |
Aurimas Liutikas | 95563a0 | 2019-07-03 16:27:38 -0700 | [diff] [blame] | 181 | main = "com.pinterest.ktlint.Main" |
| 182 | args = listOf("-F", "src/**/*.kt", "build.gradle.kts") |
Aurimas Liutikas | 467bb9a | 2019-07-03 16:21:01 -0700 | [diff] [blame] | 183 | } |
Aurimas Liutikas | 40d8f83 | 2019-07-26 14:23:58 -0700 | [diff] [blame] | 184 | |
Aurimas Liutikas | 0b1d707 | 2019-08-07 06:26:24 -0700 | [diff] [blame] | 185 | val libraryName = "Metalava" |
| 186 | val repositoryName = "Dist" |
| 187 | |
Aurimas Liutikas | 40d8f83 | 2019-07-26 14:23:58 -0700 | [diff] [blame] | 188 | publishing { |
| 189 | publications { |
Aurimas Liutikas | 0b1d707 | 2019-08-07 06:26:24 -0700 | [diff] [blame] | 190 | create<MavenPublication>(libraryName) { |
Aurimas Liutikas | 40d8f83 | 2019-07-26 14:23:58 -0700 | [diff] [blame] | 191 | from(components["java"]) |
| 192 | pom { |
| 193 | licenses { |
| 194 | license { |
| 195 | name.set("The Apache License, Version 2.0") |
| 196 | url.set("http://www.apache.org/licenses/LICENSE-2.0.txt") |
| 197 | } |
| 198 | } |
| 199 | developers { |
| 200 | developer { |
| 201 | name.set("The Android Open Source Project") |
| 202 | } |
| 203 | } |
| 204 | scm { |
| 205 | connection.set("scm:git:https://android.googlesource.com/platform/tools/metalava") |
| 206 | url.set("https://android.googlesource.com/platform/tools/metalava/") |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | repositories { |
| 213 | maven { |
Aurimas Liutikas | 0b1d707 | 2019-08-07 06:26:24 -0700 | [diff] [blame] | 214 | name = repositoryName |
| 215 | url = uri("file://${getDistributionDirectory().canonicalPath}/repo/m2repository") |
Aurimas Liutikas | 40d8f83 | 2019-07-26 14:23:58 -0700 | [diff] [blame] | 216 | } |
| 217 | } |
Aurimas Liutikas | 0b1d707 | 2019-08-07 06:26:24 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | tasks.register("createArchive", Zip::class.java) { |
| 221 | description = "Create a zip of the library in a maven format" |
| 222 | group = "publishing" |
| 223 | |
| 224 | from("${getDistributionDirectory().canonicalPath}/repo") |
| 225 | archiveFileName.set("top-of-tree-m2repository-all-${getBuildId()}.zip") |
| 226 | destinationDirectory.set(getDistributionDirectory()) |
| 227 | dependsOn("publish${libraryName}PublicationTo${repositoryName}Repository") |
| 228 | } |