11apply plugin : ' maven-publish'
2+ apply plugin : ' signing'
23
34tasks. withType(JavaCompile ). configureEach {
45 options. encoding = " UTF-8"
@@ -18,30 +19,163 @@ tasks.register("javadocJarLocal", Jar) {
1819 from tasks. named(" javadoc" )
1920}
2021
22+ def releaseVersion = (project. findProperty(" TRACEFIX_VERSION" )
23+ ?: project. findProperty(" libraryVersion" )
24+ ?: " 0.0.6" ). toString()
25+ def debugVersion = (project. findProperty(" TRACEFIX_VERSION_DEBUG" )
26+ ?: releaseVersion). toString()
27+ def publishGroupId = (project. findProperty(" publishedGroupId" ) ?: " io.github.gracker" ). toString()
28+ def publishArtifactId = (project. findProperty(" artifact" ) ?: project. name). toString()
29+
30+ def ossrhUsername = project. findProperty(" ossrhUsername" ) ?: System . getenv(" OSSRH_USERNAME" )
31+ def ossrhPassword = project. findProperty(" ossrhPassword" ) ?: System . getenv(" OSSRH_PASSWORD" )
32+ def signingKey = project. findProperty(" signingKey" ) ?: System . getenv(" SIGNING_KEY" )
33+ def signingPassword = project. findProperty(" signingPassword" ) ?: System . getenv(" SIGNING_PASSWORD" )
34+ def signingKeyId = project. findProperty(" signingKeyId" ) ?: System . getenv(" SIGNING_KEY_ID" )
35+
36+ def pomLibraryName = (project. findProperty(" libraryName" ) ?: " TraceFix" ). toString()
37+ def pomDescription = (project. findProperty(" libraryDescription" ) ?: " Auto add Systrace tag while building apk" ). toString()
38+ def pomSiteUrl = (project. findProperty(" siteUrl" ) ?: " https://github.com/Gracker/TraceFix" ). toString()
39+ def pomGitUrl = (project. findProperty(" gitUrl" ) ?: " https://github.com/Gracker/TraceFix.git" ). toString()
40+ def pomLicenseName = (project. findProperty(" licenseName" ) ?: " The Apache Software License, Version 2.0" ). toString()
41+ def pomLicenseUrl = (project. findProperty(" licenseUrl" ) ?: " http://www.apache.org/licenses/LICENSE-2.0.txt" ). toString()
42+ def pomDeveloperId = (project. findProperty(" developerId" ) ?: " TraceFix" ). toString()
43+ def pomDeveloperName = (project. findProperty(" developerName" ) ?: " TraceFix" ). toString()
44+ def pomDeveloperEmail = (project. findProperty(" developerEmail" ) ?: " tracefix@example.com" ). toString()
45+
46+ def configurePomMetadata = { MavenPublication publication ->
47+ publication. pom {
48+ name. set(pomLibraryName)
49+ description. set(pomDescription)
50+ url. set(pomSiteUrl)
51+ licenses {
52+ license {
53+ name. set(pomLicenseName)
54+ url. set(pomLicenseUrl)
55+ }
56+ }
57+ developers {
58+ developer {
59+ id. set(pomDeveloperId)
60+ name. set(pomDeveloperName)
61+ email. set(pomDeveloperEmail)
62+ }
63+ }
64+ scm {
65+ connection. set(pomGitUrl)
66+ developerConnection. set(pomGitUrl)
67+ url. set(pomSiteUrl)
68+ }
69+ }
70+ }
71+
2172publishing {
2273 repositories {
2374 maven {
2475 name = " tracefixRemote"
2576 url = rootProject. layout. buildDirectory. dir(" tracefix-remote-repo" )
2677 }
78+
79+ maven {
80+ name = " sonatype"
81+ def releasesRepoUrl = uri(" https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/" )
82+ def snapshotsRepoUrl = uri(" https://s01.oss.sonatype.org/content/repositories/snapshots/" )
83+ url = releaseVersion. endsWith(" SNAPSHOT" ) ? snapshotsRepoUrl : releasesRepoUrl
84+ credentials {
85+ username = ossrhUsername
86+ password = ossrhPassword
87+ }
88+ }
2789 }
2890
2991 publications {
3092 traceFixLocal(MavenPublication ) {
3193 from components. java
32- groupId = publishedGroupId
33- artifactId = artifact
34- version = TRACEFIX_VERSION_DEBUG
94+ groupId = publishGroupId
95+ artifactId = publishArtifactId
96+ version = debugVersion
3597 artifact tasks. named(" sourcesJarLocal" )
3698 artifact tasks. named(" javadocJarLocal" )
99+ configurePomMetadata(delegate)
100+ }
101+
102+ traceFixRelease(MavenPublication ) {
103+ from components. java
104+ groupId = publishGroupId
105+ artifactId = publishArtifactId
106+ version = releaseVersion
107+ artifact tasks. named(" sourcesJarLocal" )
108+ artifact tasks. named(" javadocJarLocal" )
109+ configurePomMetadata(delegate)
37110 }
38111 }
39112}
40113
114+ def isSonatypeReleaseTask = {
115+ gradle. startParameter. taskNames. any { taskName ->
116+ String normalized = taskName. toLowerCase()
117+ normalized. contains(" sonatype" ) || normalized. endsWith(" publishreleasetosonatype" )
118+ }
119+ }
120+
121+ signing {
122+ required {
123+ isSonatypeReleaseTask()
124+ }
125+ if (isSonatypeReleaseTask()) {
126+ if (signingKey && signingPassword) {
127+ if (signingKeyId) {
128+ useInMemoryPgpKeys(signingKeyId. toString(), signingKey. toString(), signingPassword. toString())
129+ } else {
130+ useInMemoryPgpKeys(signingKey. toString(), signingPassword. toString())
131+ }
132+ } else {
133+ logger. lifecycle(" SIGNING_KEY/SIGNING_PASSWORD not found. Signing will fail for Sonatype publish." )
134+ }
135+ sign publishing. publications. traceFixRelease
136+ }
137+ }
138+
139+ tasks. register(" verifyReleasePublishConfig" ) {
140+ doLast {
141+ List<String > missing = []
142+ if (! ossrhUsername) {
143+ missing. add(" ossrhUsername or OSSRH_USERNAME" )
144+ }
145+ if (! ossrhPassword) {
146+ missing. add(" ossrhPassword or OSSRH_PASSWORD" )
147+ }
148+ if (! signingKey) {
149+ missing. add(" signingKey or SIGNING_KEY (ASCII-armored private key)" )
150+ }
151+ if (! signingPassword) {
152+ missing. add(" signingPassword or SIGNING_PASSWORD" )
153+ }
154+ if (! missing. isEmpty()) {
155+ throw new GradleException (" Missing release publish config: " + missing. join(" , " ))
156+ }
157+ }
158+ }
159+
160+ tasks. withType(PublishToMavenRepository ). configureEach { publishTask ->
161+ if (publishTask. name == " publishTraceFixReleasePublicationToSonatypeRepository" ) {
162+ publishTask. dependsOn(" verifyReleasePublishConfig" )
163+ }
164+ }
165+
166+ tasks. matching { it. name == " signTraceFixReleasePublication" }. configureEach { signTask ->
167+ signTask. onlyIf {
168+ isSonatypeReleaseTask()
169+ }
170+ if (isSonatypeReleaseTask()) {
171+ signTask. dependsOn(" verifyReleasePublishConfig" )
172+ }
173+ }
174+
41175tasks. register(" buildAndPublishToLocalMaven" ) {
42- dependsOn(" build" , " publishToMavenLocal " )
176+ dependsOn(" build" , " publishTraceFixLocalPublicationToMavenLocal " )
43177 doLast {
44- println " * published to maven local: ${ publishedGroupId } :${ artifact } :${ TRACEFIX_VERSION_DEBUG } "
178+ println " * published to maven local: ${ publishGroupId } :${ publishArtifactId } :${ debugVersion } "
45179 }
46180}
47181
@@ -51,3 +185,10 @@ tasks.register("buildAndPublishToRemoteRepo") {
51185 println " * published to file repo: ${ rootProject.buildDir} /tracefix-remote-repo"
52186 }
53187}
188+
189+ tasks. register(" publishReleaseToSonatype" ) {
190+ dependsOn(" publishTraceFixReleasePublicationToSonatypeRepository" )
191+ doLast {
192+ println " * published release: ${ publishGroupId} :${ publishArtifactId} :${ releaseVersion} "
193+ }
194+ }
0 commit comments