Step by step, we will teach you how to share an open-source project to the Maven central repository and an open-source project maven

Source: Internet
Author: User
Tags maven central sonatype

Step by step, we will teach you how to share an open-source project to the Maven central repository and an open-source project maven

Welcome to my Sina Weibo:Http://weibo.com/kifile

Reprinted please indicate the sourceHttp://blog.csdn.net/kifile)


I believe many programmers have written more or less some common library files, and many people will share their code with code hosting websites like Github in the spirit of open source.

In fact, the purpose of sharing code is to allow more people to use it. However, many people may find it very troublesome to download and use the tools such as Github, so is there any way to help us use the code without downloading the source code on Github?

The answer is naturally acceptable. Like Maven, Gradle (in fact, the mechanism for Gradle to get code is also based on Maven) is an automatic build tool that can help you get specified code from the Internet, and configure it in your project. Therefore, as long as we can upload our code to the Maven central repository, others can reference your project through several lines of code.

Next, we will explain how to upload an open source project to the Maven central repository step by step.

First, let me briefly introduce the environment configuration of my own platform:

Platform: MacOS Yosemite.

IDE: Android Studio 1.0.2.

Project: Android Library.

In fact, other platforms are similar, but for convenience, I will briefly introduce the sharing steps based on the above environment configuration.

First put Maven Official Guide address: http://central.sonatype.org/pages/ossrh-guide.html

1. Apply for the sonatype account and create a project request

Maven-related management work seems to have been hosted on the sonatype.org website for processing. When we want to add a new project to the Maven central repository, we must first apply for an account here, and provide the corresponding project information.

Apply for account address here: https://issues.sonatype.org/secure/Signup! Default. jspa

After the application is complete, we need to provide the corresponding project information, address here: https://issues.sonatype.org/secure/CreateIssue.jspa? Issuetype = 21 & pid = 10134

Although the webpage for entering project information is in English, it is quite simple. Many input boxes have corresponding Input prompts, so I will not introduce it more, figure 1 briefly introduces the required information:



Figure 1 project-related information

After you click Create, you will enter a jira page (sonatype uses jira to track the progress of each project). At the same time, you will receive an email prompting you in your registered email address, the task is successfully created and is waiting for processing.

What we need to do next is to wait. The maven project can be officially released only after sonatype is approved. According to the description on the official website, it takes about two days, however, early the next morning, my project sent an email notifying me that the project was successfully created, and I liked their speed.


2. Upload the project to the Maven repository.

If you receive a feedback email from sonatype and confirm that a new project has been created for you, you can start uploading your project.

I am going to upload a newly written Android click effect Library file to the Maven repository this time. Because I am using Android Studio, I am directly using gradle in my Library project. in the build file.

First, add the following code to the end of the gradle. build file:

apply plugin: 'maven'apply plugin: 'signing'signing {    sign configurations.archives}group = "com.kifile"archivesBaseName = "MaterialView"version = "1.0"uploadArchives {    repositories {        mavenDeployer {            beforeDeployment {                MavenDeployment deployment -> signing.signPom(deployment);            }            repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") {                authentication(userName: ossrhUsername, password: ossrhPassword)            }            snapshotRepository(url: "https://oss.sonatype.org/content/repositories/snapshots/") {                authentication(userName: ossrhUsername, password: ossrhPassword)            }            pom.project {                name 'MaterialView'                packaging 'aar'                description 'This project is build to let the view looks like a material design widget when touched. '                url 'https://github.com/Kifile/MaterialView'                scm {                    connection 'scm:git:https://github.com/Kifile/MaterialView.git'                    developerConnection 'scm:git:https://github.com/Kifile/MaterialView.git'                    url 'https://github.com/Kifile/MaterialView.git'                }                licenses {                    license {                        name 'The Apache License, Version 2.0'                        url 'http://www.apache.org/licenses/LICENSE-2.0.txt'                    }                }                developers {                    developer {                        id 'kifile'                        name 'Kifile Chou'                        email 'kifile@kifile.com'                    }                }            }        }    }}

In the above code, you can change the group, archiveBaseName, version, and uploadArchives # pom. project descriptions in the root domain to your own.

This information will be automatically packaged into a Maven project and the corresponding information will be encapsulated during the next upload.

Now we have configured the corresponding version information. Next we need to configure the signature and password required for the upload.

First, we need to create a gpg signature key and upload the public key to the specified server.

Under MacOS, we can download a tool called GPGTools (http://www.gpgtools.org) to generate our signature file.

After the tool is installed, we can see a software named GPG Keychains in Launchpad. After opening it, we choose to create a new key. See figure 2:


Figure 2 create a GPG key

After you click Create, enter the corresponding information and click Generate secret key. Then, we get the signature secret key, right-click the created secret key, and click Send public key to key server. you can upload the Local Public Key to the specified server. See figure 3.


Figure 3 uploading a public key to a public server

After the upload is complete, we can officially configure the signature information and your Maven account information. We create a new gradle. properties file under the root directory of the Library Module. The content is as follows:

Signing. keyId = enter the keyIdsigning displayed after the new key is created. password = enter your key password signing. secretKeyRingFile =/Users/your username /. gnupg/secring. gpgossrhUsername = your account name on sonatype ossrhPassword = your password on sonatype


Now that you have successfully configured the upload information, go to the directory where the Library Module is located through the command line and enter gradle uploadArchives to upload your file to the Maven central repository.


3. Publish your application

Do you think uploading to the Maven central database is over?

So far, you have uploaded your files to a private Maven server and cannot be accessed by others. If you want to officially release your application, continue with the following steps.

A. Go to the Maven project console.

Go to the following address: https://oss.sonatype.org/, and log on with the sonatype account and password in the upper right corner.

B. Search for the project you uploaded
If you log on successfully, you will see an option named Staging Repositories in the left-side navigation bar. Click it and you will find that many list options appear, such as 4.


Figure 4 repository list

Based on the List name, you will find a Repository named after your groud id is removed and a four-digit Repository is added. For example, this is called comkifile1000. select it, you will find that the Close button on the top is on. After you confirm that the upload is correct, you can click Close to Close the warehouse and not upload again. If it is disabled successfully, you can click Refresh to find that the Release button is on and click it to publish. If the failure is disabled, you can check the interface below to find the cause of the failure, upload the code again, close the code, and publish it.


Now, the entire Maven project release process has been completed.

Please note that the release does not take effect in real time. My code is about three hours before it can be retrieved in the Maven central repository. If you fail to retrieve the code in time after the release, please try again later.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.