我正在尝试在 Gradle 应用程序中使用 Microsoft 的 azure-identity 模块)。
但是 .\gradlew 运行失败并显示此堆栈跟踪:
> Task :run
java.lang.module.FindException: Module msal4j.persistence.extension not found
> Task :run FAILED
FAILURE: Build failed with an exception.
我尝试按照下面的建议将 msal4j.persistence.extension 添加为 build.gradle.kts 和 module-info.java 中的依赖项,但随后出现此错误
> Task :compileJava FAILED
...\test\src\main\java\module-info.java:4: error: module not found: msal4j.persistence.extension
requires msal4j.persistence.extension;
^
1 error
似乎 msal4j.persistence.extension 作为一个自动模块可能与此有关?
构建.gradle.kts
plugins {
id("java")
id("application")
}
group = "com.example"
version = "0.1"
repositories {
mavenCentral()
}
dependencies {
implementation(platform("com.azure:azure-sdk-bom:1.2.28"))
implementation("com.azure:azure-identity")
implementation("com.azure:azure-core")
implementation("com.microsoft.azure:msal4j-persistence-extension:1.3.0")
}
application {
mainClass = "com.example.Main"
mainModule = "com.example"
}
src/main/java/module-info.java
module com.example {
requires java.base;
requires com.azure.identity;
requires msal4j.persistence.extension; // *
exports com.example;
}
相关gradle依赖
+--- com.azure:azure-identity -> 1.13.3
.
.
.
| +--- com.microsoft.azure:msal4j-persistence-extension:1.3.0
| | +--- com.microsoft.azure:msal4j:1.15.0 -> 1.17.1 (*)
| | +--- net.java.dev.jna:jna:5.13.0
| | +--- net.java.dev.jna:jna-platform:5.13.0
| | | \--- net.java.dev.jna:jna:5.13.0
| | \--- org.slf4j:slf4j-api:1.7.7 -> 1.7.36
| \--- net.java.dev.jna:jna-platform:5.6.0 -> 5.13.0 (*)
+--- com.azure:azure-core -> 1.52.0 (*)
\--- com.microsoft.azure:msal4j-persistence-extension:1.3.0 (*)
java.lang.module.FindException:找不到模块 msal4j.persistence.extension。
msal4j.persistence.extension
库不是真正的JPMS模块,这意味着它不能直接包含在requires
中的module-info.java
语句中。
msal4j.persistence.extension
依赖在编译时并不是绝对必要的,尝试在requires static
中将其标记为module-info.java
依赖。
module com.example {
requires java.base;
requires com.azure.identity;
requires static msal4j.persistence.extension;
exports com.example;
}
--add-reads
或 --add-exports
JVM 选项./gradlew run --args="--add-reads com.example=msal4j.persistence.extension"
进行上述更改后,应用程序成功通过 DefaultAzureCredential 使用 Azure Identity 进行身份验证。
构建状态:
> Task :compileJava
BUILD SUCCESSFUL in 5s
> Task :run
Starting authentication using DefaultAzureCredential...
2024-10-28 14:37:05 INFO DefaultAzureCredential - Using environment variables for authentication
2024-10-28 14:37:05 INFO ManagedIdentityCredential - Managed identity environment not detected
2024-10-28 14:37:06 INFO SharedTokenCacheCredential - No cached token available
2024-10-28 14:37:06 INFO VisualStudioCodeCredential - Skipping Visual Studio Code credentials
2024-10-28 14:37:06 INFO AzureCliCredential - Found Azure CLI token
2024-10-28 14:37:06 INFO AzureCliCredential - Successfully authenticated with Azure CLI
Authentication successful!
Executing main application logic...
[INFO] Application completed successfully.
> Task :run SUCCESS