Goodbye JCenter, publish your open source library to MavenCentral

This article is simultaneously published on my WeChat official account. You can follow by scanning the QR code at the bottom of the article or searching for Guo Lin on WeChat. Articles are updated every working day.

Regarding the abandonment of JCenter, I believe many friends have already known it. I also wrote an article a few months ago to analyze the ins and outs of the JCenter abandonment event. Interested friends can read this article to talk about the event that JCenter is about to be stopped .

Several key points were mentioned in the above article:

  1. JCenter will not accept any new library submissions after March 31, 2021.
  1. After February 1, 2022, JCenter will stop providing library download services.
  1. Google will publish a plan on how to migrate the open source library from JCenter on its official Android developer website.

At that time, the overall plan was like this, and then a few months passed, I really have to say that the plan will never catch up with the changes.

First of all, Google revised the statement on the official website of the Android developer, and did not mention that it will give a relocation plan. Just inform the publisher of the open source library, and all suggestions for publishing the library will be published on MavenCentral in the future. And inform users of open source libraries that you have to find out where these libraries are.

This wave of operations by Google has really thrown the pot away. The abandonment of JCenter has nothing to do with "I", it is a matter between third-party developers and JCenter.

At that time, after I saw Google's revised statement, I thought to myself, what about the migration of this old library, Google doesn't care at all.

But after a closer look, I found out that JCenter also changed the statement. In JCenter's latest statement, the article that stopped the download service after February 1, 2022 was deleted. In other words, although JCenter no longer allows developers to submit new libraries to it, the old libraries that have been uploaded will continue to provide download services, so there is no need to migrate them.

I don't know why JCenter changed its attitude to do such a good deed. I guess it is possible that Google negotiated with them in private? But in any case, this is a good thing for developers of open source libraries, which means that we don't need to think about the problems of old libraries.

The question now is, what about the new library in the future?

Why use MavenCentral

Google has given its official attitude, suggesting that developers publish libraries on MavenCentral in the future.

The official attitude is actually very important, because JCenter was corrected by Google in the same way in the past (Google was screwed miserably), otherwise JCenter would not be able to have such a large influence in the Android field.

If you now use the latest version of Android Studio to create a new project, you will find that the declaration of the warehouse in the build.gradle file becomes like this:

allprojects {
    
    
    repositories {
    
    
        google()
        mavenCentral()
        jcenter() // Warning: this repository is going to shut down soon
    }
}

It can be seen that the mavenCentral() warehouse has become the default warehouse when creating a project, and the jcenter() warehouse has been marked as obsolete.

Many domestic developers may feel that MavenCentral is too much trouble, far less easy to use than JitPack.

However, the problem with JitPack is that it is not officially recommended by Google. If you publish your library on JitPack, developers who want to use your library have to import an additional JitPack warehouse, which will undoubtedly increase the cost of use.

Therefore, even if it is troublesome, I still recommend that developers publish their open source libraries on MavenCentral.

So next, let's see how to achieve it.

Prepare domain name

Compared with JCenter, MavenCentral's publishing standards are much higher. Not only is the operation more troublesome, but MavenCentral will also check whether you own the domain name corresponding to the package path.

For example, if you submit a library, its package path format is as follows:

com.example.test:library:1.0.0

Then you must have the domain name example.com.

In contrast, JCenter does not check whether you really own the domain name at all. Whoever submits the library using this package path first will own the package path.

In addition, it costs money to register your own domain name. If you don’t want to spend money, you can also use GitHub to complete it, but then your package path must be prefixed with io.github instead of using a custom package path.

This article introduces how to use your own domain name to submit to MavenCentral. If you want to know how to use the io.github domain name, you can refer to this article Publishing Android Lib to Maven Central .

So needless to say, the domain name must be prepared first. I registered a domain name called guolindev.com on Alibaba Cloud, so I can use the package path prefixed with com.guolindev.

Create work order

Next, visit the following address to register a Sonatype account:

https://issues.sonatype.org/secure/Dashboard.jspa

After registration, revisit this address to enter the Sonatype Dashboard interface.

Now click on the Create button on the top toolbar to create a ticket:

