修改 Sonatype Nexus Repository Manager 发布 scope 为 runtime

问题描述

如果 library 依赖其他 library,则向 maven 仓库发布时,其 pom 文件会有相应的声明:

1
2
3
4
5
6
<dependency>
<groupId>com.yeuban</groupId>
<artifactId>lib_child</artifactId>
<version>1.0.0</version>
<scope>runtime</scope>
</dependency>

其中 scope 表示依赖的 library 的编译模式:

  • runtime: 表示该 library 不会参与编译,但会参与打包。对应 Android gradle 的 compileOnly
  • compile: 表示该 library 会参与编译和打包。对应 Android gradle 中的 implementationapi

而向内网 Nexus Repository Manager 发布 library 时,其默认生成的 scopecompile,哪怕 gradle 编译的配置是 compileOnly

解决方案

解决方法参考: https://www.droidship.com/posts/uploading_an_android_library_on_jcenter/

手动编写 pom 文件生成的内容,可以解决上述问题。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
uploadArchives {
repositories {
mavenDeployer {
pom.withXml {
// remove <dependencies> tag from pom file, generate it manually
def rootNode = asNode()
rootNode.remove(rootNode.dependencies)
final dependenciesNode = rootNode.appendNode('dependencies')

ext.addDependency = { Dependency dep, String scope ->
if (dep.group == null || dep.version == null || dep.name == null || dep.name ==
"unspecified") {
return
} // ignore invalid dependencies

final dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', dep.group)
dependencyNode.appendNode('artifactId', dep.name)
dependencyNode.appendNode('version', dep.version)
dependencyNode.appendNode('scope', scope)

if (!dep.transitive) {
// If this dependency is transitive, we should force exclude all its
dependencies them from the POM
final exclusionNode = dependencyNode.appendNode('exclusions').appendNode
('exclusion')
exclusionNode.appendNode('groupId', '*')
exclusionNode.appendNode('artifactId', '*')
} else if (!dep.properties.excludeRules.empty) {
// Otherwise add specified exclude rules
final exclusionNode = dependencyNode.appendNode('exclusions').appendNode
('exclusion')
dep.properties.excludeRules.each { ExcludeRule rule ->
exclusionNode.appendNode('groupId', rule.group ?: '*')
exclusionNode.appendNode('artifactId', rule.module ?: '*')
}
}
}

// List all "compile" dependencies (for old Gradle)
configurations.compile.getDependencies().each { dep -> addDependency(dep, "compile") }
// List all "api" dependencies (for new Gradle) as "compile" dependencies
configurations.api.getDependencies().each { dep -> addDependency(dep, "compile") }
// List all "implementation" dependencies (for new Gradle) as "runtime" dependencies
configurations.implementation.getDependencies().each { dep -> addDependency(dep, "runtime") }
}

pom.groupId = "com.yueban.lib"
pom.version = "1.0.0"
repository(url: mavenServer + mavenReleases) {
authentication(userName: repoUsername, password: repoPassword)
}
}

task androidSourcesJar(type: Jar) {
classifier = 'sources'
from android.sourceSets.main.java.sourceFiles
}

artifacts {
archives androidSourcesJar
}
}
}