[Publish AAR to maven] use Gradle to publish AAR to maven warehouse

Source: Internet
Author: User
Tags gpg sonatype

========================================================
Qiujuer
Blog: Blog.csdn.net/qiujuer
Website: www.qiujuer.net
Open Source Library: github.com/qiujuer/genius-android
Reprint Please specify source: http://blog.csdn.net/qiujuer/article/details/44195131
--open source of learning, for open source, beginner's mentality, with June mutual encouragement!

========================================================

A good library wants to share with the world's partners, so it's shared on GitHub, and someone sees it, but it's hard to find a configuration to use. But finding someone else's library requires just one line of code, and I want to know why it's so simple.
In this article you will learn how to publish your library to remote repository maven and how to use it.

Get ready

Before we get started, we have some preparation, and if we're ready, then it's going to be very smooth.

    • Sign up for your https://issues.sonatype.org account and create your Group Id
    • Learn to use Gpgtools and generate your key pair; see: Signing and Encrypting files using GPG
    • Write your library and ready to publish, tools: Android-studio
    • Add Gradle file to publish Maven
    • Add your personal information, such as your publishing address, library version information, etc.
    • Development release snapshot, and official version, audit
Begin

Two of the previous work because of a large space, the opening of a separate, welcome to see ~ ~, we start from the third place.

Writing Lib

Write the library, here I use Android-studio; As for its use of the establishment process and simple to use everyone can be seen: the environment configuration of the official version of Android Studio 1.0

Here I use the genius-android Open Source library for demonstration purposes.

In this project we try to publish the UI section, which is circled in part for the files we need.

Writing a publishing file

Publishing a file is one of the maven_push.gradle . Since the code is longer, you can go to GitHub to view it.

The code has been published in the Befoot Open source project.
The code runs in Gradle, which is to package the code file according to the configuration, then sign the file, and finally publish your file to the repository.

There are a number of methods and parameters that can be seen in the code, such as:

