按照在專案之間簡單共享工件中描述的設定,我們處于特殊情況,我們有一個多模塊 gradle 構建,它生成不同型別的 jar,我們想在配置中宣告對這些 jar 的依賴。
dependencies {
instrumentedClasspath(project(path: ":producer", configuration: 'instrumentedJars'))
}
來自檔案的效果很好。
在專案依賴項測驗中,我有一個重現設定的專案(名稱不同,但想法相同)。
但是我在 Gradle 插件中執行此操作,并且我希望在 java 中具有相同的宣告。
DependencyHandler dependencyHandler = project.getDependencies();
// this adds a dependency to the main jar of the 'producer' project:
dependencyHandler.add("instrumentedClasspath", project.getRootProject().findProject(":producer"));
// this is not working:
dependencyHandler.add("instrumentedClasspath", project.getRootProject().findProject(":producer").getConfigurations().getByName("instrumentedJars"));
失敗:
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':printConf'.
> Could not resolve all dependencies for configuration ':instrumentedJars'.
> Cannot convert the provided notation to an object of type Dependency: configuration ':producer:instrumentedJars' artifacts.
The following types/formats are supported:
- Instances of Dependency.
- String or CharSequence values, for example 'org.gradle:gradle-core:1.0'.
- Maps, for example [group: 'org.gradle', name: 'gradle-core', version: '1.0'].
- FileCollections, for example files('some.jar', 'someOther.jar').
- Projects, for example project(':some:project:path').
- ClassPathNotation, for example gradleApi().
Comprehensive documentation on dependency notations is available in DSL reference for DependencyHandler type.
* Try:
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
uj5u.com熱心網友回復:
project(...)
內的dependencies塊是來自DependencyHandler和
path: ":producer", configuration: 'instrumentedJars'
實際上是一個地圖 {"path"=":producer", "configuration"="instrumentedJars"}。
所以這樣的事情應該在 Java 中作業:
Map<String, String> map = new HashMap<>();
map.put("path", ":producer");
map.put("configuration", "instrumentedJars");
dependencyHandler.add("instrumentedClasspath", dependencyHandler.project(map));
注意:使用 Kotlin 構建腳本時,您可以輕松查看函式的型別和宣告,并且可能更容易發現 API。所以在 Kotlinproject(...)中的dependencies塊是一個擴展方法定義為:
fun DependencyHandler.project(
path: String,
configuration: String? = null
): ProjectDependency =
uncheckedCast(
project(
if (configuration != null) mapOf("path" to path, "configuration" to configuration)
else mapOf("path" to path)
)
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/386757.html
