允许使用 token 发布到 maven repo

This commit is contained in:
tursom 2024-08-08 18:34:44 +08:00
parent 6052adebd9
commit 8e6d63617b
11 changed files with 117 additions and 27 deletions

View File

@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
distributionUrl=https://mirrors.cloud.tencent.com/gradle/gradle-8.5-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

View File

@ -56,7 +56,7 @@ dependencyResolutionManagement {
pluginManagement {
repositories {
maven {
url = uri("https://jmp.mvn.tursom.cn:20080/repository/maven-public/")
url = uri("https://mvn.tursom.cn:20080/repository/maven-public/")
}
}
}

View File

@ -3,4 +3,5 @@ dependencies {
api(project(":ts-core"))
api(group = "com.squareup.okhttp3", name = "okhttp", version = "4.12.0")
compileOnly(group = "io.netty", name = "netty-all", version = nettyVersion)
compileOnly(group = "com.google.code.gson", name = "gson", version = "2.8.9")
}

View File

@ -32,6 +32,7 @@ fun Call.Factory.delete(
}.execute()
@JvmOverloads
@JvmName("deleteWithParam")
fun Call.Factory.delete(
url: String,
param: Map<String, String>,

View File

@ -30,7 +30,7 @@ allprojects {
repositories {
maven {
url = uri("https://jmp.mvn.tursom.cn:20080/repository/maven-public/")
url = uri("https://mvn.tursom.cn:20080/repository/maven-public/")
}
}
@ -72,10 +72,9 @@ fun publishToTursom(repositoryHandler: RepositoryHandler, project: Project) {
try {
repositoryHandler.maven {
name = "tursom"
val releasesRepoUrl = project.uri("https://jmp.mvn.tursom.cn:20080/repository/maven-releases/")
val snapshotRepoUrl = project.uri("https://jmp.mvn.tursom.cn:20080/repository/maven-snapshots/")
val releasesRepoUrl = project.uri("https://mvn.tursom.cn:20080/repository/maven-releases/")
val snapshotRepoUrl = project.uri("https://mvn.tursom.cn:20080/repository/maven-snapshots/")
url = if (project.project.version.toString().endsWith("SNAPSHOT")) snapshotRepoUrl else releasesRepoUrl
println(url)
credentials {
val artifactoryUser: String = project.rootProject.ext["tursom.artifactoryUser"] as String
val artifactoryPassword: String = project.rootProject.ext["tursom.artifactoryPassword"] as String
@ -106,27 +105,47 @@ fun publishToGithub(repositoryHandler: RepositoryHandler, project: Project) {
}
fun registerPublishRepos(repositoryHandler: RepositoryHandler, project: Project) {
val repositoriesRegex = "repositories\\.[a-zA-z]*".toRegex()
val repositoriesRegex = "repositories\\..*".toRegex()
project.rootProject.properties.keys.asSequence().filter {
it matches repositoriesRegex
}.forEach {
val repositoryName = project.rootProject.ext.properties["$it.name"]?.toString() ?: it.substringAfterLast('.')
val repo = it.substringAfter(".").substringBefore('.')
val repositoryName = project.rootProject.ext.properties["repositories.$repo.name"]?.toString() ?: repo
try{
repositoryHandler.getByName(repositoryName)
return@forEach
}catch (_: Exception) {
}
try {
val artifactoryUser = project.rootProject.ext.properties["$it.artifactoryUser"].toString()
val artifactoryPassword = project.rootProject.ext.properties["$it.artifactoryPassword"].toString()
val artifactoryUser = project.rootProject.ext.properties["repositories.$repo.artifactoryUser"]?.toString()
val artifactoryPassword = project.rootProject.ext.properties["repositories.$repo.artifactoryPassword"]?.toString()
val token = project.rootProject.ext.properties["repositories.$repo.artifactoryToken"]?.toString()
repositoryHandler.maven {
name = repositoryName
val releasesRepoUrl = project.rootProject.ext.properties["$it.release"]?.let { project.uri(it.toString()) }
val snapshotRepoUrl = project.rootProject.ext.properties["$it.snapshot"]?.let { project.uri(it.toString()) }
val repoUrl = project.rootProject.ext.properties["$it.url"]?.let { project.uri(it.toString()) }
val releasesRepoUrl = project.rootProject.ext.properties["repositories.$repo.release"]?.let { project.uri(it.toString()) }
val snapshotRepoUrl = project.rootProject.ext.properties["repositories.$repo.snapshot"]?.let { project.uri(it.toString()) }
val repoUrl = project.rootProject.ext.properties["repositories.$repo.url"]?.let { project.uri(it.toString()) }
url = if (project.project.version.toString().endsWith("SNAPSHOT")
&& snapshotRepoUrl != null
) {
snapshotRepoUrl
} else releasesRepoUrl ?: repoUrl!!
credentials {
username = artifactoryUser
password = artifactoryPassword
if (artifactoryUser != null && artifactoryPassword != null) {
credentials {
username = artifactoryUser
password = artifactoryPassword
}
} else if (token != null) {
credentials(HttpHeaderCredentials::class.java) {
name = "Authorization"
value = "Bearer $token"
}
authentication {
create<HttpHeaderAuthentication>("header")
}
}
}
} catch (e: Exception) {

View File

@ -7,7 +7,7 @@ include("ts-gradle-repos")
pluginManagement {
repositories {
maven {
url = uri("https://jmp.mvn.tursom.cn:20080/repository/maven-public/")
url = uri("https://mvn.tursom.cn:20080/repository/maven-public/")
}
}
}

View File

@ -82,3 +82,9 @@ fun setProperty(target: Project, key: String, value: Any) {
val Project.ext: ExtraPropertiesExtension
get() = (this as ExtensionAware).extensions
.getByName("ext") as ExtraPropertiesExtension
fun ExtraPropertiesExtension.getOrDefault(key: String, default: Any?) = if(has(key)) {
get(key)
}else{
default
}

View File

@ -4,11 +4,14 @@ import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.artifacts.dsl.RepositoryHandler
import org.gradle.api.artifacts.repositories.PasswordCredentials
import org.gradle.api.credentials.HttpHeaderCredentials
import org.gradle.api.plugins.ExtensionAware
import org.gradle.api.plugins.ExtraPropertiesExtension
import org.gradle.api.publish.PublicationContainer
import org.gradle.api.publish.PublishingExtension
import org.gradle.api.publish.maven.MavenPublication
import org.gradle.authentication.http.HttpHeaderAuthentication
import org.gradle.internal.credentials.DefaultHttpHeaderCredentials
class PublishPlugin : Plugin<Project> {
override fun apply(target: Project) {
@ -46,9 +49,9 @@ private fun Project.createTursomPublishRepository(repositoryHandler: RepositoryH
}
repository.url = if (version.endsWith("SNAPSHOT")) {
uri("https://jmp.mvn.tursom.cn:20080/repository/maven-snapshots/")
uri("https://mvn.tursom.cn:20080/repository/maven-snapshots/")
} else {
uri("https://jmp.mvn.tursom.cn:20080/repository/maven-releases/")
uri("https://mvn.tursom.cn:20080/repository/maven-releases/")
}
repository.credentials(PasswordCredentials::class.java) { credentials ->
@ -74,10 +77,16 @@ private fun Project.scanAndCreatePublishRepository(repositoryHandler: Repository
it matches repositoriesRegex
}.forEach { repositoryName ->
try {
val artifactoryUser = rootProject.ext["$repositoryName.artifactoryUser"]?.toString()
?: throw Exception("no artifactory user found")
val artifactoryPassword = rootProject.ext["$repositoryName.artifactoryPassword"]?.toString()
?: throw Exception("no artifactory password found")
repositoryHandler.getByName(repositoryName)
return@forEach
} catch (_: Exception) {
}
try {
val artifactoryUser = rootProject.ext.getOrDefault("$repositoryName.artifactoryUser", null)?.toString()
val artifactoryPassword = rootProject.ext.getOrDefault("$repositoryName.artifactoryPassword", null)?.toString()
val token = rootProject.ext.getOrDefault("$repositoryName.token", null)?.toString()
repositoryHandler.maven { repository ->
repository.name = properties["$repository.name"]?.toString()
?: repositoryName.substringAfterLast('.')
@ -95,10 +104,22 @@ private fun Project.scanAndCreatePublishRepository(repositoryHandler: Repository
} else {
releasesRepoUrl
} ?: repoUrl ?: throw Exception("no repo found")
repository.credentials {
it.username = artifactoryUser
it.password = artifactoryPassword
if (artifactoryUser != null && artifactoryPassword != null) {
repository.credentials {
it.username = artifactoryUser
it.password = artifactoryPassword
}
} else if (token != null) {
repository.credentials(HttpHeaderCredentials::class.java) {
it.name = "Authorization"
it.value = "Bearer $token"
}
repository.authentication {
it.create("header", HttpHeaderAuthentication::class.java)
}
}
}
} catch (e: Exception) {
println(

View File

@ -41,7 +41,7 @@ fun RepositoryHandler.useTursomRepositories(
}
if (tursom) {
maven {
it.url = project.uri("https://jmp.mvn.tursom.cn:20080/repository/maven-public")
it.url = project.uri("https://mvn.tursom.cn:20080/repository/maven-public")
}
}
}

View File

@ -6,4 +6,6 @@ dependencies {
compileOnly(project(":ts-core:ts-json"))
compileOnly(group = "com.aayushatharva.brotli4j", name = "brotli4j", version = "1.7.1")
implementation(group = "org.slf4j", name = "slf4j-api", version = "1.7.32")
testImplementation("org.jetbrains.kotlin:kotlin-test")
}

View File

@ -0,0 +1,40 @@
package cn.tursom.web.router.impl
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNull
class SimpleRouterTest {
@Test
fun testAddSubRoute() {
// 创建 SimpleRouter 实例
val router = SimpleRouter<String>()
// 添加路由
router.addSubRoute("/test", "testValue", null)
// 获取路由
val result = router.get("/test")
// 验证结果
assertEquals("testValue", result.first)
}
@Test
fun testDelRoute() {
// 创建 SimpleRouter 实例
val router = SimpleRouter<String>()
// 添加路由
router.addSubRoute("/test", "testValue", null)
// 删除路由
router.delRoute("/test")
// 获取路由
val result = router.get("/test")
// 验证结果
assertNull(result.first)
}
}