Although this work order seems to have many items to fill in, it is actually very simple. I will post a screenshot of a work order I filled out here:

We only need to fill in the required fields marked with an asterisk.

  • Project and Issue Type remain the default options and do not need to be modified.
  • Summary can be filled in at will, here I fill in the project name.
  • Group Id is very important and determines what the package path of the library is. The prefix uses the inverted method of the domain name you just applied for, and you can customize some paths related to the library name later.
  • Project URL fill in the Github address of the project.
  • The SCM url only needs to add the .git suffix after the above Github address.

Click Create to complete the ticket creation.

After creating a work order, you need to wait for the staff to review it. I have tested that no one will review your work order during non-working hours. So if you create a ticket over the weekend, you may have to wait two to three days for someone to process it.

After a worker handles your ticket, he will leave a comment in your ticket, as shown in the picture below:

The meaning of this comment is that you have to prove that the domain name corresponding to the package path you just filled in the Group Id is yours.

The way to prove it is to add a resolution of type TXT to your domain name and point it to the URL address of your work order.

If your domain name is registered in Alibaba Cloud, you can go to the domain name management interface, click on the domain name to resolve, and then add the resolution record as shown in the example below:

After adding the analysis record, you need to reply in the work order, you can just simply comment Done. In this way, the staff will verify whether the resolution record you added has taken effect. After verifying that there is no problem, they will tell you in the comments that the application has passed:

According to the comments, we now have permission to publish libraries to the address s01.oss.sonatype.org.

create key

MavenCentral also requires that all published libraries must be signed using GPG, so we will do this next.

GPG is built-in on some Linux systems. If the Linux system you are using is not built-in, you can install it with the following command:

sudo apt install gnupg

The Mac system can be installed with the help of HomeBrew, the command is as follows:

brew install gpg

Windows system needs to visit the following address to download and install manually:

http://www.gnupg.org/download/

After the installation is complete, enter the following command to create the key:

gpg --full-generate-key

Next, a series of options will pop up for you to choose, such as key type, key length, key expiration time, etc. If there is no special requirement, just click Enter all the way and use the default option.

In addition, you will be asked to fill in your name, email address, etc., and set a password for this key.

After filling in the above information, you can see the key you created:

Next, we need to upload the newly created key to the GPG server. In this way, MavenCentral can get our key from the GPG server at that time, and then verify the uploaded package.

The upload command is as follows:

gpg --keyserver hkp://pgp.mit.edu --send-keys C37AF927

Note that the key ID used after the last send-keys is the last 8 digits of the key ID you just created. Be sure to use the last 8 digits here. Because I used the complete key ID before, there have been strange errors in the result.

In the last step, execute the following command to generate a private key file:

gpg --export-secret-keys  -o 文件路径/secring.gpg

This private key file must be saved locally, and it will be used later when submitting the library.

start posting

After all the above steps are completed, we can start publishing the open source library we wrote.

In order to make the publishing process easier, I use a third-party open source plugin such as gradle-maven-publish-plugin. Of course, if you directly use the official maven-publish plugin, you can also achieve the same function, but you have to write a lot of extra code.

The open source library address of the gradle-maven-publish-plugin plugin is:

https://github.com/vanniktech/gradle-maven-publish-plugin

The use of this library is very simple, just copy and paste the following code to the bottom of the build.gradle file of the module where the open source library you want to release is located:

...

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.vanniktech:gradle-maven-publish-plugin:0.17.0'
    }
}

allprojects {
    plugins.withId("com.vanniktech.maven.publish") {
        mavenPublish {
            sonatypeHost = "S01"
        }
    }
}

apply plugin: "com.vanniktech.maven.publish"

Next, open the gradle.properties file in the root directory of the project, and fill in the following content according to the template below:

GROUP=com.guolindev.glance
POM_ARTIFACT_ID=glance
VERSION_NAME=1.0.0

POM_NAME=Glance
POM_DESCRIPTION=A simple and handy Android database debugging library.
POM_INCEPTION_YEAR=2020
POM_URL=https://github.com/guolindev/Glance/

POM_LICENSE_NAME=The Apache Software License, Version 2.0
POM_LICENSE_URL=https://www.apache.org/licenses/LICENSE-2.0.txt
POM_LICENSE_DIST=repo

