Skip to content

Commit

Permalink
Prepared for maven central publication.
Browse files Browse the repository at this point in the history
  • Loading branch information
vsnappy1 committed Nov 13, 2024
1 parent a97412c commit d6db1f5
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 23 deletions.
20 changes: 6 additions & 14 deletions .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,29 +45,21 @@ jobs:
uses: android-actions/setup-android@v2.0.10

- name: Run Tests
run: ./gradlew test
run: ./gradlew :resourcemanager:test

- name: Generate Artifacts
run: ./gradlew :resourcemanager-compiler:generateArtifacts :resourcemanager-runtime:generateArtifacts --parallel
run: ./gradlew :resourcemanager:generateArtifacts

- name: Generate CheckSum
run: ./gradlew :resourcemanager-compiler:generateChecksum :resourcemanager-runtime:generateChecksum --parallel
run: ./gradlew :resourcemanager:generateChecksum

- name: Sign Artifacts
run: ./gradlew :resourcemanager-compiler:signArtifacts :resourcemanager-runtime:signArtifacts --parallel
run: ./gradlew :resourcemanager:signArtifacts
env:
GPG_PASSPHRASE: ${{ secrets.GPG_PASSPHRASE }}

- name: Create bundle
run: ./gradlew :resourcemanager-compiler:generateBundle :resourcemanager-runtime:generateBundle --parallel

- name: Upload bundle
uses: actions/upload-artifact@v4.4.3
with:
name: resourcemanager-compiler
path: resourcemanager-compiler/build/artifacts

