|
1 | | -// Common logic shared across modules whose artifacts are provided to jCenter and Sonatype OSS. |
| 1 | +// Common logic shared across modules whose artifacts are provided to Maven Central. |
2 | 2 | // |
3 | 3 | // This script expects an extra named "deployConfig" to exist on the Project instance. |
4 | 4 | // Its type is defined within the buildSrc module of this project. |
5 | | - |
6 | | -// Access Map properties |
7 | 5 | if (!project.ext.has("deployConfig")) { |
8 | 6 | throw new IllegalStateException("Deployed module '$name' requires a 'deployConfig'") |
9 | 7 | } |
10 | 8 |
|
11 | | -final def deployConfig = project.ext.deployConfig |
12 | | -final def deployCredentials = project.ext.deployCredentials |
13 | | -final String targetPlatform = deployConfig.platform.name |
14 | | -final String groupId = deployConfig.groupId |
15 | | -final String artifactId = deployConfig.artifactId |
16 | | -final String versionNumber = deployConfig.currentVersion |
17 | | -final String license = deployConfig.license |
18 | | -final String description = deployConfig.description |
19 | | - |
20 | | -// Apply required plugins |
21 | | -apply plugin: "com.jfrog.bintray" |
22 | | - |
23 | | -if (targetPlatform == "java") { |
24 | | - // Releases |
25 | | - apply plugin: "maven" |
26 | | - // Snapshots |
27 | | - apply plugin: "maven-publish" |
28 | | -} else if (targetPlatform == "android") { |
29 | | - // Releases |
30 | | - apply plugin: "com.github.dcendents.android-maven" |
31 | | - // Snapshots |
32 | | - apply plugin: "digital.wup.android-maven-publish" |
33 | | -} |
| 9 | +final Deployed deployConfig = project.ext.deployConfig |
| 10 | +final DeployedCredentials deployCredentials = project.ext.deployCredentials |
| 11 | +final boolean isAndroid = project.plugins.findPlugin("com.android.library") != null |
34 | 12 |
|
35 | | -// ------------------------------------------------------------------------------------------------ |
36 | | -// Artifacts Configuration |
37 | | -// ------------------------------------------------------------------------------------------------ |
| 13 | +apply plugin: "maven-publish" |
| 14 | +apply plugin: "signing" |
| 15 | +apply plugin: "org.jetbrains.dokka" |
38 | 16 |
|
39 | | -// Include sources.jar archive in each release |
40 | | -if (targetPlatform == "java") { |
41 | | - task sourcesJar(type: Jar, dependsOn: classes) { |
42 | | - baseName = artifactId |
43 | | - classifier = "sources" |
44 | | - from sourceSets.main.allSource |
45 | | - } |
46 | | -} else { |
47 | | - task sourcesJar(type: Jar) { |
48 | | - baseName = artifactId |
49 | | - classifier = "sources" |
| 17 | +// Create artifact tasks |
| 18 | +task androidSourcesJar(type: Jar) { |
| 19 | + archiveClassifier.set("sources") |
| 20 | + if (isAndroid) { |
50 | 21 | from android.sourceSets.main.java.srcDirs |
| 22 | + } else { |
| 23 | + from sourceSets.main.java.srcDirs |
51 | 24 | } |
52 | 25 | } |
53 | 26 |
|
54 | | -// Include javadoc.jar archive in each release |
55 | | -if (targetPlatform == "android") { |
56 | | - apply plugin: "org.jetbrains.dokka-android" |
57 | | -} else { |
58 | | - apply plugin: "org.jetbrains.dokka" |
| 27 | +task javadocJar(type: Jar, dependsOn: dokkaJavadoc) { |
| 28 | + archiveClassifier.set("javadoc") |
| 29 | + from dokkaJavadoc.outputDirectory |
59 | 30 | } |
60 | 31 |
|
61 | | -task javadocJar(type: Jar, dependsOn: dokka) { |
62 | | - baseName = artifactId |
63 | | - classifier = "javadoc" |
64 | | - from dokka.outputDirectory |
| 32 | +artifacts { |
| 33 | + archives androidSourcesJar |
| 34 | + archives javadocJar |
65 | 35 | } |
66 | 36 |
|
67 | | -// ------------------------------------------------------------------------------------------------ |
68 | | -// Publication Configuration |
69 | | -// ------------------------------------------------------------------------------------------------ |
| 37 | +// Setup publication details |
| 38 | +group = deployConfig.groupId |
| 39 | +version = deployConfig.currentVersion |
70 | 40 |
|
71 | | -group = groupId |
72 | | -archivesBaseName = artifactId |
73 | | -version = versionNumber |
| 41 | +final def isSnapshot = version.endsWith("SNAPSHOT") |
74 | 42 |
|
75 | 43 | publishing { |
76 | 44 | publications { |
77 | | - library(MavenPublication) { p -> |
78 | | - p.from components.getByName(targetPlatform) |
79 | | - p.artifact sourcesJar |
80 | | - p.artifact javadocJar |
81 | | - p.groupId groupId |
82 | | - p.artifactId artifactId |
83 | | - p.version version |
84 | | - p.pom.withXml { |
85 | | - def root = asNode() |
86 | | - root.appendNode("description", description) |
87 | | - root.appendNode("name", artifactId) |
88 | | - root.appendNode("url", Artifacts.githubUrl) |
| 45 | + release(MavenPublication) { |
| 46 | + groupId deployConfig.groupId |
| 47 | + artifactId deployConfig.artifactId |
| 48 | + version deployConfig.currentVersion |
| 49 | + |
| 50 | + if (isAndroid) { |
| 51 | + artifact("$buildDir/outputs/aar/${project.getName()}-release.aar") |
| 52 | + } else { |
| 53 | + artifact("$buildDir/libs/${project.getName()}-${version}.jar") |
89 | 54 | } |
90 | | - } |
91 | | - } |
92 | | -} |
93 | | - |
94 | | -// Copy POM to location expected by Bintray |
95 | | -task copyPom(type: Copy) { |
96 | | - from "build/publications/library" |
97 | | - into "build/poms" |
98 | | - include "pom-default.xml" |
99 | | -} |
| 55 | + artifact androidSourcesJar |
| 56 | + artifact javadocJar |
| 57 | + |
| 58 | + pom { |
| 59 | + name = deployConfig.artifactId |
| 60 | + description = deployConfig.description |
| 61 | + url = Artifacts.githubRepo |
| 62 | + |
| 63 | + licenses { |
| 64 | + license { |
| 65 | + name = Artifacts.license |
| 66 | + url = "${Artifacts.githubUrl}/blob/main/LICENSE" |
| 67 | + } |
| 68 | + } |
100 | 69 |
|
101 | | -// ------------------------------------------------------------------------------------------------ |
102 | | -// Target Configuration |
103 | | -// ------------------------------------------------------------------------------------------------ |
| 70 | + developers { |
| 71 | + developer { |
| 72 | + id = "mannodermaus" |
| 73 | + name = "Marcel Schnelle" |
| 74 | + } |
| 75 | + } |
104 | 76 |
|
105 | | -// Unified task for snapshots & releases |
106 | | -task deploy() { |
107 | | -} |
| 77 | + scm { |
| 78 | + connection = "scm:git:${Artifacts.githubRepo}.git" |
| 79 | + developerConnection = "scm:git:ssh://github.com/${Artifacts.githubRepo}.git" |
| 80 | + url = "${Artifacts.githubUrl}/tree/main" |
| 81 | + } |
108 | 82 |
|
109 | | -project.configure(project) { |
110 | | - if (project.version.endsWith("-SNAPSHOT")) { |
111 | | - // Configure deployment of snapshot versions to Sonatype OSS |
112 | | - project.publishing { |
113 | | - repositories { |
114 | | - maven { |
115 | | - name "snapshot" |
116 | | - credentials { |
117 | | - username deployCredentials.sonatypeUser |
118 | | - password deployCredentials.sonatypePass |
| 83 | + // Include transitive dependencies |
| 84 | + withXml { |
| 85 | + def dependenciesNode = asNode().appendNode("dependencies") |
| 86 | + project.configurations.implementation.allDependencies.each { |
| 87 | + def dependencyNode = dependenciesNode.appendNode("dependency") |
| 88 | + dependencyNode.appendNode("groupId", it.group) |
| 89 | + dependencyNode.appendNode("artifactId", it.name) |
| 90 | + dependencyNode.appendNode("version", it.version) |
119 | 91 | } |
120 | | - url "https://oss.sonatype.org/content/repositories/snapshots" |
121 | 92 | } |
122 | 93 | } |
123 | 94 | } |
124 | | - project.tasks.getByName("bintrayUpload").enabled = false |
125 | | - publish.dependsOn copyPom |
126 | | - deploy.finalizedBy "publishLibraryPublicationToSnapshotRepository" |
| 95 | + } |
127 | 96 |
|
128 | | - } else { |
129 | | - // Configure deployment of release versions to Bintray |
130 | | - project.artifacts { |
131 | | - archives javadocJar |
132 | | - archives sourcesJar |
133 | | - } |
| 97 | + repositories { |
| 98 | + maven { |
| 99 | + name = "central" |
134 | 100 |
|
135 | | - project.bintray { |
136 | | - user = deployCredentials.bintrayUser |
137 | | - key = deployCredentials.bintrayKey |
138 | | - configurations = ["archives"] |
139 | | - dryRun = false |
140 | | - publish = true |
141 | | - pkg { |
142 | | - repo = "maven" |
143 | | - name = artifactId |
144 | | - desc = description |
145 | | - licenses = [license] |
146 | | - githubRepo = Artifacts.githubRepo |
147 | | - websiteUrl = Artifacts.githubUrl |
148 | | - vcsUrl = "${Artifacts.githubUrl}.git" |
149 | | - issueTrackerUrl = "${Artifacts.githubUrl}/issues" |
150 | | - publicDownloadNumbers = true |
151 | | - version { |
152 | | - name = versionNumber |
153 | | - desc = description |
154 | | - } |
| 101 | + def releasesRepoUrl = "https://oss.sonatype.org/service/local/staging/deploy/maven2/" |
| 102 | + def snapshotsRepoUrl = "https://oss.sonatype.org/content/repositories/snapshots/" |
| 103 | + url = isSnapshot ? snapshotsRepoUrl : releasesRepoUrl |
| 104 | + |
| 105 | + credentials { |
| 106 | + username deployCredentials.ossrhUsername |
| 107 | + password deployCredentials.ossrhPassword |
155 | 108 | } |
156 | 109 | } |
157 | | - // Very important! Otherwise, the POM will be overwritten & have missing dependencies |
158 | | - project.tasks.getByName("install").finalizedBy copyPom |
159 | | - deploy.finalizedBy "generatePomFileForLibraryPublication", "publish", "install", "bintrayUpload" |
160 | 110 | } |
161 | 111 | } |
| 112 | + |
| 113 | +// Setup code signing |
| 114 | +ext["signing.keyId"] = deployCredentials.signingKeyId |
| 115 | +ext["signing.password"] = deployCredentials.signingPassword |
| 116 | +ext["signing.secretKeyRingFile"] = deployCredentials.signingKeyRingFile |
| 117 | +signing { |
| 118 | + sign publishing.publications |
| 119 | +} |
| 120 | + |
| 121 | +// Setup deployment |
| 122 | +nexusStaging { |
| 123 | + packageGroup = deployConfig.groupId |
| 124 | + stagingProfileId = deployCredentials.sonatypeStagingProfileId |
| 125 | + username = deployCredentials.ossrhUsername |
| 126 | + password = deployCredentials.ossrhPassword |
| 127 | +} |
| 128 | + |
| 129 | +// Catch-all deployment task for multiple modules |
| 130 | +def deploy = tasks.maybeCreate("deploy") |
| 131 | +deploy.finalizedBy "publishReleasePublicationToCentralRepository" |
| 132 | +if (!isSnapshot) { |
| 133 | + deploy.finalizedBy "closeAndReleaseRepository" |
| 134 | +} |
0 commit comments