blob: 44be4c3de1c073cb578842bc122265c1a42e0e52 [file] [log] [blame]
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -07001import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
2import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
3import java.io.FileInputStream
4import java.io.FileNotFoundException
5import java.util.Properties
6
7buildscript {
8 repositories {
9 jcenter()
10 }
11 dependencies {
12 classpath("com.github.jengelman.gradle.plugins:shadow:4.0.4")
13 }
14}
15
16buildDir = getBuildDirectory()
17
Jeff Gaston90bd2762019-10-07 18:04:32 -040018defaultTasks = listOf("installDist", "test", "shadowJar", "createArchive", "ktlint")
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -070019
20repositories {
21 google()
22 jcenter()
Aurimas Liutikas6c2e7f82020-05-05 10:24:57 -070023 val lintRepo = project.findProperty("lintRepo") as String?
Aurimas Liutikas222577c2020-05-01 17:21:57 -070024 if (lintRepo != null) {
25 logger.warn("Building using custom $lintRepo maven repository")
26 maven {
27 url = uri(lintRepo)
28 }
29 }
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -070030}
31
32plugins {
Aurimas Liutikas51d82cc2020-04-29 15:01:30 -070033 kotlin("jvm") version "1.3.72"
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -070034 id("application")
35 id("java")
36 id("com.github.johnrengelman.shadow") version "4.0.4"
Aurimas Liutikas40d8f832019-07-26 14:23:58 -070037 id("maven-publish")
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -070038}
39
40group = "com.android"
41version = getMetalavaVersion()
42
43application {
44 mainClassName = "com.android.tools.metalava.Driver"
45 applicationDefaultJvmArgs = listOf("-ea", "-Xms2g", "-Xmx4g")
46}
47
48java {
49 sourceCompatibility = JavaVersion.VERSION_1_8
50 targetCompatibility = JavaVersion.VERSION_1_8
51}
52
53tasks.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 Liutikas6c2e7f82020-05-05 10:24:57 -070064val customLintVersion = findProperty("lintVersion") as String?
Aurimas Liutikas222577c2020-05-01 17:21:57 -070065val 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 Liutikas51d82cc2020-04-29 15:01:30 -070071val kotlinVersion: String = "1.3.72"
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -070072
73dependencies {
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
86tasks.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
94tasks.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
108fun getMetalavaVersion(): Any {
Aurimas Liutikasb5d8fc22019-07-08 10:49:40 -0700109 val versionPropertyFile = File(projectDir, "src/main/resources/version.properties")
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -0700110 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
126fun 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 */
138fun 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
146fun isBuildingOnServer(): Boolean {
147 return System.getenv("OUT_DIR") != null && System.getenv("DIST_DIR") != null
148}
149
Aurimas Liutikas0b1d7072019-08-07 06:26:24 -0700150/**
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 */
156fun getBuildId(): String {
157 return if (System.getenv("DIST_DIR") != null) File(System.getenv("DIST_DIR")).name else "0"
158}
159
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -0700160// KtLint: https://github.com/shyiko/ktlint
161
162fun Project.getKtlintConfiguration(): Configuration {
163 return configurations.findByName("ktlint") ?: configurations.create("ktlint") {
Aurimas Liutikas95563a02019-07-03 16:27:38 -0700164 val dependency = project.dependencies.create("com.pinterest:ktlint:0.33.0")
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -0700165 dependencies.add(dependency)
166 }
167}
168
169tasks.register("ktlint", JavaExec::class.java) {
170 description = "Check Kotlin code style."
171 group = "Verification"
172 classpath = getKtlintConfiguration()
Aurimas Liutikas95563a02019-07-03 16:27:38 -0700173 main = "com.pinterest.ktlint.Main"
174 args = listOf("src/**/*.kt", "build.gradle.kts")
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -0700175}
176
177tasks.register("ktlintFormat", JavaExec::class.java) {
178 description = "Fix Kotlin code style deviations."
179 group = "formatting"
180 classpath = getKtlintConfiguration()
Aurimas Liutikas95563a02019-07-03 16:27:38 -0700181 main = "com.pinterest.ktlint.Main"
182 args = listOf("-F", "src/**/*.kt", "build.gradle.kts")
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -0700183}
Aurimas Liutikas40d8f832019-07-26 14:23:58 -0700184
Aurimas Liutikas0b1d7072019-08-07 06:26:24 -0700185val libraryName = "Metalava"
186val repositoryName = "Dist"
187
Aurimas Liutikas40d8f832019-07-26 14:23:58 -0700188publishing {
189 publications {
Aurimas Liutikas0b1d7072019-08-07 06:26:24 -0700190 create<MavenPublication>(libraryName) {
Aurimas Liutikas40d8f832019-07-26 14:23:58 -0700191 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 Liutikas0b1d7072019-08-07 06:26:24 -0700214 name = repositoryName
215 url = uri("file://${getDistributionDirectory().canonicalPath}/repo/m2repository")
Aurimas Liutikas40d8f832019-07-26 14:23:58 -0700216 }
217 }
Aurimas Liutikas0b1d7072019-08-07 06:26:24 -0700218}
219
220tasks.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}