From 45bdb39e57ec1a338436da8927c9bf8ed098e03e Mon Sep 17 00:00:00 2001 From: Felipe Bonezi Date: Sun, 25 Jun 2023 22:59:39 -0300 Subject: [PATCH] feat: publish to Maven Nexus --- .github/workflows/android.yml | 23 ++++++++++ app/build.gradle | 7 +++ app/publish-remote.gradle | 86 +++++++++++++++++++++++++++++++++++ build.gradle | 5 ++ scripts/publish-root.gradle | 43 ++++++++++++++++++ 5 files changed, 164 insertions(+) create mode 100644 app/publish-remote.gradle create mode 100644 scripts/publish-root.gradle diff --git a/.github/workflows/android.yml b/.github/workflows/android.yml index cefaf67..0f9c469 100644 --- a/.github/workflows/android.yml +++ b/.github/workflows/android.yml @@ -25,3 +25,26 @@ jobs: - name: Build with Gradle run: ./gradlew build + + publish: + name: Publish to Maven Central + if: "github.event.release || github.event.release.prerelease" + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - name: Setup JDK 17 + uses: actions/setup-java@v3 + with: + java-version: '17' + distribution: 'temurin' + cache: gradle + + - name: Setup credentials + run: | + echo $GPG_KEY_FILE | base64 --decode > secring.gpg + echo $GRADLE_PROPERTIES_FILE | base64 --decode > gradle.properties + echo 'Setup credentials successfuly!' + + - name: Publish package + run: ./gradlew publishReleasePublicationToSonatypeRepository diff --git a/app/build.gradle b/app/build.gradle index a4a60f4..5821127 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -21,3 +21,10 @@ dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) testImplementation 'junit:junit:4.13.2' } + +ext { + PUBLISH_GROUP_ID = 'io.github.felipebonezi' + PUBLISH_VERSION = '1.0.0' + PUBLISH_ARTIFACT_ID = 'android-printer-lib' +} +apply from: 'publish-remote.gradle' diff --git a/app/publish-remote.gradle b/app/publish-remote.gradle new file mode 100644 index 0000000..dcdcb04 --- /dev/null +++ b/app/publish-remote.gradle @@ -0,0 +1,86 @@ +apply plugin: 'maven-publish' +apply plugin: 'signing' + + +task androidSourcesJar(type: Jar) { + archiveClassifier.set('sources') + if (project.plugins.findPlugin("com.android.library")) { + // For android libraries + from android.sourceSets.main.java.srcDirs + from android.sourceSets.main.kotlin.srcDirs + } else { + // For pure kotlin libraries, in case you have them + from sourceSets.main.java.srcDirs + from sourceSets.main.kotlin.srcDirs + } +} + +artifacts { + archives androidSourcesJar +} + +group = PUBLISH_GROUP_ID +version = PUBLISH_VERSION + +afterEvaluate { + publishing { + publications { + release(MavenPublication) { + // The coordinates of the library, being set from variables that + // we'll setup later + groupId PUBLISH_GROUP_ID + artifactId PUBLISH_ARTIFACT_ID + version PUBLISH_VERSION + + // Two artifacts, the `aar` (or `jar`) and the sources +// if (project.plugins.findPlugin("com.android.library")) { +// from components.release +// } else { +// from components.java +// } + + artifact(bundleReleaseAar) +// artifacts javaDocJar + + // Mostly self-explanatory metadata + pom { + name = PUBLISH_ARTIFACT_ID + description = "Android printer library" + url = 'https://github.com/felipebonezi/android-printer-lib' + licenses { + license { + name = 'Apache 2.0 license' + url = 'https://opensource.org/license/apache-2-0/' + } + } + developers { + developer { + id = 'felipebonezi' + name = 'Felipe Bonezi' + email = 'felipebonezi@gmail.com' + url = 'https://github.com/sponsor/felipebonezi' + } + } + + // Version control info + scm { + connection = 'scm:git:git://github.com/felipebonezi/android-printer-lib.git' + developerConnection = 'scm:git:ssh://github.com/felipebonezi/android-printer-lib.git' + url = 'http://github.com/felipebonezi/android-printer-lib' + } + } + + } + } + } +} + + +signing { + useInMemoryPgpKeys( + rootProject.ext["signing.keyId"], + rootProject.ext["signing.key"], + rootProject.ext["signing.password"] + ) + sign publishing.publications +} diff --git a/build.gradle b/build.gradle index 9ab771f..e5e83b4 100644 --- a/build.gradle +++ b/build.gradle @@ -13,6 +13,11 @@ buildscript { } } +plugins { + id("io.github.gradle-nexus.publish-plugin") version "1.3.0" +} +apply from: 'scripts/publish-root.gradle' + allprojects { repositories { google() diff --git a/scripts/publish-root.gradle b/scripts/publish-root.gradle new file mode 100644 index 0000000..15f83ba --- /dev/null +++ b/scripts/publish-root.gradle @@ -0,0 +1,43 @@ +// Create variables with empty default values +ext["signing.keyId"] = '' +ext["signing.password"] = '' +ext["signing.key"] = '' +ext["ossrhUsername"] = '' +ext["ossrhPassword"] = '' +ext["sonatypeStagingProfileId"] = '' + + +File secretPropsFile = project.rootProject.file('local.properties') +if (secretPropsFile.exists()) { + // Read local.properties file first if it exists + Properties p = new Properties() + new FileInputStream(secretPropsFile).withCloseable { is -> + p.load(is) + } + p.each {name, value -> + ext[name] = value + } +} else { + // Use system environment variables + ext["ossrhUsername"] = System.getenv('SONATYPE_USERNAME') + ext["ossrhPassword"] = System.getenv('SONATYPE_PASSWORD') + ext["sonatypeStagingProfileId"] = System.getenv('SONATYPE_STAGING_PROFILE_ID') + ext['signing.keyId'] = System.getenv('GPG_KEY_ID') + ext['signing.password'] = System.getenv('SONATYPE_PASSWORD') + ext["signing.key"] = System.getenv('GPG_KEY_FILE') +} + + + +// Set up Sonatype repository +nexusPublishing { + repositories { + sonatype { + stagingProfileId = sonatypeStagingProfileId + username = ossrhUsername + password = ossrhPassword + nexusUrl.set(uri("https://s01.oss.sonatype.org/service/local/")) + snapshotRepositoryUrl.set(uri("https://s01.oss.sonatype.org/content/repositories/snapshots/")) + } + } +}