Maven 库的可选 Gradle 依赖项 [英] Optional Gradle dependencies for Maven libraries
问题描述
我正在开发一个 Android 库,并且仅当使用我的库的项目也包含该依赖项时才希望使用该依赖项.类似于 Picasso 对 OkHttp 的作用>.
I'm working on an Android library and would like to use a dependency only if the project using my library includes that dependency as well. Similar to what Picasso does with OkHttp.
我已经通过检查 ClassNotFoundExceptions 在主代码中解决了这个问题,但在将其部署到 Maven Central 时它仍然包含依赖项.如何生成诸如 Maven 的 <optional>true</optional>
标记之类的东西?
I already took care of this on the main code by checking for ClassNotFoundExceptions but it still includes the dependencies upon deploying it to Maven Central. How do I generate something such as Maven's <optional>true</optional>
tag?
我正在使用 gradle-mvn-push 通过 Gradle 部署我的工件.
I'm using gradle-mvn-push to deploy my artifacts via Gradle.
推荐答案
投票给 GRADLE-1749.
同时你可以使用pom.withXml
的方式来修改生成的pom文件,例如添加
标签或者修改<;范围>
值:
In the mean time you can use pom.withXml
approach to modify the generated pom file, for example to add <optional>
tags or change <scope>
values:
apply plugin: 'java'
apply plugin: 'maven-publish'
configurations {
optional
compile.extendsFrom optional
}
dependencies {
compile 'com.some.group:artifact:1.0';
optional 'com.another.group:optional-artifact:1.0'
}
publishing {
publications {
maven( MavenPublication ) {
from components.java
pom.withXml {
asNode().dependencies.dependency.findAll { xmlDep ->
// mark optional dependencies
if ( project.configurations.optional.allDependencies.findAll { dep ->
xmlDep.groupId.text() == dep.group && xmlDep.artifactId.text() == dep.name
} ) {
def xmlOptional = xmlDep.optional[ 0 ];
if ( !xmlOptional ) {
xmlOptional = xmlDep.appendNode( 'optional' )
}
xmlOptional.value = 'true';
}
// fix maven-publish issue when all maven dependencies are placed into runtime scope
if ( project.configurations.compile.allDependencies.findAll { dep ->
xmlDep.groupId.text() == dep.group && xmlDep.artifactId.text() == dep.name
} ) {
def xmlScope = xmlDep.scope[ 0 ];
if ( !xmlScope ) {
xmlScope = xmlDep.appendNode( 'scope' )
}
xmlScope.value = 'compile';
}
}
}
}
}
}
这篇关于Maven 库的可选 Gradle 依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!