I have been trying to publish the library to MavenCentral(), which is published locally successfully through Maven Central in(.m2/repository/...) and working fine. But when I try to publish the library globally on MavenCentral(), it FAILED.
This is my build.gradle file:
plugins {
id("com.android.library")
id("org.jetbrains.kotlin.android")
id("signing")
id("maven-publish")
}
// Configure the signing block
signing {
val signingKey: String? by project
val signingPassword: String? by project
useInMemoryPgpKeys(signingKey, signingPassword)
}
android {
namespace = "com.gk.image_preview"
compileSdk = 34
defaultConfig {
minSdk = 24
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles("consumer-rules.pro")
}
buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
buildFeatures {
compose = true
}
composeOptions {
kotlinCompilerExtensionVersion = "1.5.1"
}
// afterEvaluate {
// android.libraryVariants.forEach { libraryVariant ->
// publishing.publications.create(libraryVariant.name, MavenPublication::class.java) {
// from(components.getByName(libraryVariant.name))
// groupId = "io.github.cypher103360"
// artifactId = "image-preview"
// version = "1.0.0"
// }
// }
// }
}
afterEvaluate {
publishing {
publications {
create<MavenPublication>("release") {
from(components["release"])
groupId = "io.github.cypher103360"
artifactId = "image-preview"
version = "1.0.0"
pom {
name = artifactId
description = "This is the image preview library."
scm {
connection = "scm:[email protected]:Cypher103360/MyLibrary.git"
url = "https://github.com/Cypher103360/MyLibrary.git"
}
developers {
developer {
id = "cypher103360"
name = "Gaurav"
email = "[email protected]"
}
}
}
}
}
repositories {
maven {
name = "MyImagePreviewSnapshot"
url = uri("https://s01.oss.sonatype.org/content/repositories/snapshots/")
credentials {
username = project.findProperty("ossrhUsername")?.toString()
password = project.findProperty("ossrhPassword")?.toString()
}
}
maven {
name = "imagePreview"
url = uri("https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/")
credentials {
username = project.findProperty("ossrhUsername")?.toString()
password = project.findProperty("ossrhPassword")?.toString()
}
}
}
}
}
dependencies {
implementation("androidx.core:core-ktx:1.12.0")
implementation("androidx.appcompat:appcompat:1.6.1")
implementation("com.google.android.material:material:1.10.0")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
implementation(platform("androidx.compose:compose-bom:2023.08.00"))
implementation("androidx.compose.ui:ui")
implementation("androidx.compose.ui:ui-graphics")
implementation("androidx.compose.ui:ui-tooling-preview")
implementation("androidx.compose.material3:material3")
}
And my logs:
> Configure project :image-preview
Kotlin DSL property assignment is an incubating feature.
> Task :image-preview:publishReleasePublicationToImagePreviewRepository FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':image-preview:publishReleasePublicationToImagePreviewRepository'.
> Failed to publish publication 'release' to repository 'imagePreview'
> Could not PUT 'https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/io/github/cypher103360/image-preview/1.0.1-SNAPSHOT/maven-metadata.xml'. Received status code 400 from server: Bad Request
* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
> Get more help at https://help.gradle.org.
BUILD FAILED in 4s
26 actionable tasks: 23 executed, 2 from cache, 1 up-to-date
Can anyone help me to figure out the actual issue, so that I can successfully publish the library?
But... the Sonatype OSSRH (OSS Repository Hosting) Publishing Guide does mention:*
I would assume you cannot deliver a SNAPSHOT version to
staging.You would need a mechanism to decide whether to publish to the snapshot repository or the staging repository based on the version name.
And configure the repository URL dynamically in the
publishingsection of yourbuild.gradle.That would check if the
versionends with"SNAPSHOT". If it does, it sets the repository URL to the snapshot repository. Otherwise, it uses the staging repository URL. Thecredentialsblock remains the same for both repositories.Make sure the
versionvariable in yourpublishingblock does reflect the version of your library, and it should be dynamically set based on whether you are doing a snapshot or a release build.And double-check that
ossrhUsernameandossrhPasswordare correctly defined in yourgradle.propertiesor wherever you are storing your credentials.Execute your Gradle task with the
--infoor--debugflag for more detailed output:The URL should have been
https://s01.oss.sonatype.org/content/repositories/snapshots/, nothttps://s01.oss.sonatype.org/service/local/staging/deploy/maven2/. That is why I have added theprintln "Publishing to repository URL: $repositoryUrl"debug statement.Check that you see the right URL when passing a version like
1.0.0-SNAPSHOT.The publishing process is generally working correctly, but there is a warning regarding a duplicate path in the
:image-preview:releaseSourcesJartask. That warning indicates an issue in the configuration of the task that creates the sources JAR file for your project.The warning
Encountered duplicate path "main/com/gk/image_preview/ImagePreview.kt" during copy operation configured with DuplicatesStrategy.WARNsuggests that the same file path is being included more than once in the sources JAR. That might happen if the task configuration incorrectly includes the same source files multiple times or if there are actual duplicate files in your source directories.So review the configuration of the
sourceSetsin yourbuild.gradle. Make sure you are not including the same directory or file more than once. Look at how thereleaseSourcesJartask is set up. It should be configured to include the source files from your project without duplication. If you are customizing this task, make sure it is correctly aggregating source files. Verify that there are not duplicate files in your source directories. Sometimes, there can be copies or backups of files that could be inadvertently included.If you have customized the
releaseSourcesJartask, it should resemble something like this:That example creates a sources JAR from the main source set without duplication.
If the duplication is intentional and harmless, you can change the duplicate strategy to 'INCLUDE' to suppress the warning. However, this is generally not recommended without understanding why the duplicates exist.
You see an error message in the Nexus Repository Manager during the closing of the repository: it indicate that the required signature files for your artifacts are missing. These
.ascfiles are PGP signatures, and their absence means that your artifacts (the.module,.pom,.sources.jar, and.aarfiles) have not been signed as expected by the repository's requirements.The
signingplugin should be configured to automatically sign the artifacts produced by your build. Make sure thesigningblock is correctly configured and that it is being applied to all the relevant artifacts (aar,jar,pom, etc.).An example of a signing configuration in
build.gradlemight look like this (usinguseInMemoryPgpKeys):In this example,
sign publishing.publicationsis the key line that explicitly signs all Maven publications defined in the publishing block. I do not see that line in yourbuild.gradle, so I suspect that is the issue.If the repository where you have uploaded your library is not already included in your project's build file, you will need to add it. For example, if you are using Gradle, you can add the repository URL to your
build.gradlefile in therepositoriesblock:Next, you will need to add your library as a dependency in the module where you want to use it. In your
build.gradlefile, under thedependenciesblock, add a line for your library, specifying the group ID, artifact ID, and version:After adding the repository and dependency, sync your project with the updated Gradle files. If you are using an IDE like Android Studio, it usually prompts you to sync after making changes to your Gradle files.
Once the project sync is complete, and the build is successful, you can start using the classes, methods, or resources from your library in your project.