aboutsummaryrefslogtreecommitdiff
path: root/development/importMaven/build.gradle.kts
blob: f43c95578e7c201943adc3782492ed35625865a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
 * Copyright 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import org.apache.maven.model.Dependency
import org.apache.maven.model.Parent
import org.apache.maven.model.Repository
import org.apache.maven.model.building.DefaultModelBuilderFactory
import org.apache.maven.model.building.DefaultModelBuildingRequest
import org.apache.maven.model.building.ModelBuildingException
import org.apache.maven.model.building.ModelBuildingRequest
import org.apache.maven.model.building.ModelSource
import org.apache.maven.model.resolution.ModelResolver
import java.security.MessageDigest
import org.gradle.api.artifacts.result.ResolvedArtifactResult
import java.io.InputStream

buildscript {
    repositories {
        jcenter()
        mavenCentral()
        google()
    }

    dependencies {
        classpath(gradleApi())
        classpath("org.apache.maven:maven-model:3.5.4")
        classpath("org.apache.maven:maven-model-builder:3.5.4")
    }
}

typealias MavenListener = (String, String, String, File) -> Unit

/**
 * A Maven module resolver which uses Gradle.
 */
class MavenModuleResolver(val project: Project, val listener: MavenListener) : ModelResolver {

    override fun resolveModel(dependency: Dependency): ModelSource? {
        return resolveModel(dependency.groupId, dependency.artifactId, dependency.version)
    }

    override fun resolveModel(parent: Parent): ModelSource? {
        return resolveModel(parent.groupId, parent.artifactId, parent.version)
    }

    override fun resolveModel(
        groupId: String,
        artifactId: String,
        version: String
    ): ModelSource? {
        val pomQuery = project.dependencies.createArtifactResolutionQuery()
        val pomQueryResult = pomQuery.forModule(groupId, artifactId, version)
            .withArtifacts(
                MavenModule::class.java,
                MavenPomArtifact::class.java
            )
            .execute()
        var result: File? = null
        for (component in pomQueryResult.resolvedComponents) {
            val pomArtifacts = component.getArtifacts(MavenPomArtifact::class.java)
            for (pomArtifact in pomArtifacts) {
                val pomFile = pomArtifact as? ResolvedArtifactResult
                if (pomFile != null) {
                    result = pomFile.file
                    listener.invoke(groupId, artifactId, version, result)
                }
            }
        }
        return object : ModelSource {
            override fun getInputStream(): InputStream {
                return result!!.inputStream()
            }

            override fun getLocation(): String {
                return result!!.absolutePath
            }
        }
    }

    override fun addRepository(repository: Repository?) {
        // We don't need to support this
    }

    override fun addRepository(repository: Repository?, replace: Boolean) {
        // We don't need to support this
    }

    override fun newCopy(): ModelResolver {
        return this
    }
}

// The output folder inside prebuilts
val prebuiltsLocation = file("../../../../prebuilts/androidx")
val internalFolder = "internal"
val externalFolder = "external"
val configurationName = "fetchArtifacts"
val fetchArtifacts = configurations.create(configurationName)
val fetchArtifactsContainer = configurations.getByName(configurationName)
// Passed in as a project property
val artifactName = project.findProperty("artifactName")

val internalArtifacts = listOf(
    "android.arch(.*)?".toRegex(),
    "com.android.support(.*)?".toRegex()
)

val potentialInternalArtifacts = listOf(
    "androidx(.*)?".toRegex()
)

// Need to exclude androidx.databinding
val forceExternal = setOf(
    ".databinding"
)

plugins {
    java
}

repositories {
    jcenter()
    mavenCentral()
    google()
}

if (artifactName != null) {
    dependencies {
        // This is the configuration container that we use to lookup the
        // transitive closure of all dependencies.
        fetchArtifacts(artifactName)
    }
}

/**
 * Returns the list of libraries that are *internal*.
 */
fun filterInternalLibraries(artifacts: Set<ResolvedArtifact>): Set<ResolvedArtifact> {
    return artifacts.filter {
        val moduleVersionId = it.moduleVersion.id
        val group = moduleVersionId.group

        for (regex in internalArtifacts) {
            val match = regex.matches(group)
            if (match) {
                return@filter regex.matches(group)
            }
        }

        for (regex in potentialInternalArtifacts) {
            val matchResult = regex.matchEntire(group)
            val match = regex.matches(group) &&
                    matchResult?.destructured?.let { (sub) ->
                        !forceExternal.contains(sub)
                    } ?: true
            if (match) {
                return@filter true
            }
        }
        false
    }.toSet()
}

/**
 * Returns the supporting files (POM, Source files) for a given artifact.
 */
