Skip to content

Commit e2baabf

Browse files
authored
Merge pull request #645 from jsonschema2dataclass/agp7-compat
Add AGP compat plugin.
2 parents 5122336 + bed102b commit e2baabf

File tree

9 files changed

+77
-22
lines changed

9 files changed

+77
-22
lines changed

build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import org.jsonschema2dataclass.internal.plugin.ktLintFormatVersion
44

55
plugins {
66
id("com.gradle.plugin-publish") version "1.1.0" apply false
7-
id("org.jsonschema2dataclass.internal")
7+
id("org.jsonschema2dataclass.internal.spotless")
88
if (JavaVersion.current() >= JavaVersion.VERSION_11) {
99
id("com.diffplug.spotless") version "6.14.0"
1010
}

internal/build.gradle.kts

+6-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@ plugins {
77
gradlePlugin {
88
plugins {
99
create("internal") {
10-
id = "org.jsonschema2dataclass.internal"
11-
implementationClass = "org.jsonschema2dataclass.internal.plugin.InternalPlugin"
10+
id = "org.jsonschema2dataclass.internal.spotless"
11+
implementationClass = "org.jsonschema2dataclass.internal.plugin.SpotlessPlugin"
12+
}
13+
create("agp-compat") {
14+
id = "org.jsonschema2dataclass.internal.agpcompat"
15+
implementationClass = "org.jsonschema2dataclass.internal.plugin.AGPCompat8Plugin"
1216
}
1317
}
1418
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package org.jsonschema2dataclass.internal.plugin
2+
3+
import org.gradle.api.Plugin
4+
import org.gradle.api.Project
5+
import org.gradle.api.artifacts.CacheableRule
6+
import org.gradle.api.artifacts.ComponentMetadataContext
7+
import org.gradle.api.artifacts.ComponentMetadataRule
8+
import org.gradle.api.attributes.java.TargetJvmVersion
9+
import org.gradle.kotlin.dsl.withModule
10+
import javax.inject.Inject
11+
12+
private val compatComponentsDefault = listOf(
13+
"com.android.tools.build:gradle",
14+
"com.android.tools.build:gradle-api",
15+
"com.android.tools.build:builder",
16+
"com.android.tools.build:builder-model",
17+
"com.android.tools.build:manifest-merger",
18+
)
19+
20+
class AGPCompat8Plugin : Plugin<Project> {
21+
override fun apply(project: Project) {
22+
project.afterEvaluate {
23+
dependencies.components {
24+
// This project still uses Java 8, so let's rewrite the metadata,
25+
// so that the produced jars can still be used with Java 8.
26+
// https://docs.gradle.org/current/userguide/component_metadata_rules.html
27+
for (component in compatComponentsDefault) {
28+
withModule<TargetJvmVersionRule>(component) { params(8) }
29+
}
30+
}
31+
}
32+
}
33+
}
34+
35+
/**
36+
* taken from https://github.com/TWiStErRob/net.twisterrob.gradle
37+
* licence: MIT licence
38+
*/
39+
@CacheableRule
40+
abstract class TargetJvmVersionRule @Inject constructor(
41+
private val jvmVersionOverride: Int,
42+
) : ComponentMetadataRule {
43+
44+
override fun execute(context: ComponentMetadataContext) {
45+
context.details.allVariants {
46+
attributes {
47+
attribute(TargetJvmVersion.TARGET_JVM_VERSION_ATTRIBUTE, jvmVersionOverride)
48+
}
49+
}
50+
}
51+
}

internal/src/main/kotlin/org/jsonschema2dataclass/internal/plugin/InternalPlugin.kt renamed to internal/src/main/kotlin/org/jsonschema2dataclass/internal/plugin/SpotlessPlugin.kt

+10-10
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ import org.gradle.kotlin.dsl.extra
88

99
const val ktLintFormatVersion = "0.48.2"
1010
const val googleJavaFormatVersion = "1.15.0"
11-
private const val spotlessDisable = "org.jsonschema2dataclass.internal.spotless.disable"
11+
private const val EXTRA_SPOTLESS_DISABLE = "org.jsonschema2dataclass.internal.spotless.disable"
1212

13-
@Suppress("unused")
14-
class InternalPlugin : Plugin<Project> {
13+
class SpotlessPlugin : Plugin<Project> {
1514
override fun apply(project: Project) {
1615
project.plugins.withId("com.diffplug.spotless") {
1716
applySpotless(project)
@@ -20,42 +19,43 @@ class InternalPlugin : Plugin<Project> {
2019
}
2120

2221
private fun applySpotless(project: Project) {
23-
val spotlessDisable = project.extra.has(spotlessDisable) && project.extra[spotlessDisable].toString().toBoolean()
22+
val spotlessDisable = project.extra.has(EXTRA_SPOTLESS_DISABLE) &&
23+
project.extra[EXTRA_SPOTLESS_DISABLE].toString().toBoolean()
2424
project.extensions.configure(SpotlessExtension::class.java) {
2525
if (spotlessDisable) {
2626
this.isEnforceCheck = false
2727
}
2828
kotlin {
29-
targetExclude(".idea", "**/.idea", "**/build")
29+
targetExclude(".idea/**", "**/.idea/**", "**/build")
3030
target("**/*.kt")
3131
ktlint(ktLintFormatVersion)
3232
endWithNewline()
3333
}
3434
kotlinGradle {
35-
targetExclude(".idea", "**/.idea", "**/build")
35+
targetExclude(".idea/**", "**/.idea/**", "**/build")
3636
target("**/*.kts")
3737
ktlint(ktLintFormatVersion)
3838
endWithNewline()
3939
}
4040
json {
41-
targetExclude(".idea", "**/.idea", "**/build")
41+
targetExclude(".idea/**", "**/.idea/**", "**/build")
4242
target("**/*.json")
4343
jackson()
4444
endWithNewline()
4545
}
4646
yaml {
47-
targetExclude(".idea", "**/.idea", "**/build")
47+
targetExclude(".idea/**", "**/.idea/**", "**/build")
4848
target("**/*.yaml")
4949
jackson()
5050
endWithNewline()
5151
}
5252
format("xml") {
53-
targetExclude(".idea", "**/.idea", "**/build")
53+
targetExclude(".idea/**", "**/.idea/**", "**/build")
5454
target("**/*.xml")
5555
eclipseWtp(EclipseWtpFormatterStep.XML)
5656
}
5757
java {
58-
targetExclude(".idea", "**/.idea", "**/build")
58+
targetExclude(".idea/**", "**/.idea/**", "**/build")
5959
target("**/*.java")
6060
googleJavaFormat(googleJavaFormatVersion)
6161
}

plugin-gradle/build.gradle.kts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
allprojects {
2+
tasks.withType<JavaCompile>().configureEach {
3+
this.sourceCompatibility = JavaVersion.VERSION_1_8.toString()
4+
this.targetCompatibility = JavaVersion.VERSION_1_8.toString()
5+
}
6+
}

plugin-gradle/compat/agp34/build.gradle.kts

-7
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,6 @@ repositories {
1515
mavenCentral()
1616
}
1717

18-
val provided: Configuration by configurations.creating
19-
sourceSets {
20-
main {
21-
this.compileClasspath += provided
22-
}
23-
}
24-
2518
dependencies {
2619
compileOnly(project(":plugin-gradle:processors:common"))
2720

plugin-gradle/compat/agp7/build.gradle.kts

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
plugins {
22
`kotlin-dsl`
33
`kotlin-dsl-precompiled-script-plugins`
4+
id("org.jsonschema2dataclass.internal.agpcompat")
45
}
56

67
repositories {

plugin-gradle/compat/android/build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ dependencies {
2525

2626
implementation(project(":plugin-gradle:processors:common"))
2727
implementation(project(":plugin-gradle:compat:agp34"))
28-
if (JavaVersion.current() >= JavaVersion.VERSION_11) {
28+
if (JavaVersion.current() > JavaVersion.VERSION_1_8) {
2929
implementation(project(":plugin-gradle:compat:agp7"))
3030
}
3131
}

settings.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ include(":plugin-gradle:compat:android")
66
include(":plugin-gradle:compat:agp34")
77

88
// If is for testing on Java 1.8
9-
if (JavaVersion.current() >= JavaVersion.VERSION_11) {
9+
if (JavaVersion.current() > JavaVersion.VERSION_1_8) {
1010
include(":plugin-gradle:compat:agp7")
1111
}
1212
include(":plugin-gradle:processors:common")

0 commit comments

Comments
 (0)