POM_SCM_URL=https://github.com/guolindev/Glance/
POM_SCM_CONNECTION=scm:git:git://github.com/guolindev/Glance.git
POM_SCM_DEV_CONNECTION=scm:git:ssh://github.com/guolindev/Glance.git

POM_DEVELOPER_ID=guolindev
POM_DEVELOPER_NAME=Lin Guo
POM_DEVELOPER_URL=https://github.com/guolindev/

signing.keyId=密钥ID的后8位
signing.password=密钥密码
signing.secretKeyRingFile=私钥文件路径

mavenCentralUsername=Sonatype账号
mavenCentralPassword=Sonatype密码

The first three items of this template determine the package path of your open source library. For example, according to the way I wrote above, the package path of this open source library is:

com.guolindev.glance:glance:1.0.0

Most of the other items are explanatory, and you can fill them in according to your actual situation.

In the last part of the key, fill in the public key ID and password you just created, as well as the file path of the private key, and also fill in the Sonatype account password.

Since we have filled in a lot of privacy content in this file, we must remember to exclude it from version control.

Now, we can find the publish task in the Gradle tab of the toolbar on the right side of Android Studio, and double-click to execute the library to publish it.

If all goes well, you should be able to see in the console that the library has been published successfully.


Sync to MavenCentral

But it's not over here.

What needs to be known is that we did not publish the library to MavenCentral just now, and MavenCentral does not allow us to publish the library directly.

Looking back at the ticket created at the beginning, what did the staff reply to us?

As you can see, we can only publish libraries to the address s01.oss.sonatype.org, which is not MavenCentral.

In addition, the release script we just wrote in the build.gradle file also confirms this:

allprojects {
    plugins.withId("com.vanniktech.maven.publish") {
        mavenPublish {
            sonatypeHost = "S01"
        }
    }
}

Here, the sonatypeHost is specified as S01, and S01 refers to publishing the library to s01.oss.sonatype.org.

So how can the library be published to MavenCentral? Next we need to do some synchronization.

Visit the following URL and log in with your Sonatype account password (the login button is in the upper right corner of the page):

https://s01.oss.sonatype.org/

After successful login, there will be a Staging Repositories option in the left sidebar of the page:

Click this option, and you can see the open source library that has just been successfully released:

Now we need to sync this repository to the MavenCentral repository. The operation method is to check the warehouse first, then click the Close button to close the warehouse, and click Confirm in the confirmation window that pops up.

After the Close is finished, refresh the interface, and you will find that the Release button becomes clickable:

Click the Release button and click Confirm in the pop-up window, so that the open source library can be synchronized to MavenCentral.

After a while, you will see a new comment on the previously created ticket interface:

This is to tell us that the synchronization has started, and it usually takes about 30 minutes. But if you want to find the warehouse we submitted in search.maven.org , it may take up to 4 hours.

I didn't specifically verify how long it would take to synchronize. Anyway, after I clicked the Release button, I went to search.maven.org the next day to search, and I could already find the library submitted yesterday:

After the submission is complete, if you want to reference this library in the project, you only need to write like this:

dependencies {
    implementation 'com.guolindev.glance:glance:1.0.0'
}

It is no different from the previous way of writing using the JCenter warehouse.

So far, we have gone through all the processes of publishing the open source library to MavenCentral. As long as you follow these processes, I believe everyone can successfully publish the library to MavenCentral.

At the same time, this article has become another DLC of "The First Line of Code 3rd Edition" . With Google's high-speed technical iteration, I don't know how many patch articles have been published for this book.

In addition, careful friends may have noticed that I have been using the Glance library for demonstrations in this article, which means that a new version of Glance will be released soon. Then in the next original article, I will talk to you about the new features of the new version of Glance, and we will see you in the next issue.


If you want to learn Kotlin and the latest Android knowledge, you can refer to my new book "The First Line of Code 3rd Edition" , click here to view details .


Pay attention to my technical public account, and high-quality technical articles will be pushed every day.

Scan the QR code below on WeChat to follow:

Guess you like

Origin blog.csdn.net/sinyu890807/article/details/119706565