generated from DanySK/template-for-gradle-plugins
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.gradle.kts
229 lines (210 loc) · 6.86 KB
/
build.gradle.kts
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
import org.jetbrains.kotlin.config.KotlinCompilerVersion.VERSION as KOTLIN_VERSION
plugins {
`java-gradle-plugin`
alias(libs.plugins.dokka)
alias(libs.plugins.gitSemVer)
alias(libs.plugins.gradlePluginPublish)
alias(libs.plugins.jacoco.testkit)
alias(libs.plugins.kotlin.jvm)
alias(libs.plugins.kotlin.qa)
alias(libs.plugins.publishOnCentral)
alias(libs.plugins.multiJvmTesting)
alias(libs.plugins.taskTree)
}
/*
* Project information
*/
group = "org.danilopianini"
description = "Automated Quality Assurance configuration for Kotlin Projects built with Gradle"
class ProjectInfo {
val longName = "Kotlin Quality Assurance Gradle plugin"
val website = "https://github.com/DanySK/$name"
val vcsUrl = "$website.git"
val scm = "scm:git:$website.git"
val pluginImplementationClass = "$group.kotlinqa.KotlinQAPlugin"
val tags = listOf("kotlin", "static analysis", "quality assurance", "qa")
}
val info = ProjectInfo()
gitSemVer {
buildMetadataSeparator.set("-")
assignGitSemanticVersion()
}
repositories {
mavenCentral()
gradlePluginPortal()
}
tasks.create("copyToolVersions") {
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> { dependsOn(this@create) }
val destinationDir =
layout.buildDirectory.map {
file("${it.asFile.absolutePath}/resources/main/org/danilopianini/kotlinqa/")
}
val destination = destinationDir.map { File(it, "versions.properties") }
tasks.withType<PublishToMavenRepository> {
dependsOn(this@create)
doFirst {
val destinationFile = destination.get()
require(destinationFile.exists()) {
"File ${destinationFile.path} has not been generated."
}
}
}
val catalogFile = file("${rootProject.rootDir.absolutePath}/gradle/libs.versions.toml")
inputs.file(catalogFile)
outputs.file(destination)
doLast {
file(destinationDir).mkdirs()
val catalog = catalogFile.readText()
val libraries =
listOf("detekt", "jacoco", "ktlint", "pmd").joinToString("\n") { library ->
val version =
Regex("""^$library\s*=\s*"([\d\w\.\-\+]+)"\s*$""", RegexOption.MULTILINE)
.findAll(catalog)
.firstOrNull()
?.destructured
?.component1()
?: throw IllegalStateException("No version available for $library in:\n$catalog")
"$library=$version"
}
destination.get().writeText(libraries)
}
}
multiJvm {
maximumSupportedJvmVersion.set(latestJavaSupportedByGradle)
}
dependencies {
api(gradleApi())
api(gradleKotlinDsl())
api(libs.bundles.kotlin.qa)
implementation(kotlin("stdlib-jdk8"))
implementation(libs.kotlin.gradle.plugin.api)
testImplementation(libs.testkit)
testImplementation(libs.bundles.kotlin.testing)
}
/*
* The following lines are a workaround for
* https://github.com/gradle/gradle/issues/16603.
* The issue is related to the Gradle Daemon getting terminated by the Gradle Testkit,
* and the JaCoCo agent not waiting for it.
*/
inline fun <reified T : Task> Project.disableTrackState() {
tasks.withType<T>().configureEach {
doNotTrackState("Otherwise JaCoCo does not work correctly")
}
}
disableTrackState<Test>()
disableTrackState<JacocoReport>()
// Enforce Kotlin version coherence
configurations.matching { it.name != "detekt" }.all {
resolutionStrategy.eachDependency {
if (requested.group == "org.jetbrains.kotlin" && requested.name.startsWith("kotlin")) {
useVersion(KOTLIN_VERSION)
because("All Kotlin modules should use the same version, and compiler uses $KOTLIN_VERSION")
}
}
}
val (currentMajor, currentMinor) =
GradleVersion
.current()
.version
.toString()
.split('.')
.take(2)
.map { it.toInt() }
(0..currentMinor).forEach { minor ->
val variant = "$currentMajor.$minor"
val variantName = "gradle$currentMajor$minor"
java {
registerFeature(variantName) {
usingSourceSet(sourceSets.main.get())
capability(group.toString(), project.name, project.version.toString())
}
}
configurations.configureEach {
if (isCanBeConsumed && name.startsWith(variantName)) {
attributes {
attribute(GradlePluginApiVersion.GRADLE_PLUGIN_API_VERSION_ATTRIBUTE, objects.named(variant))
}
dependencies {
"${variantName}Api"(gradleApi())
"${variantName}Api"(gradleKotlinDsl())
"${variantName}Api"(libs.bundles.kotlin.qa)
"${variantName}Implementation"(kotlin("stdlib-jdk8"))
"${variantName}Implementation"(libs.kotlin.gradle.plugin.api)
}
}
}
}
kotlin {
compilerOptions {
allWarningsAsErrors = true
}
}
tasks.withType<Test> {
mustRunAfter(tasks.generateJacocoTestKitProperties)
useJUnitPlatform()
testLogging {
showStandardStreams = true
showCauses = true
showStackTraces = true
events(
*org.gradle.api.tasks.testing.logging.TestLogEvent
.values(),
)
exceptionFormat = org.gradle.api.tasks.testing.logging.TestExceptionFormat.FULL
}
}
tasks.jacocoTestReport {
reports {
// Used by Codecov.io
xml.required.set(true)
}
}
signing {
if (System.getenv()["CI"].equals("true", ignoreCase = true)) {
val signingKey: String? by project
val signingPassword: String? by project
useInMemoryPgpKeys(signingKey, signingPassword)
}
}
/*
* Publication on Maven Central and the Plugin portal
*/
publishOnCentral {
projectLongName.set(info.longName)
projectDescription.set(description ?: TODO("Missing description"))
projectUrl.set(info.website)
scmConnection.set(info.scm)
repository("https://maven.pkg.github.com/DanySK/${rootProject.name}".lowercase(), name = "github") {
user.set("danysk")
password.set(System.getenv("GITHUB_TOKEN"))
}
}
publishing {
publications {
withType<MavenPublication> {
pom {
developers {
developer {
name.set("Danilo Pianini")
email.set("[email protected]")
url.set("http://www.danilopianini.org/")
}
}
}
}
}
}
gradlePlugin {
plugins {
website.set(info.website)
vcsUrl.set(info.vcsUrl)
create("") {
id = "$group.${project.name}"
displayName = info.longName
description = project.description
implementationClass = info.pluginImplementationClass
tags.set(info.tags)
}
}
}