1+ import com.charleskorn.kaml.Yaml
2+ import com.charleskorn.kaml.decodeFromStream
13import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
2- import org.gradle.kotlin.dsl.support.uppercaseFirstChar
34import java.lang.System.getProperty
5+ import java.lang.System.getenv
46import java.net.URI
57
68group = " net.eratiem"
@@ -40,33 +42,37 @@ subprojects {
4042 }
4143}
4244
45+ // ###############
46+ // # Maven Repos #
47+ // ###############
48+ buildscript {
49+ dependencies {
50+ classpath(libs.kotlinx.serialization.core)
51+ classpath(libs.kaml)
52+ }
53+ repositories.mavenCentral()
54+ }
55+
56+ val repoList = File (" maven-repos.yml" ).takeIf (File ::exists)?.inputStream()?.use { stream ->
57+ Yaml .default.decodeFromStream<List <Map <String , String ?>>>(stream).map {
58+ MavenRepo (
59+ it[" name" ]!! ,
60+ URI (it[" url" ]!! ),
61+ it[" username" ],
62+ it[" password" ],
63+ it[" publish" ]?.toBooleanStrictOrNull() ? : false
64+ )
65+ }
66+ }
67+
4368
4469// ##############
4570// # Publishing #
4671// ##############
4772
4873publishing {
4974 repositories {
50- val repoNames = System .getProperties().filterKeys { (it as String ).startsWith(" project.publish" , true ) }
51- .map { (it.key as String ).removePrefix(" project.publish." ).substringBefore(" ." ) }.toSet()
52-
53- repoNames.forEach {
54- maven {
55- name = it.uppercaseFirstChar()
56- url = uri(getProperty(" project.publish.$it .url" ))
57-
58- getProperty(" project.publish.$it .auth-type" )?.let { authType ->
59- when (authType) {
60- else -> {
61- credentials {
62- username = " ${properties[" project.publish.$it .username" ]} "
63- password = " ${properties[" project.publish.$it .access-token" ]} "
64- }
65- }
66- }
67- }
68- }
69- }
75+ repoList?.filter { it.publish }?.forEach { createRepository(it) }
7076 }
7177}
7278
@@ -81,10 +87,7 @@ allprojects {
8187 }
8288
8389 repositories {
84- maven {
85- name = " Bit-Build | Artifactory"
86- url = URI (" https://artifactory.bit-build.de/artifactory/public" )
87- }
90+ repoList?.forEach { createRepository(it) }
8891 }
8992
9093 tasks {
@@ -136,4 +139,30 @@ fun <T> T.applyIf(condition: Boolean, block: T.() -> Unit) =
136139 apply { block() }
137140 } else {
138141 this
139- }
142+ }
143+
144+ fun RepositoryHandler.createRepository (repo : MavenRepo ) {
145+ maven {
146+ name = repo.name.replace(Regex (" [^A-Za-z0-9_\\ -. ]" ), " " ).replace(Regex (" \\ s+" ), " _" )
147+ url = repo.url
148+
149+ val envName = name.replace(Regex (" \\ ." ), " " ).uppercase()
150+ val user = repo.username ? : getenv(" ${envName} _USERNAME" )
151+ val pass = repo.password ? : getenv(" ${envName} _PASSWORD" )
152+
153+ if (! user.isNullOrBlank() || ! pass.isNullOrBlank()) {
154+ credentials {
155+ if (! user.isNullOrBlank()) username = user
156+ if (! pass.isNullOrBlank()) password = pass
157+ }
158+ }
159+ }
160+ }
161+
162+ data class MavenRepo (
163+ val name : String ,
164+ val url : URI ,
165+ val username : String? = null ,
166+ val password : String? = null ,
167+ val publish : Boolean = false
168+ )
0 commit comments