-
Notifications
You must be signed in to change notification settings - Fork 0
/
maven-publish.gradle
159 lines (129 loc) · 4.6 KB
/
maven-publish.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
import org.gradle.api.internal.artifacts.dependencies.AbstractDependency
import org.gradle.api.internal.artifacts.dependencies.SelfResolvingDependencyInternal
apply plugin: 'maven-publish'
publishing {
repositories {
mavenLocal()
}
}
ext.getArtifactId = { SourceSet sourceSet ->
sourceSet.name == 'main' ? project.name : "$project.name-$sourceSet.name"
}
ext.addPublication = { SourceSet sourceSet ->
def sourceSetName = sourceSet.name
def sourcesJarTask = project.tasks.create(name: "${sourceSetName}SourcesJar", type: Jar) {
from sourceSet.allSource
classifier = 'sources'
}
def javaDocJarTask = project.tasks.create(name: "${sourceSetName}JavaDocJar", type: Jar) {
from javadoc
classifier = 'javadoc'
}
def jarTask = sourceSetName.equals('main') ? jar : project.tasks.create(name: "${sourceSetName}Jar", type: Jar, dependsOn: sourceSet.classesTaskName) {
from sourceSet.output
def artifactId = project.getArtifactId(sourceSet)
archiveBaseName = artifactId
}
project.publishing.publications.create("${sourceSetName}Maven", MavenPublication) {
groupId = project.group
artifactId = project.getArtifactId(sourceSet)
artifact sourcesJarTask
artifact jarTask
if (sourceSetName == 'main') {
artifact javaDocJarTask
}
pom.withXml {
def depsNode = asNode().appendNode('dependencies')
def dependencies = configurations.getByName(sourceSet.implementationConfigurationName).allDependencies
dependencies
.findAll { it.group != null && it.name != null }
.forEach {
def depNode = depsNode.appendNode('dependency')
depNode.appendNode('groupId', it.group)
depNode.appendNode('artifactId', it.name)
depNode.appendNode('version', it.version)
if (it.hasProperty('excludeRules') && !it.excludeRules.isEmpty()) {
def exclusionsNode = depNode.appendNode('exclusions')
it.excludeRules.each { rule ->
def exclusionNode = exclusionsNode.appendNode('exclusion')
exclusionNode.appendNode('groupId', rule.group)
exclusionNode.appendNode('artifactId', rule.module)
}
}
}
}
}
}
class SourceSetDependency extends AbstractDependency implements FileCollectionDependency, SelfResolvingDependencyInternal {
private final Project project
private final SourceSet sourceSet
SourceSetDependency(Project project, SourceSet sourceSet) {
this.project = project
this.sourceSet = sourceSet
}
SourceSetDependency(Project project, String sourceSetName) {
this(project, project.sourceSets.getByName(sourceSetName))
}
@Override
FileCollection getFiles() {
return sourceSet.output
}
@Override
Set<File> resolve() {
return sourceSet.output.files
}
@Override
Set<File> resolve(boolean transitive) {
return sourceSet.output.files
}
@Override
String getGroup() {
return project.group
}
@Override
String getName() {
return project.getArtifactId(sourceSet)
}
@Override
String getVersion() {
return project.version
}
@Override
boolean contentEquals(Dependency dependency) {
if (!(dependency instanceof SourceSetDependency)) {
return false
}
SourceSetDependency sourceSetDep = (SourceSetDependency)dependency
return this.project == sourceSetDep.project && this.sourceSet == sourceSetDep.sourceSet
}
@Override
Dependency copy() {
return new SourceSetDependency(project, sourceSet)
}
@Override
ComponentIdentifier getTargetComponentId() {
return null
}
@Override
TaskDependency getBuildDependencies() {
return null
}
}
def sourceSetDependency(SourceSet sourceSet) {
return new SourceSetDependency(project, sourceSet)
}
def sourceSetDependency(String sourceSet) {
return new SourceSetDependency(project, sourceSet)
}
project.ext.sourceSetDependency = this.&sourceSetDependency
project.ext.publishSourceSets = { sourceSets ->
sourceSets.findAll { it.name != 'test' && it.name != 'generated' }.each {
project.addPublication(it)
}
}
project.afterEvaluate {
if (project.hasProperty('sourceSets')) {
project.publishSourceSets(project.sourceSets)
}
}
task install(type: DefaultTask, dependsOn: publishToMavenLocal)