我们具有以下项目结构:
/lib-flavored
/lib-first-flavored
/lib-second-flavored
/lib-third
具有以下依赖性
:lib-flavored
|- :lib-first-flavored
| |- :lib-second-flavored
|- :lib-second-flavored
|- :lib-third
被添加到gradle之类的
dependencies {
implementation project(path: ':lib-first-flavored')
implementation project(path: ':lib-second-flavored')
implementation project(path: ':lib-third')
}
风味库使用如下的gradle脚本。
android {
flavorDimensions 'flavor'
productFlavors {
foo {
versionNameSuffix 'foo'
...
}
bar {
versionNameSuffix 'bar'
...
}
}
}
project.afterEvaluate {
publishing {
publications {
lib(MavenPublication) {
from components.fooRelease
groupId rootProject.ext.groupId
artifactId rootProject.ext.artifactId
version android.defaultConfig.versionName
}
libBar(MavenPublication) {
from components.barRelease
alias true
groupId rootProject.ext.groupId.replace('.foo', '.bar')
artifactId rootProject.ext.artifactId
version android.defaultConfig.versionName
}
}
}
}
虽然此设置运行良好,并且在使用gradle构建项目时正确使用了正确的依赖关系,但在发布libBar
时确实会产生错误的pom。
它使用的主要出版物是建立com.foo
组,而不是带有风格的com.bar
依赖关系:
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<!-- This module was also published with a richer model, Gradle metadata, -->
<!-- which should be used instead. Do not delete the following line which -->
<!-- is to indicate to Gradle or any Gradle module metadata file consumer -->
<!-- that they should prefer consuming it instead. -->
<!-- do_not_remove: published-with-gradle-metadata -->
<modelVersion>1.0.0</modelVersion>
<groupId>com.bar</groupId>
<artifactId>lib</artifactId>
<version>1.0.0</version>
<packaging>aar</packaging>
<dependencies>
<dependency>
<groupId>com.foo</groupId>
<artifactId>first</artifactId>
<version>1.0.0</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.foo</groupId>
<artifactId>second</artifactId>
<version>1.0.0</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.foo</groupId>
<artifactId>third</artifactId>
<version>1.0.0</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>
我可以根据当前构建的样式指示gradle选择特定的发布目标吗?
P.S。如果我进行二进制构建,我可以采用某种解决方法,因为这将使我能够如下定义依赖项。但这仅在依赖项已经发布的情况下才建立。这会导致不良的开发经验。
dependencies {
final ver = android.defaultConfig.versionName
fooApi "com.foo.lib-first:$ver"
barApi "com.bar.lib-first:$ver"
fooApi "com.foo.lib-second:$ver"
barApi "com.bar.lib-second:$ver"
api "com.foo.lib-third:$ver"
}