-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathreflections-manifest.gradle.kts
More file actions
42 lines (36 loc) · 1.81 KB
/
Copy pathreflections-manifest.gradle.kts
File metadata and controls
42 lines (36 loc) · 1.81 KB
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
// Copyright 2021 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
import org.reflections.Reflections
import org.reflections.scanners.SubTypesScanner
import org.reflections.scanners.TypeAnnotationsScanner
import org.reflections.serializers.JsonSerializer
import org.reflections.util.ConfigurationBuilder
import java.net.URLClassLoader
tasks.register("cacheReflections") {
description = "Caches reflection output to make regular startup faster. May go stale and need cleanup at times."
val sourceSets = project.convention.getPlugin(JavaPluginConvention::class.java).sourceSets
val mainSourceSet: SourceSet = sourceSets[SourceSet.MAIN_SOURCE_SET_NAME]
inputs.files(mainSourceSet.output.classesDirs)
dependsOn(tasks.named("classes"))
outputs.upToDateWhen { tasks.named("classes").get().state.upToDate }
// TODO: output to the task's own build directory, because putting things in the
// classes directory confuses gradle caching.
val manifestFile = File(mainSourceSet.output.classesDirs.first(), "manifest.json")
outputs.file(manifestFile)
doLast {
val classPaths = mainSourceSet.compileClasspath.map { it.toURI().toURL() }
val classLoader = URLClassLoader(classPaths.toTypedArray())
try {
val reflections = Reflections(
ConfigurationBuilder()
.setSerializer(JsonSerializer())
.addClassLoader(classLoader)
// .filterInputsBy(FilterBuilder.parsePackages("+org"))
.addUrls(inputs.files.map { it.toURI().toURL() })
.setScanners(TypeAnnotationsScanner(), SubTypesScanner(false)))
reflections.save(manifestFile.toString())
} catch (e: java.net.MalformedURLException) {
logger.error("Cannot parse input to url", e)
}
}
}