This repository has been archived by the owner on Apr 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 288
/
publish-maven.gradle
184 lines (154 loc) · 5.19 KB
/
publish-maven.gradle
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
apply plugin: 'maven-publish'
//alias install -> publishToMavenLocal
task install(dependsOn: 'publishToMavenLocal') {}
// exclude hadoop install sub projects
def hadoopInstalls = ['cdh5', 'hadoop27', 'hdp22', 'phd21', 'phd30']
if (hadoopInstalls.contains(project.name)) {
project.tasks.findByPath("artifactoryPublish")?.enabled = false
}
def nonPublishedProjects = hadoopInstalls + 'documentation-toolchain'
def customizePom = {
description project.description
name project.name
url linkHomepage
organization {
name 'Spring by Pivotal'
url 'https://spring.io'
}
licenses {
license {
name 'The Apache Software License, Version 2.0'
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
distribution 'repo'
}
}
scm {
url linkScmUrl
connection linkScmConnection
developerConnection linkScmDevConnection
}
developers {
xdDevelopers.each { dev ->
developer {
id dev.id
name dev.name
if (dev.email) {
email dev.email
}
}
}
}
}
// This is needed to generate elements that are also defined methods in MavenPublication
customizePom.resolveStrategy = Closure.DELEGATE_FIRST
task sourceJar(type: Jar) {
from sourceSets.main.allJava
}
task javadocMaven(type: Jar, dependsOn: javadoc) {
from javadoc.destinationDir
}
def providedDep = { dep ->
dependency {
artifactId dep.name
groupId dep.group
version dep.version
scope provided
}
}
providedDep.resolveStrategy = Closure.DELEGATE_ONLY
class ProjectDependenciesVersionResolver {
Map<String, ResolvedDependency> resolvedDependencies = [:]
ProjectDependenciesVersionResolver(project) {
project.configurations.findAll {
['compile', 'runtime', 'default', 'optional', 'provided'].contains(it.name)
}.each { cfg ->
cfg.resolvedConfiguration.firstLevelModuleDependencies.each { dep ->
def existing = resolvedDependencies.get("${dep.moduleGroup}:${dep.moduleName}".toString(), dep)
if (!existing) {
existing = resolvedDependencies.put("${dep.moduleGroup}:${dep.moduleName}".toString(), dep)
}
//TODO: This may never happen, but added here just in case it does.
else {
assert existing.moduleVersion == dep.moduleVersion:
"Cannot resolve dependency version for ${project.name}. Different versions ${dep} ${existing}"
}
}
}
}
def getVersion(groupId, artifactId) {
def dep = resolvedDependencies["${groupId}:${artifactId}".toString()]
assert dep: "Unable to resolve dependency version for ${groupId}:${artifactId}"
return dep.moduleVersion
}
}
//Note setting enabled=false on publishing tasks doesn't work as expected
if (!nonPublishedProjects.contains(project.name)) {
publishing {
publications {
mavenCustom(MavenPublication) {
from components.java
artifact sourceJar {
classifier 'sources'
}
artifact javadocMaven {
classifier 'javadoc'
}
pom.withXml {
def projectDependenciesVersionResolver = new ProjectDependenciesVersionResolver(project)
asNode().version + customizePom
asNode().dependencies.'*'.each {
// Add version as the 3rd element
it.version ||
it.children().add(2,
new Node(null, 'version',
projectDependenciesVersionResolver.getVersion(it.groupId.text(), it.artifactId.text())))
}
project.configurations.optional.allDependencies.each { dep ->
def mvnDep = asNode().dependencies.'*'.find {
it.artifactId.text() == dep.name && it.groupId.text() == dep.group
}
new Node(mvnDep, 'optional', 'true')
}
project.configurations.provided.allDependencies.each { dep ->
def mvnDep = asNode().dependencies.'*'.find {
it.artifactId.text() == dep.name && it.groupId.text() == dep.group
}
if (mvnDep) {
mvnDep.scope*.replaceNode(new Node(null, 'scope', 'provided'))
} else {
asNode().dependencies + providedDep(dep)
}
}
project.configurations.compile.allDependencies.each { dep ->
// Since provided is a subset of compile or runtime, 'runtime' guard is required here to preserve
// dependencies already changed to 'provided' above
def mvnDep = asNode().dependencies.'*'.find {
it.artifactId.text() == dep.name && it.groupId.text() == dep.group && it.scope?.text() == 'runtime'
}
mvnDep?.scope*.replaceNode(new Node(null, 'scope', 'compile'))
}
//
// This plugin does not support excludes applied to configurations.
//
asNode().dependencies.'*'.each { mvnDep ->
def scope = mvnDep.scope ? mvnDep.scope.text() : 'compile'
def configuration = project.configurations."${scope}"
configuration.excludeRules.each { ex ->
// TODO: Extra credit for getting the builder style to work here (see 'providedDep' above)
if (mvnDep.exclusions) {
def exclusion = new Node(mvnDep.exclusions[0], 'exclusion', null)
new Node(exclusion, 'groupId', ex.group)
new Node(exclusion, 'artifactId', ex.module ?: '*')
} else {
def exclusions = new Node(mvnDep, 'exclusions', null)
def exclusion = new Node(exclusions, 'exclusion', null)
new Node(exclusion, 'groupId', ex.group)
new Node(exclusion, 'artifactId', ex.module ?: '*')
}
}
}
}
}
}
}
}