blob: 6ae19c94cd110c37c90f4ff2dc2028e7cb66b8d6 [file] [log] [blame]
Aurimas Liutikasd507ea72020-08-13 16:23:54 -07001import com.android.tools.metalava.configureBuildInfoTask
Aurimas Liutikas6d6b55b2020-04-29 10:24:34 -07002import org.gradle.api.tasks.testing.logging.TestLogEvent
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -07003import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
4import java.io.FileInputStream
5import java.io.FileNotFoundException
6import java.util.Properties
7
8buildscript {
9 repositories {
10 jcenter()
11 }
12 dependencies {
13 classpath("com.github.jengelman.gradle.plugins:shadow:4.0.4")
14 }
15}
16
Aurimas Liutikas0f980f72020-06-25 13:15:39 -070017if (JavaVersion.current() != JavaVersion.VERSION_1_8) {
18 throw GradleException("You are using Java ${JavaVersion.current()}, but this build only supports Java 8. Please set your JAVA_HOME to JDK 8")
19}
20
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -070021buildDir = getBuildDirectory()
22
Aurimas Liutikasf42c0182020-05-06 16:52:12 -070023defaultTasks = mutableListOf("installDist", "test", "createArchive", "ktlint")
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -070024
25repositories {
26 google()
27 jcenter()
Aurimas Liutikas6c2e7f82020-05-05 10:24:57 -070028 val lintRepo = project.findProperty("lintRepo") as String?
Aurimas Liutikas222577c2020-05-01 17:21:57 -070029 if (lintRepo != null) {
30 logger.warn("Building using custom $lintRepo maven repository")
31 maven {
32 url = uri(lintRepo)
33 }
34 }
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -070035}
36
37plugins {
Aurimas Liutikas51d82cc2020-04-29 15:01:30 -070038 kotlin("jvm") version "1.3.72"
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -070039 id("application")
40 id("java")
Aurimas Liutikas40d8f832019-07-26 14:23:58 -070041 id("maven-publish")
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -070042}
43
44group = "com.android"
45version = getMetalavaVersion()
46
47application {
48 mainClassName = "com.android.tools.metalava.Driver"
49 applicationDefaultJvmArgs = listOf("-ea", "-Xms2g", "-Xmx4g")
50}
51
52java {
53 sourceCompatibility = JavaVersion.VERSION_1_8
54 targetCompatibility = JavaVersion.VERSION_1_8
55}
56
57tasks.withType(KotlinCompile::class.java) {
58 sourceCompatibility = "1.8"
59 targetCompatibility = "1.8"
60
61 kotlinOptions {
62 jvmTarget = "1.8"
63 apiVersion = "1.3"
64 languageVersion = "1.3"
Aurimas Liutikasf3306a22020-05-05 14:46:08 -070065 allWarningsAsErrors = true
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -070066 }
67}
68
Aurimas Liutikas6c2e7f82020-05-05 10:24:57 -070069val customLintVersion = findProperty("lintVersion") as String?
Aurimas Liutikas222577c2020-05-01 17:21:57 -070070val studioVersion: String = if (customLintVersion != null) {
71 logger.warn("Building using custom $customLintVersion version of Android Lint")
72 customLintVersion
73} else {
Matthew Gharrity3fff9952020-07-31 11:52:15 -070074 "27.2.0-alpha07"
Aurimas Liutikas222577c2020-05-01 17:21:57 -070075}
Aurimas Liutikas51d82cc2020-04-29 15:01:30 -070076val kotlinVersion: String = "1.3.72"
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -070077
78dependencies {
79 implementation("com.android.tools.external.org-jetbrains:uast:$studioVersion")
80 implementation("com.android.tools.external.com-intellij:intellij-core:$studioVersion")
81 implementation("com.android.tools.lint:lint-api:$studioVersion")
82 implementation("com.android.tools.lint:lint-checks:$studioVersion")
83 implementation("com.android.tools.lint:lint-gradle:$studioVersion")
84 implementation("com.android.tools.lint:lint:$studioVersion")
85 implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlinVersion")
86 implementation("org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion")
87 testImplementation("com.android.tools.lint:lint-tests:$studioVersion")
88 testImplementation("junit:junit:4.11")
89}
90
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -070091tasks.withType(Test::class.java) {
Aurimas Liutikas6d6b55b2020-04-29 10:24:34 -070092 testLogging.events = hashSetOf(
93 TestLogEvent.FAILED,
94 TestLogEvent.PASSED,
95 TestLogEvent.SKIPPED,
96 TestLogEvent.STANDARD_OUT,
97 TestLogEvent.STANDARD_ERROR
98 )
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -070099 val zipTask = project.tasks.register("zipResultsOf${name.capitalize()}", Zip::class.java) {
100 destinationDirectory.set(File(getDistributionDirectory(), "host-test-reports"))
101 archiveFileName.set("metalava-tests.zip")
102 }
103 if (isBuildingOnServer()) ignoreFailures = true
104 finalizedBy(zipTask)
105 doFirst {
106 zipTask.configure {
107 from(reports.junitXml.destination)
108 }
109 }
110}
111
112fun getMetalavaVersion(): Any {
Aurimas Liutikasb5d8fc22019-07-08 10:49:40 -0700113 val versionPropertyFile = File(projectDir, "src/main/resources/version.properties")
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -0700114 if (versionPropertyFile.canRead()) {
115 val versionProps = Properties()
116 versionProps.load(FileInputStream(versionPropertyFile))
117 val metalavaVersion = versionProps["metalavaVersion"]
118 ?: throw IllegalStateException("metalava version was not set in ${versionPropertyFile.absolutePath}")
119 return if (isBuildingOnServer()) {
120 metalavaVersion
121 } else {
122 // Local builds are not public release candidates.
123 "$metalavaVersion-SNAPSHOT"
124 }
125 } else {
126 throw FileNotFoundException("Could not read ${versionPropertyFile.absolutePath}")
127 }
128}
129
130fun getBuildDirectory(): File {
131 return if (System.getenv("OUT_DIR") != null) {
132 File(System.getenv("OUT_DIR"), "host/common/metalava")
133 } else {
134 File("../../out/host/common")
135 }
136}
137
138/**
139 * The build server will copy the contents of the distribution directory and make it available for
140 * download.
141 */
142fun getDistributionDirectory(): File {
143 return if (System.getenv("DIST_DIR") != null) {
144 File(System.getenv("DIST_DIR"))
145 } else {
146 File("../../out/dist")
147 }
148}
149
150fun isBuildingOnServer(): Boolean {
151 return System.getenv("OUT_DIR") != null && System.getenv("DIST_DIR") != null
152}
153
Aurimas Liutikas0b1d7072019-08-07 06:26:24 -0700154/**
155 * @return build id string for current build
156 *
157 * The build server does not pass the build id so we infer it from the last folder of the
158 * distribution directory name.
159 */
160fun getBuildId(): String {
161 return if (System.getenv("DIST_DIR") != null) File(System.getenv("DIST_DIR")).name else "0"
162}
163
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -0700164// KtLint: https://github.com/shyiko/ktlint
165
166fun Project.getKtlintConfiguration(): Configuration {
167 return configurations.findByName("ktlint") ?: configurations.create("ktlint") {
Aurimas Liutikas95563a02019-07-03 16:27:38 -0700168 val dependency = project.dependencies.create("com.pinterest:ktlint:0.33.0")
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -0700169 dependencies.add(dependency)
170 }
171}
172
173tasks.register("ktlint", JavaExec::class.java) {
174 description = "Check Kotlin code style."
175 group = "Verification"
176 classpath = getKtlintConfiguration()
Aurimas Liutikas95563a02019-07-03 16:27:38 -0700177 main = "com.pinterest.ktlint.Main"
178 args = listOf("src/**/*.kt", "build.gradle.kts")
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -0700179}
180
181tasks.register("ktlintFormat", JavaExec::class.java) {
182 description = "Fix Kotlin code style deviations."
183 group = "formatting"
184 classpath = getKtlintConfiguration()
Aurimas Liutikas95563a02019-07-03 16:27:38 -0700185 main = "com.pinterest.ktlint.Main"
186 args = listOf("-F", "src/**/*.kt", "build.gradle.kts")
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -0700187}
Aurimas Liutikas40d8f832019-07-26 14:23:58 -0700188
Aurimas Liutikas0b1d7072019-08-07 06:26:24 -0700189val libraryName = "Metalava"
190val repositoryName = "Dist"
191
Aurimas Liutikas40d8f832019-07-26 14:23:58 -0700192publishing {
193 publications {
Aurimas Liutikas0b1d7072019-08-07 06:26:24 -0700194 create<MavenPublication>(libraryName) {
Aurimas Liutikas40d8f832019-07-26 14:23:58 -0700195 from(components["java"])
196 pom {
197 licenses {
198 license {
199 name.set("The Apache License, Version 2.0")
200 url.set("http://www.apache.org/licenses/LICENSE-2.0.txt")
201 }
202 }
203 developers {
204 developer {
205 name.set("The Android Open Source Project")
206 }
207 }
208 scm {
209 connection.set("scm:git:https://android.googlesource.com/platform/tools/metalava")
210 url.set("https://android.googlesource.com/platform/tools/metalava/")
211 }
212 }
213 }
214 }
215
216 repositories {
217 maven {
Aurimas Liutikas0b1d7072019-08-07 06:26:24 -0700218 name = repositoryName
219 url = uri("file://${getDistributionDirectory().canonicalPath}/repo/m2repository")
Aurimas Liutikas40d8f832019-07-26 14:23:58 -0700220 }
221 }
Aurimas Liutikas0b1d7072019-08-07 06:26:24 -0700222}
223
224tasks.register("createArchive", Zip::class.java) {
225 description = "Create a zip of the library in a maven format"
226 group = "publishing"
227
228 from("${getDistributionDirectory().canonicalPath}/repo")
229 archiveFileName.set("top-of-tree-m2repository-all-${getBuildId()}.zip")
230 destinationDirectory.set(getDistributionDirectory())
231 dependsOn("publish${libraryName}PublicationTo${repositoryName}Repository")
232}
Aurimas Liutikas606c4102020-05-14 16:03:11 -0700233
234// Workaround for https://github.com/gradle/gradle/issues/11717
235tasks.withType(GenerateModuleMetadata::class.java).configureEach {
236 doLast {
237 val metadata = outputFile.asFile.get()
238 var text = metadata.readText()
239 metadata.writeText(
240 text.replace(
241 "\"buildId\": .*".toRegex(),
242 "\"buildId:\": \"${getBuildId()}\"")
243 )
244 }
Aurimas Liutikasd507ea72020-08-13 16:23:54 -0700245}
246
247configureBuildInfoTask(project, getDistributionDirectory())