Gradle子工程之间通过 compile project(":XXX")
这种方式依赖是非常便利的,但是这样的时候,如果用uploadArchives
上传jar到maven,生成的pom.xml是不正确的。本文将给出解决这一问题的一种方法。
解决办法就是添加一个新的配置,比如叫mvnpub,然后通过它来配置发布时pom里应当用到的依赖,在生成pom.xml时,用mvnpub替换默认的compile。代码如下:
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
|
//add configuration mvnpub to replace the default compile configuration when uploadArchives
configurations {
mvnpub
}
javadoc {
options.encoding =
"UTF-8"
}
task packageJavadoc(type: Jar, dependsOn:
'javadoc'
) {
from javadoc.destinationDir
classifier =
'javadoc'
}
task packageSources(type: Jar) {
from sourceSets.main.allSource
classifier =
'sources'
}
artifacts {
archives packageJavadoc {
type =
'javadoc'
}
archives packageSources {
type =
'sources'
}
}
uploadArchives {
repositories {
mavenDeployer {
//replace the default compile to mvnpub to prevent the broken project dependency
conf2ScopeMappings.mappings.remove(configurations.compile)
conf2ScopeMappings.addMapping(
300
, configurations.mvnpub,
"compile"
)
repository(url: rootProject[
'upload.repository'
]) {
authentication(userName:
'name'
, password:
'password'
)
}
snapshotRepository(url: rootProject[
'upload.snapshotRepository'
]) {
authentication(userName:
'name'
, password:
'password'
)
}
pom.groupId =
"me.sulong"
pom.artifactId =
"gradle-sample"
pom.version =
"1.0"
}
}
}
task printMappings << {
conf2ScopeMappings.mappings.
each
{ k, v ->
println
"configuration: ${k}"
println
"mapping: \n\tpriority: ${v.priority}\n\tscope: ${v.scope}"
println
"========================================"
}
}
|
其中printMappings任务是用来查看映射情况的。以上代码在 gradle-1.0-milestone-8a测试通过。
This work, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.