def isReleaseBuild() {    return VERSION_NAME.contains("SNAPSHOT"false}

The purpose of this method is to determine if the version name contains a "SNAPSHOT" field, which is to determine whether a snapshot version is currently being published for transfer to a different server address.
But where does the "Version_name" come from? Look down ~ ~

Gradle.properties

All fields in the "Maven_push" file are access to your "gradle.properties" file.

You can see that there are two of these files in our project, and the contents of the two files can all be written in one file. The reason for writing two, the following one represents the global variables in this project, as for the uilib is confined to the use of uilib.

Now let's open it up and open the following global file first.

Here are a few places where we need to change the following:

Then look at the files in the library:

It is relatively simple here, nothing more than the name, ID, type, version and other information.
It is necessary to note that the above pom_group_id+pom_artifact_id+version_name will be used as follows:

{    compile ‘com.github.qiujuer:genius-ui:3.0.0-SNAPSHOT‘}

Finished? No, there is a crucial place, in the "Maven_push" code we will find:

And the signing part of it, but you will find that you have searched all the files above without discovering these fields, so where are these fields?

Quite simply, the ". Gradle" directory is explained in the Android-studio cache folder configuration, right? So now let's go and see what's in this directory.

You can see that there is also a "gradle.properties" file, the file is not in the project, but can be accessed in the project, this is a global file, any project can access the file, if you do not, then you can create one yourself OK.

Why do you want to store it here? We all know that the "gradle.properties" file in the project is generally committed to Git, so the files in the project should not contain private items such as key pairs, key passwords, and your Maven account information.

Open the file we can see:

    • See the key related things: Use GPG to sign files for encryption
    • Sonatype User name password see: Register for Maven warehouse sonatype.org account
Build.gradle

Now you also need to modify the build file in your project, where you need to change the following:

android {    sourceSets {        main {            ‘src/main/AndroidManifest.xml‘            java.srcDirs = [‘src/main/java‘]            res.srcDirs = [‘src/main/res‘]        }    }    lintOptions {        false    ‘https://raw.github.com/qiujuer/BeFoot/master/blog/gradle-mvn-push/gradle-mvn-push.gradle‘
Release

If you have done all of them, then you are now in the publishing lot ~ ~

Execute Publish command

Android-studio into the project, open the "Terminal" window, enter your project and execute the command:

$ gradle clean build uploadArchives


The first operation will download a lot of dependencies.

The last success prompt is OK.

Check the snapshot version

Here we go: https://oss.sonatype.org/content/groups/public/
Find your project address, I am this time:
https://oss.sonatype.org/content/groups/public/com/github/qiujuer/genius-ui/3.0.0-SNAPSHOT/

You can see that there are original files and signatures and other validation files.
If you do not have an "ASC" file, you will not be able to pass the "release" version.

This is not to say that the snapshot version is not available, and it can be used as follows:

dependencies {    repositories {        "https://oss.sonatype.org/content/repositories/snapshots/" }    }    ‘com.github.qiujuer:genius-ui:3.0.0-SNAPSHOT‘}

If a snapshot version has been published, it is:

‘com.github.qiujuer:genius-ui:3.0.0-SNAPSHOT-2‘
Release "Release"

The Publish method operates on the same command line as the snapshot version, and only needs to change the version_name in the:gradle.properties file in your library, removing the ""-snapshot from the back. "just OK."

After executing the command line command, then you will find that you are in:
https://oss.sonatype.org/content/groups/public/
Cannot find your release version of "release"; it's normal; if you find it now it's not normal.

Operations Warehouse Release

To complete the release release, you need to log in to the background to do some simple things.
Website: https://oss.sonatype.org/
Login: Username and password are exactly the same as in https://issues.sonatype.org/.
After successful login, click on the left navigation:"Staging repositories"

In which according to your GroupId to find your corresponding line. such as my ID: "Com.github.qiujuer" then is:

Find and click on him if it is not yours or you do not find it then see if you are not on top of the upload success.

When selected, click on the "Close" button above to fill in the description information, then wait for a while, and in the process you can click the Refresh button.

Below you can see the email alert sent, and then the "Release" button can be clicked, at this point, click on the button, fill in the description information, not too much. And then wait, and when the email alerts are sent below, congratulations on your successful release.

If you do not have a signature file in the file you uploaded above, you will not be able to operate successfully; You should re-upload the signed version.

After the successful release you use the method (you can not immediately use, usually takes 10 minutes and more than the time):

{    compile ‘com.github.qiujuer:genius-ui:3.0.0‘}
Attachment

I uploaded the files needed for this operation to GitHub.
Address: Https://github.com/qiujuer/BeFoot/tree/master/blog/gradle-mvn-push

Some questions

The place, temporarily do not write, too long ~ ~
Say a simple: if you execute the Publish command:401 error, the login failed on behalf of your account, that is, your account name or password error.

Other questions, if you come across it, I'll answer them all.

Some digression

To tell the truth this article, has long been wanted to write, but probably thought about a bit of space will be relatively large, so has not written.

This is also a friend to ask this aspect of the situation, so it took a few days to write, it really took a few days, not free time. Maybe you can look at it in 10 minutes, maybe 10 minutes, but I've spent 100 times times as much as you wrote.

Because it is written in the MarkDown editor (with fewer cases of accidental bug death), it does not add highlighting and so on as before, but the editor does not support it.

========================================================
Qiujuer
Blog: Blog.csdn.net/qiujuer
Website: www.qiujuer.net
Open Source Library: github.com/qiujuer/genius-android
Reprint Please specify source: http://blog.csdn.net/qiujuer/article/details/44195131
--open source of learning, for open source, beginner's mentality, with June mutual encouragement!

========================================================

[Publish AAR to maven] use Gradle to publish AAR to maven warehouse

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.