generated from EraTiem-Network/Kotlin-Plugin-Template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.gradle.kts
316 lines (262 loc) · 9.89 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
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
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.jetbrains.kotlin.util.capitalizeDecapitalize.toLowerCaseAsciiOnly
import java.util.stream.Collectors
plugins {
val kotlinVersion: String by System.getProperties()
val shadowVersion: String by System.getProperties()
kotlin("jvm").version(kotlinVersion)
kotlin("kapt").version(kotlinVersion)
id("com.github.johnrengelman.shadow").version(shadowVersion)
id("maven-publish")
}
group = "net.eratiem"
version = "1.2.0"
repositories {
maven {
url = uri("https://artifactory.bit-build.de/artifactory/public")
// Only needed for local repos that need authentication (for example snapshot-repos)
bitBuildCredentials(this)
}
}
dependencies {
val kotlinVersion: String by System.getProperties()
val spigotApiDependency: String? by project
val paperApiDependency: String? by project
val bungeeApiDependency: String? by project
val velocityApiDependency: String? by project
val eraloggerVersion: String by project
if (kotlinVersion.isNotBlank()) compileOnly(kotlin("stdlib", kotlinVersion))
if (eraloggerVersion.isNotBlank()) compileOnly("net.eratiem", "eralogger", eraloggerVersion)
if (!spigotApiDependency.isNullOrBlank()) compileOnly("org.spigotmc", "spigot-api", spigotApiDependency)
if (!paperApiDependency.isNullOrBlank()) compileOnly("io.papermc.paper", "paper-api", paperApiDependency)
if (!bungeeApiDependency.isNullOrBlank()) compileOnly("net.md-5", "bungeecord-api", bungeeApiDependency)
if (!velocityApiDependency.isNullOrBlank()) {
compileOnly("com.velocitypowered", "velocity-api", velocityApiDependency)
kapt("com.velocitypowered", "velocity-api", velocityApiDependency)
}
}
val jarTasks: MutableSet<TaskProvider<ShadowJar>> = mutableSetOf()
tasks {
/**
* Copy Task to fill plugin.yml and bungee.yml
*/
withType<Copy> {
outputs.upToDateWhen { false }
val mainClass = "${project.group}.${project.name.toLowerCase()}.${project.properties["mainClass"]}"
val pluginDescription: String by project
val pluginDependencies = getAsYamlList(project.properties["pluginDependencies"])
val pluginSoftDependencies = getAsYamlList(project.properties["pluginSoftdependencies"])
val authors: String = getAsYamlList(project.properties["authors"])
val props: LinkedHashMap<String, String> = linkedMapOf(
"plugin_name" to project.name,
"plugin_description" to pluginDescription,
"plugin_version" to version.toString(),
"plugin_main_class" to mainClass,
"plugin_dependencies" to pluginDependencies,
"plugin_softdependencies" to pluginSoftDependencies,
"plugin_authors" to authors
)
filesMatching(setOf("plugin.yml", "bungee.yml")) {
val api = if (this.sourceName.contains("plugin")) "pluginApiVersion" else "bungeeApiVersion"
props["plugin_api_version"] = (project.properties[api] as String?) ?: ""
expand(props)
}
}
// Disable standart jar task
jar {
enabled = false
}
project.configurations.implementation.get().isCanBeResolved = true
// Register ShadowJar-Tasks with excludes
getJarTaskExcludes().forEach { (name, excludes) -> registerShadowJarTask(name, excludes) }
// Add ShadowJar Tasks as dependency to build
build {
jarTasks.forEach(this::dependsOn)
}
/**
* Create task to copy plugin to paper server
*/
create("copyPluginToServer") {
dependsOn(build)
group = "plugin"
enabled = false
outputs.upToDateWhen { false }
val serverPath: String by project
if (serverPath.isNotBlank() && File(serverPath).exists()) {
val libsDir = File("${project.buildDir.absolutePath}${File.separator}libs")
val destinationFile =
File("$serverPath${File.separator}plugins${File.separator}${rootProject.name.toLowerCase()}.jar")
val jarFiles: List<File>? = libsDir.listFiles()?.filter { it.extension == "jar" }
if (jarFiles?.size == 1) {
jarFiles[0].copyTo(
destinationFile,
true
)
enabled = destinationFile.exists()
}
}
}
/**
* Create task to run paper server
*/
create<Copy>("generateIntelliJRunConfig") {
group = "plugin"
enabled = false
from("./runConfigs")
destinationDir = File("./.idea/runConfigurations")
include("intellij.xml")
val serverPath: String? by project
serverPath?.let { path ->
if (path.isNotBlank() && File(path).exists()) {
val paperFile: File? =
File(path).listFiles()?.filter { it.name.matches("paper.*\\.jar".toRegex()) }?.get(0)
if (paperFile != null && paperFile.exists()) {
val props: LinkedHashMap<String, String> = linkedMapOf(
"server_path" to File(path).absolutePath,
"project_dir" to "\$PROJECT_DIR\$"
)
filesMatching("intellij.xml") {
expand(props)
}
enabled = true
}
}
}
}
// Compile Stuff
val javaVersion = JavaVersion.VERSION_17
withType<JavaCompile> {
options.encoding = "UTF-8"
options.release.set(javaVersion.toString().toInt())
}
java {
toolchain.languageVersion.set(JavaLanguageVersion.of(javaVersion.toString()))
}
withType<KotlinCompile> {
kotlinOptions.jvmTarget = javaVersion.toString()
}
}
/**
* Get Jar-Task excludes to generate clean jars
*/
fun getJarTaskExcludes(): Map<String, Set<String>> {
val workingPackage = "${project.group.toString().replace('.', '/')}/${
project.name.toLowerCaseAsciiOnly().replace("""[^\w\d]""".toRegex(), "")
}"
val enableSpigot: Boolean = File(projectDir, "src/main/kotlin/$workingPackage/spigot").exists()
val enablePaper: Boolean = File(projectDir, "src/main/kotlin/$workingPackage/paper").exists()
val enableBungee: Boolean = File(projectDir, "src/main/kotlin/$workingPackage/bungee").exists()
val enableVelocity: Boolean = File(projectDir, "src/main/kotlin/$workingPackage/velocity").exists()
val enableDependency: String by project
val jarTaskExcludes: MutableMap<String, Set<String>> = mutableMapOf()
if (enableSpigot) jarTaskExcludes["spigot"] = setOf(
"$workingPackage/paper/**",
"$workingPackage/bungee/**",
"$workingPackage/velociy/**",
"bungee.yml",
"velocity-plugin.json"
)
if (enablePaper) jarTaskExcludes["paper"] = setOf(
"$workingPackage/spigot/**",
"$workingPackage/bungee/**",
"$workingPackage/velocity/**",
"bungee.yml",
"velocity-plugin.json"
)
if (enableBungee) jarTaskExcludes["bungee"] = setOf(
"$workingPackage/spigot/**",
"$workingPackage/paper/**",
"$workingPackage/velocity/**",
"plugin.yml",
"velocity-plugin.json"
)
if (enableVelocity) jarTaskExcludes["velocity"] = setOf(
"$workingPackage/spigot/**",
"$workingPackage/paper/**",
"$workingPackage/bungee/**",
"plugin.yml",
"bungee.yml"
)
if (enableDependency.toBoolean()) jarTaskExcludes[""] = setOf(
"$workingPackage/spigot/**",
"$workingPackage/paper/**",
"$workingPackage/bungee/**",
"$workingPackage/velocity/**",
"plugin.yml",
"bungee.yml",
"velocity-plugin.json"
)
return jarTaskExcludes
}
publishing {
publications {
create<MavenPublication>("maven-java") {
groupId = project.group.toString()
artifactId = project.name.toLowerCase()
version = project.version.toString()
jarTasks.forEach(this::artifact)
}
}
repositories {
maven {
url = uri(
"https://artifactory.bit-build.de/artifactory/eratiem"
+ (if (project.version.toString().contains("SNAPSHOT"))
"-snapshots" else "")
)
bitBuildCredentials(this)
}
maven {
url = uri("https://maven.pkg.github.com/EraTiem-Network/${project.name}")
githubPackageCredentials(this)
}
}
}
/**
* Register ShadowJar-Task with excludes and archive name
*/
fun registerShadowJarTask(classifier: String, excludes: Set<String>) {
jarTasks.add(tasks.register<ShadowJar>("${classifier}Jar") {
group = "plugin"
enabled = true
archiveClassifier.set("")
configurations = listOf(project.configurations.implementation.get())
archiveClassifier.set(classifier)
from(sourceSets.main.get().output) {
exclude(excludes)
}
})
}
/**
* parse comma seperated lists to
*/
fun getAsYamlList(commaSeparatedList: Any?): String {
if (commaSeparatedList is String && commaSeparatedList.isNotBlank()) {
return commaSeparatedList
.replace(" ", "")
.split(",")
.stream()
.map { "\n - $it" }
.collect(Collectors.joining())
}
return ""
}
/**
* Get credentials for Bit-Build's Artifactory (https://artifactory.bit-build.de)
*/
fun bitBuildCredentials(maven: MavenArtifactRepository) {
maven.credentials {
username = System.getenv("ARTIFACTORY_USER")
password = System.getenv("ARTIFACTORY_PASS")
}
}
/**
* Get credentials for Github-Packages
*/
fun githubPackageCredentials(maven: MavenArtifactRepository) {
maven.credentials {
username = System.getenv("GITHUB_USER")
password = System.getenv("GITHUB_TOKEN")
}
}