|
| 1 | +import com.fasterxml.jackson.databind.MapperFeature |
| 2 | +import com.fasterxml.jackson.databind.SerializationFeature |
| 3 | +import com.fasterxml.jackson.databind.json.JsonMapper |
| 4 | +import com.fasterxml.jackson.databind.node.ArrayNode |
| 5 | +import com.fasterxml.jackson.databind.node.JsonNodeFactory |
| 6 | +import com.fasterxml.jackson.databind.node.ObjectNode |
1 | 7 | import com.vanniktech.maven.publish.JavadocJar.Dokka
|
2 | 8 | import com.vanniktech.maven.publish.KotlinJvm
|
3 | 9 | import com.vanniktech.maven.publish.MavenPublishBaseExtension
|
4 |
| -import org.json.JSONObject |
5 | 10 |
|
6 | 11 | plugins {
|
7 | 12 | kotlin("jvm")
|
@@ -85,26 +90,30 @@ tasks.withType<GenerateModuleMetadata>().configureEach {
|
85 | 90 | doLast {
|
86 | 91 | try {
|
87 | 92 | outputFile.get().asFile.takeIf { it.exists() }?.let { moduleFile ->
|
88 |
| - val moduleJson = JSONObject(moduleFile.readText()) |
89 |
| - val variants = moduleJson.getJSONArray("variants") |
90 |
| - |
91 |
| - val shadowVariant = (0 until variants.length()) |
92 |
| - .map { variants.getJSONObject(it) } |
93 |
| - .find { it.getString("name") == "shadowRuntimeElements" } |
94 |
| - if (shadowVariant == null) { |
95 |
| - throw NoSuchElementException("could not find the `shadowRuntimeElements` variant!") |
96 |
| - } |
97 |
| - |
98 |
| - val runtimeVariant = (0 until variants.length()) |
99 |
| - .map { variants.getJSONObject(it) } |
100 |
| - .find { it.getString("name") == "runtimeElements" } |
101 |
| - if (runtimeVariant == null) { |
102 |
| - throw NoSuchElementException("could not find the `runtimeElements` variant!") |
103 |
| - } |
104 |
| - |
105 |
| - runtimeVariant.put("dependencies", shadowVariant.get("dependencies")) |
106 |
| - |
107 |
| - moduleFile.writeText(moduleJson.toString(2)) |
| 93 | + val mapper = JsonMapper.builder() |
| 94 | + .enable(SerializationFeature.INDENT_OUTPUT) |
| 95 | + .nodeFactory(JsonNodeFactory.withExactBigDecimals(true)) |
| 96 | + .configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, false) |
| 97 | + .configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, false) |
| 98 | + .build() |
| 99 | + val moduleJson = mapper.readTree(moduleFile) as ObjectNode |
| 100 | + val variants = moduleJson.get("variants") as ArrayNode |
| 101 | + |
| 102 | + val shadowVariant = variants |
| 103 | + .elements().asSequence() |
| 104 | + .map { it as ObjectNode } |
| 105 | + .find { it.get("name").asText() == "shadowRuntimeElements" } |
| 106 | + ?: throw NoSuchElementException("could not find the `shadowRuntimeElements` variant!") |
| 107 | + |
| 108 | + val runtimeVariant = variants |
| 109 | + .elements().asSequence() |
| 110 | + .map { it as ObjectNode } |
| 111 | + .find { it.get("name").asText() == "runtimeElements" } |
| 112 | + ?: throw NoSuchElementException("could not find the `runtimeElements` variant!") |
| 113 | + |
| 114 | + runtimeVariant.replace("dependencies", shadowVariant.get("dependencies")) |
| 115 | + |
| 116 | + moduleFile.writeText(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(moduleJson)) |
108 | 117 | }
|
109 | 118 | } catch (e: Exception) {
|
110 | 119 | throw GradleException("could not post-process the module metadata!", e)
|
|
0 commit comments