-
Notifications
You must be signed in to change notification settings - Fork 923
/
java-publish.gradle
139 lines (125 loc) · 5.98 KB
/
java-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
configure(projectsWithFlags('publish', 'java')) {
publishing {
publications {
jar(MavenPublication) {
from components.java
// We don't publish Gradle metadata yet so don't need to worry about these variants.
suppressPomMetadataWarningsFor('optionalApiElements')
suppressPomMetadataWarningsFor('optionalRuntimeElements')
// Publish resolved versions
versionMapping {
allVariants {
fromResolutionResult()
}
}
// Replace published jar with shaded when available. We manually create shading tasks instead of
// applying the shadow plugin itself so need to do it ourselves.
def jarOverrideFile = null
def jarOverrideTask = null
if (tasks.findByName('trimShadedJar')) {
jarOverrideFile = tasks.trimShadedJar.outJarFiles.find() as File
jarOverrideTask = tasks.trimShadedJar
} else if (tasks.findByName('shadedJar')) {
jarOverrideFile = tasks.shadedJar.archivePath
jarOverrideTask = tasks.shadedJar
}
if (jarOverrideFile != null) {
// For some reason this needs to be in afterEvaluate or dependencies are lost from the POM.
afterEvaluate {
def unshaded = artifacts.find { it.classifier == null && it.extension == 'jar' }
artifacts.remove(unshaded)
artifact(jarOverrideFile).builtBy(jarOverrideTask)
}
}
// Clean up the POM.
pom {
withXml {
def rootNode = asNode()
// Do not import 'com.linecorp.armeria:dependencyManagement'.
if (rootNode.get('dependencyManagement')) {
def dependencyManagement = rootNode.get('dependencyManagement')[0]
if (dependencyManagement.get('dependencies')) {
def dependencies = dependencyManagement.get('dependencies')[0]
dependencies.findAll {
def groupId = it.get('groupId')[0]
def artifactId = it.get('artifactId')[0]
groupId == rootProject.group && artifactId == 'dependencyManagement'
}.each {
dependencies.remove(it)
}
// Clean up empty tags.
if (dependencies.directChildren.isEmpty()) {
dependencyManagement.remove(dependencies)
if (dependencyManagement.directChildren.isEmpty()) {
rootNode.remove(dependencyManagement)
}
}
}
}
// Strip out shaded dependencies. We manually create shading tasks instead of applying
// the shadow plugin itself so need to do it ourselves.
if (rootNode.get('dependencies')) {
def dependencies = rootNode.get('dependencies')[0]
def shaded = dependencies.findAll {
def groupId = it.get('groupId')[0]
def artifactId = it.get('artifactId')[0]
return project.ext.relocations.find {
it.name == "${groupId.text()}:${artifactId.text()}"
}
}
shaded.each { dependencies.remove(it) }
}
}
}
}
}
}
}
configure(projectsWithFlags('publish')) {
publishing {
publications.each { publication ->
// Shared configuration for all publications, including BOM
def currentArtifactId = project.ext.artifactId
publication.artifactId currentArtifactId
// Generate the POM.
publication.pom {
// Write the elements required by OSSRH.
name = "${project.ext.projectName} (${currentArtifactId})"
description = "${project.ext.projectDescription} (${currentArtifactId})"
url = "${project.ext.projectUrl}"
inceptionYear = "${project.ext.inceptionYear}"
licenses {
license {
name = "${project.ext.licenseName}"
url = "${project.ext.licenseUrl}"
distribution = 'repo'
}
}
developers {
developer {
name = "${project.ext.authorName}"
email = "${project.ext.authorEmail}"
url = "${project.ext.authorUrl}"
}
}
scm {
url = "${project.ext.scmUrl}"
connection = "${project.ext.scmConnection}"
developerConnection = "${project.ext.scmDeveloperConnection}"
}
}
}
}
if (project.ext.isSigning()) {
signing {
sign publishing.publications
}
}
// A version catalog need to publish Gradle metadata together to be correctly imported externally.
if (!hasFlags('version-catalog')) {
// For now, disable Gradle metadata generation until verifying it is correctly generated.
tasks.withType(GenerateModuleMetadata) {
enabled = false
}
}
}