- uses: actions/upload-artifact@v4.4.3
with:
name: resourcemanager-runtime
path: resourcemanager-runtime/build/artifacts
name: resourcemanager
path: resourcemanager/build/artifacts
19 changes: 12 additions & 7 deletions buildSrc/src/main/kotlin/GenerateArtifactsTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,16 @@ import org.gradle.api.publish.maven.tasks.GenerateMavenPom
abstract class GenerateArtifactsTask : DefaultTask() {

/**
* The name of the artifact, including its version, used to label
* generated files consistently.
* The name of the artifact, used to label generated files consistently.
*/
@get:Input
lateinit var artifactNameWithVersion: String
lateinit var artifactName: String

/**
* The version of the artifact, used to label generated files consistently.
*/
@get:Input
lateinit var artifactVersion: String

/**
* The directory where all generated artifacts will be saved.
Expand Down Expand Up @@ -64,7 +69,7 @@ abstract class GenerateArtifactsTask : DefaultTask() {
project.tasks.create("generateBaseJar", Jar::class.java) {
group = "build"
description = "Generates a base JAR file containing compiled classes."
archiveBaseName.set(artifactNameWithVersion)
archiveBaseName.set(artifactName)
destinationDirectory.set(outputDirectory)
from(project.tasks.getByName("classes").outputs.files)
dependsOn(":resourcemanager-compiler:build")
Expand All @@ -78,7 +83,7 @@ abstract class GenerateArtifactsTask : DefaultTask() {
project.tasks.create("generateJavadocJar", Jar::class.java) {
group = "build"
description = "Generates a JAR file containing Javadoc."
archiveBaseName.set(artifactNameWithVersion)
archiveBaseName.set(artifactName)
archiveClassifier.set("javadoc")
destinationDirectory.set(outputDirectory)
from(project.tasks.getByName("javadoc").outputs.files)
Expand All @@ -94,7 +99,7 @@ abstract class GenerateArtifactsTask : DefaultTask() {
project.tasks.create("generateSourcesJar", Jar::class.java) {
group = "build"
description = "Generates a JAR file containing source files."
archiveBaseName.set(artifactNameWithVersion)
archiveBaseName.set(artifactName)
archiveClassifier.set("sources")
destinationDirectory.set(outputDirectory)
from(extensions.sourceSets.getByName("main").allSource)
Expand All @@ -110,7 +115,7 @@ abstract class GenerateArtifactsTask : DefaultTask() {
project.tasks.create("generatePom", GenerateMavenPom::class.java) {
group = "build"
description = "Generates the POM XML file for the Maven publication."
destination = File(outputDirectory, "$artifactNameWithVersion.pom")
destination = File(outputDirectory, "${artifactName}-${artifactVersion}.pom")
pom = this@GenerateArtifactsTask.pom
}.also { it.actions.forEach { action -> action.execute(it) } }
}
Expand Down
125 changes: 123 additions & 2 deletions resourcemanager/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
import java.util.Properties

plugins {
`kotlin-dsl`
kotlin("jvm")
id("java-gradle-plugin")
id("com.gradle.plugin-publish") version "1.3.0"
id("maven-publish")
}
val agp: String by project
val pluginGroup = "dev.randos"
val pluginVersion = "0.0.1"
val pluginName = "resourcemanager"

dependencies {
implementation(gradleApi())
implementation(localGroovy())
compileOnly("com.android.tools.build:gradle:$agp")
}

group = "dev.randos"
version = "0.0.1"
group = pluginGroup
version = pluginVersion

gradlePlugin {
website.set("https://github.com/vsnappy1/ResourceManager")
Expand Down Expand Up @@ -42,4 +48,119 @@ publishing {
repositories {
mavenLocal()
}
}

val mavenPublication: MavenPublication by lazy {
(publishing.publications.getByName("java") as MavenPublication)
}

val artifactRepo: File by lazy {
val path = mavenPublication.let {
StringBuilder().apply {
append("artifacts/")
append("${it.groupId.replace(".", "/")}/")
append("${it.artifactId}/")
append(it.version)
}.toString()
}
file(layout.buildDirectory.dir(path))
}

val publishBundleRepo: File by lazy { file(layout.buildDirectory.dir("publish/bundle"))}

val artifacts: List<File> by lazy {
val fullQualifiedPath = "${artifactRepo}/${pluginName}-${pluginVersion}"
listOf(
file("${fullQualifiedPath}.jar"),
file("${fullQualifiedPath}-javadoc.jar"),
file("${fullQualifiedPath}-sources.jar"),
file("${fullQualifiedPath}.pom")
)
}

/* ----------------- Generate Artifacts ----------------- */

tasks.register<GenerateArtifactsTask>("generateArtifacts") {
group = "build"
description = "Generates base AAR, javadoc JAR, sources JAR, and POM file."
artifactName = pluginName
artifactVersion = pluginVersion
outputDirectory = artifactRepo
pom = mavenPublication.pom
artifactRepo.mkdirs()
}

/* ----------------- Generate CheckSum ----------------- */

tasks.register<GenerateChecksumTask>("generateChecksum") {
group = "verification"
description = "Generates MD5 and SHA-1 checksums for specified files."
filesToChecksum = artifacts
}

/* ----------------- Sign Artifacts ----------------- */

tasks.register<SignArtifactsTask>("signArtifacts") {
group = "signing" // Grouping under signing category
description = "Signs all artifacts with GPG/PGP."
filesToSign = artifacts
passphrase = getSecret("GPG_PASSPHRASE")
}

fun getSecret(name: String): String {
// Load passphrase from local.properties or environment variable
val properties = Properties()
val localPropertiesFile = project.rootProject.file("local.properties")

// Check if local.properties exists
if (localPropertiesFile.exists()) {
properties.load(localPropertiesFile.inputStream())
}

return properties.getProperty(name) ?: System.getenv(name)
?: throw GradleException("$name not found in local.properties or environment variables.")
}

/* ----------------- Maven Publish (Meta data) ----------------- */
publishing {
publications {
register<MavenPublication>("java") {
groupId = pluginGroup
artifactId = pluginName
version = pluginVersion

pom {
name = "${groupId}:${artifactId}"
description =
"ResourceManager is an Android plugin that simplifies accessing Android resources (strings, colors, drawables, etc.) in both Android and non-Android components (e.g., ViewModel) using generated code."
url = "https://github.com/vsnappy1/ResourceManager"

licenses {
license {
name = "The Apache License, Version 2.0"
url = "http://www.apache.org/licenses/LICENSE-2.0.txt"
}
}

developers {
developer {
name = "Vishal Kumar"
email = "vsnappy1@gmail.com"
organization = "Randos"
organizationUrl = "https://www.randos.dev"
}
}

scm {
connection = "scm:git:git://github.com/vsnappy1/ResourceManager.git"
developerConnection =
"scm:git:ssh://github.com/vsnappy1/ResourceManager.git"
url = "https://github.com/vsnappy1/ResourceManager"
}
}
afterEvaluate {
from(components["java"])
}
}
}
}

0 comments on commit d6db1f5

Please sign in to comment.