fun supportingArtifacts(
    artifact: ResolvedArtifact,
    internal: Boolean = false
): List<ResolvedArtifactResult> {
    val supportingArtifacts = mutableListOf<ResolvedArtifactResult>()
    val pomQuery = project.dependencies.createArtifactResolutionQuery()
    val modelBuilderFactory = DefaultModelBuilderFactory()
    val builder = modelBuilderFactory.newInstance()
    val resolver = MavenModuleResolver(project) { groupId, artifactId, version, pomFile ->
        copyPomFile(groupId, artifactId, version, pomFile, internal)
    }
    val pomQueryResult = pomQuery.forComponents(artifact.id.componentIdentifier)
        .withArtifacts(
            MavenModule::class.java,
            MavenPomArtifact::class.java
        )
        .execute()

    for (component in pomQueryResult.resolvedComponents) {
        val pomArtifacts = component.getArtifacts(MavenPomArtifact::class.java)
        for (pomArtifact in pomArtifacts) {
            val pomFile = pomArtifact as? ResolvedArtifactResult
            if (pomFile != null) {
                try {
                    val request: ModelBuildingRequest = DefaultModelBuildingRequest()
                    request.modelResolver = resolver
                    request.pomFile = pomFile.file
                    // Turn off validations becuase there are lots of bad POM files out there.
                    request.validationLevel = ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL
                    builder.build(request).effectiveModel
                } catch (exception: ModelBuildingException) {
                    println("Error building model request for $pomArtifact")
                }
                supportingArtifacts.add(pomFile)
            }
        }
    }

    // Create a separate query for a sources. This is because, withArtifacts seems to be an AND.
    // So if artifacts only have a distributable without a source, we still want to copy the POM file.
    val sourcesQuery = project.dependencies.createArtifactResolutionQuery()
    val sourcesQueryResult = sourcesQuery.forComponents(artifact.id.componentIdentifier)
        .withArtifacts(
            MavenModule::class.java,
            SourcesArtifact::class.java
        )
        .execute()

    for (component in sourcesQueryResult.resolvedComponents) {
        val sourcesArtifacts = component.getArtifacts(SourcesArtifact::class.java)
        for (sourcesArtifact in sourcesArtifacts) {
            val sourcesFile = sourcesArtifact as? ResolvedArtifactResult
            if (sourcesFile != null) {
                supportingArtifacts.add(sourcesFile)
            }
        }
    }
    return supportingArtifacts
}

/**
 * Helps generate digests for the artifacts.
 */
fun digest(file: File, algorithm: String): File {
    val messageDigest = MessageDigest.getInstance(algorithm)
    val contents = file.readBytes()
    val digestBytes = messageDigest.digest(contents)
    val builder = StringBuilder()
    for (byte in digestBytes) {
        builder.append(String.format("%02x", byte))
    }
    val parent = System.getProperty("java.io.tmpdir")
    val outputFile = File(parent, "${file.name}.${algorithm.toLowerCase()}")
    outputFile.deleteOnExit()
    outputFile.writeText(builder.toString())
    return outputFile
}

/**
 * Copies artifacts to the right locations.
 */
fun copyArtifact(artifact: ResolvedArtifact, internal: Boolean = false) {
    val folder = if (internal) internalFolder else externalFolder
    val moduleVersionId = artifact.moduleVersion.id
    val group = moduleVersionId.group
    val groupPath = group.split(".").joinToString("/")
    val pathComponents = listOf(
        prebuiltsLocation,
        folder,
        groupPath,
        moduleVersionId.name,
        moduleVersionId.version
    )
    val location = pathComponents.joinToString("/")
    val supportingArtifacts = supportingArtifacts(artifact, internal = internal)
    // Copy main artifact
    println("Copying $artifact to $location")
    copy {
        from(
            artifact.file,
            digest(artifact.file, "MD5"),
            digest(artifact.file, "SHA1")
        )
        into(location)
    }
    // Copy supporting artifacts
    for (supportingArtifact in supportingArtifacts) {
        println("Copying $supportingArtifact to $location")
        copy {
            from(
                supportingArtifact.file,
                digest(supportingArtifact.file, "MD5"),
                digest(supportingArtifact.file, "SHA1")
            )
            into(location)
        }
    }
}

/**
 * Copies associated POM files to the right location.
 */
fun copyPomFile(
    group: String,
    name: String,
    version: String,
    pomFile: File,
    internal: Boolean = false
) {
    val folder = if (internal) internalFolder else externalFolder
    val groupPath = group.split(".").joinToString("/")
    val pathComponents = listOf(
        prebuiltsLocation,
        folder,
        groupPath,
        name,
        version
    )
    val location = pathComponents.joinToString("/")
    // Copy associated POM files.
    println("Copying ${pomFile.name} to $location")
    copy {
        from(
            pomFile,
            digest(pomFile, "MD5"),
            digest(pomFile, "SHA1")
        )
        into(location)
    }
}

tasks {
    val fetchArtifacts by creating {
        doLast {
            // Collect all the internal and external dependencies.
            // Copy the jar/aar's and their respective POM files.
            val internalLibraries =
                filterInternalLibraries(
                    fetchArtifactsContainer
                        .resolvedConfiguration
                        .resolvedArtifacts
                )

            val externalLibraries =
                fetchArtifactsContainer
                    .resolvedConfiguration
                    .resolvedArtifacts.filter {
                    val isInternal = internalLibraries.contains(it)
                    !isInternal
                }

            println("\r\nInternal Libraries")
            internalLibraries.forEach { library ->
                copyArtifact(library, internal = true)
            }

            println("\r\nExternal Libraries")
            externalLibraries.forEach { library ->
                copyArtifact(library, internal = false)
            }
            println("\r\nResolved artifacts for $artifactName.")
        }
    }
}

defaultTasks("fetchArtifacts")