Gradle任务报错IllegalArgumentException: identity-provider未找到
正在开发Gradle任务用于获取GraphQL Schema并保存到文件,任务需要从application-dev.yml加载配置,使用SnakeYAML解析时持续报错“identity-provider not found”。
相关代码
build.gradle.kts
// Load configuration from YAML fun loadConfig(): Map<String, String> { val yaml = Yaml() val inputStream = FileInputStream("src/main/resources/application-dev.yml") val data: Map<String, Any> = yaml.load(inputStream) println("Data loaded from YAML: $data") val customsConfig = data["customs"] as? Map<*, *> ?: run { println("Customs key not found or not a map!") throw IllegalArgumentException("customs not found") } println("Customs Config: $customsConfig") val identityProviderConfig = customsConfig["identity-provider"] as? Map<*, *> ?: run { println("Identity-provider key not found or not a map!") throw IllegalArgumentException("identity-provider not found") } println("Identity Provider Config: $identityProviderConfig") val tokenConfig = identityProviderConfig["token"] as? Map<*, *> ?: run { println("Token key not found or not a map!") throw IllegalArgumentException("token not found") } println("Token Config: $tokenConfig") return tokenConfig as Map<String, String> } // Task to fetch GraphQL schema tasks.register("fetchSchema") { doLast { try { println("Loading configuration...") val config = loadConfig() val clientId = config["client-id"] ?: throw IllegalArgumentException("client-id not found") val clientSecret = config["client-secret"] ?: throw IllegalArgumentException("client-secret not found") val token = fetchToken(clientId, clientSecret) println("Token fetched: $token") println("Fetching GraphQL Schema...") fetchGraphQLSchema("https://xxx-data.api.dev.xxx.com/graphql", token, "src/main/resources/schema2.graphql") println("Schema has been fetched successfully.") } catch (e: Exception) { println("An error occurred: ${e.message}") e.printStackTrace() } } }
application-dev.yml
customs: bridge: public-url: http://localhost:8080 security: post: user: creation: enabled: false multi-tenancy: enabled: true default: maxPoolSize: 10 minIdle: 0 connectionTimeout: 5000 idleTimeout: 20000 maxLifetime: 600000 driverClassName: oracle.jdbc.driver.OracleDriver identity-provider: token: url: https://login.dev.XXX.com/connect/token client-id: placeholder_client_id tenant-id: placeholder_tenant_id scope: placeholder_scope client-secret: placeholder_client_secret
控制台输出
> Task :fetchSchema Loading configuration... Data loaded from YAML: {server={port=8080, allow={origin=*}, tomcat={max-swallow-size=10MB}}, spring={sql={init-mode=never}, output={ansi={enabled=always}}, resources={cache-period=60000, chain={cache=true, enabled=true}}, thymeleaf={cache=false}, ... Identity-provider key not found or not a map! An error occurred: identity-provider not found
排查与解决步骤
验证YAML文件结构与缩进
YAML对缩进严格要求,必须使用空格(不能用Tab)。检查identity-provider节点是否正确嵌套在customs下,缩进层级与bridge、security等节点一致。打印完整的Customs配置内容
当前控制台输出中customsConfig被截断,无法确认其完整结构。修改代码,在获取customsConfig后打印所有键和完整内容:println("Customs Config keys: ${customsConfig.keys}") println("Full Customs Config: $customsConfig")这能直接确认
customs节点下是否存在identity-provider键,以及键名是否与代码中使用的一致(比如是否被解析为驼峰式identityProvider)。确认文件路径正确性
检查FileInputStream("src/main/resources/application-dev.yml")的路径是否正确:- 确认文件确实存在于项目根目录下的
src/main/resources中 - 检查文件名拼写(比如是否写成
application-dev.yaml而非.yml)
- 确认文件确实存在于项目根目录下的
检查SnakeYAML解析配置
默认情况下SnakeYAML不会转换键的大小写格式,但如果自定义了解析规则(比如使用Constructor或TypeDescription),可能会将kebab-case的键转为驼峰式。此时需要调整代码中的键名,或者修改SnakeYAML的解析配置保持键名原样。安全类型转换优化
为避免类型转换失败导致的异常,可以增加更多调试信息,比如在获取identity-provider前,先判断customsConfig中该键的类型:println("Type of 'identity-provider' in customs: ${customsConfig["identity-provider"]?.javaClass}")
内容的提问来源于stack exchange,提问作者Japkutija

