- Apply Gradle plugin
plugins { id("me.him188.maven-central-publish") version "1.0.0-dev-1" }
- Configure
mavenCentralPublish
RaedMavenCentralPublishExtension.pomConfigurators
for required information, or simply configure a GitHub project like:mavenCentralPublish { singleDevGithubProject("Him188", "yamlkt") licenseFromGitHubProject("Apache-2.0", "master") }
- Set credentials: set any of Gradle property, JVM property, JVM or system environment variable
named
publication.credentials
orPUBLICATION_CREDENTIALS
- Finish. Run task
publish
.
If you publish your artifact (namely your project files)
to Maven Central repository, anyone can access your files directly, in Gradle
by declaring mavenCentral()
repository and in maven that is already done by default.
Anyone can publish artifacts to the Maven Central repository (MC in short), however MC has strict rules.
If you are a personal developer, you usually don't need to pay specific attention to these rule, just follow this guide and use this plugin.
Follow steps 1 to 7 described by How to Publish Your Artifacts to Maven Central . (Thanks to the original author)
https://oss.sonatype.org/ or new server https://s01.oss.sonatype.org
As of February 2021, all new projects began being provisioned on https://s01.oss.sonatype.org/
- Login with your Sonatype account
- On the upper right corner, click your username
- Click
Profile
- Click
Summary
and chooseUser Token
Access User Token
- Note down the
username
andpassword
, this will be your Sonatype User Token.
You can also use this pair of username and password to log into your account, so it is just like another way to access your Sonatype account, but you can reset the User Token so it is more safe.
- Generate a key pair
-
Install GnuPG (Binary releases)
-
Download
key-gen.sh
( from Karlatemp/PublicationSign by @Karlatemp) -
Execute
key-gen.sh
, and you will getkeys.gpg.pub
andkeys.gpg
that are to be used later.keys.gpg.pub
: the public keykeys.gpg
: the private key
-
Make sure that your GPG Key has no password otherwise you cannot sign your artifacts successfully.
-
Upload the key pair to a public keyserver
Open
keys.gpg.pub
as a text file, upload its content to the website.
Credentials is a pack of your Sonatype user and key pair that can be re-used for further publications.
- Download a generator from releases
- Create a directory, place the following files into it.
- the generator you just downloaded
keys.gpg.pub
: your GnuPG public keykeys.gpg
: your GnuPG private keysonatype.txt
: a text file that contains your Sonatype User Token, withusername
in the first line, and thepassword
in the second line.
- Execute the generator.
- The keys are packed into a file
credentials.txt
, the content of which is yourcredentials
You can re-use the credentials
for all of your projects.
Then follow the chapter I already had credentials.
You need credentials
, otherwise read I am new to publication first.
Note that the plugin should be applied before any other plugins, in other words, at the first line of plugins
block.
The plugin should be applied to the subproject that needs to be published. Applying the plugin to the root project will not affect subprojects.
plugins {
id("me.him188.maven-central-publish") version "1.0.0-dev-1"
// then apply other plugins if needed
}
plugins {
id 'net.mamoe.maven-central-publish' version '1.0.0-dev-1
// then apply other plugins if needed
}
If your Sonatype account was created after February 2021, you may need to use the new server.
mavenCentralPublish {
useCentralS01() // use new server
// ... add other configurations
}
When you get error about 'Unauthorized' when executing the task publish
, you might consider using the new or the old
server (by removing the useCentralS01()
).
You can read more on the official documentation
A simple configurator for GitHub projects with single developer.
mavenCentralPublish {
singleDevGithubProject("GitHub username of the owner of the repository", "GitHub repository name")
licenseFromGitHubProject("Open-source License name", "Repository main branch name")
}
A real example from yamlkt:
mavenCentralPublish {
singleDevGithubProject("Him188", "yamlkt")
licenseFromGitHubProject("Apache-2.0", "master")
}
mavenCentralPublish {
githubProject("GitHub username of the owner of the repository", "GitHub repository name")
licenseFromGitHubProject("Open-source License name", "Repository main branch name")
developer("Developer1")
developer("Developer2") // add as many as needed
}
Read MavenCentralPublishExtension.publicationConfigurators
for detailed manual configurations.
You should have got credentials previously, then follow one of the following methods.
In your GRADLE_HOME
(to set globally) or project dir (to set locally), open or create gradle.properties
, append a
line:
publication.credentials=CONTENT OF credentials.txt
This should be like:
publication.credentials=0afc0c2d2d2d2... (very long)
Add a system environment variable named publication.credentials
or PUBLICATION_CREDENTIALS
with the content of ***
credentials.txt*** (not the file path)
Add an JVM environment variable named publication.credentials
or PUBLICATION_CREDENTIALS
with the content of ***
credentials.txt*** (not the file path)
Add argument -Ppublication.credentials=CONTENT OF credentials.txt
.
mavenCentralPublish {
credentials = TODO() // provide a PublicationCredentials instance.
}
Now you can execute the publish
task to upload artifacts. If you want to use this plugin a second time, just
read I have used this plugin before
When publish
executed successfully, you need to do close
and release
on
Sonatype https://oss.sonatype.org/#stagingRepositories.
If a close
has failed, it means this plugin has done something wrong. Please report
to issues.
If all succeed, your repository will be in Maven Central and can be accessed publicly. You can check on https://mvnrepository.com/.
close
and release
can be done automatically by another plugin https://github.com/bmuschko/gradle-nexus-plugin.
maven-central-publish
has already configured that for you. You can execute task closeAndReleaseRepository
in root
project.
However, this can only handle single publication. If you publish multiple modules at one time, you can only close and release on the Sonatype website.