Although there are many articles on this topic, there are many comments on how to use GPG in Mac OS; The upload script is difficult to configure. This article will address these issues.

The article will follow the overall process, but will focus on the use of GPG in Mac OS and scripting templating.

Apply for GroupId right of use

  1. Issues.sonatype.org Register an account

    Create Issue with type Project

  1. Fill in the information

There are two values for the Group Id:

  • If you have a publicly accessible domain name, fill in your domain name
  • If github exists, enter IO. Github. Username (username is the username).
  1. Take note of the response and follow the instructions

More details can be found at juejin.cn/post/695359…

GPG generates the key

All commands are run on the Mac OS

  1. Install gnupg

    brew install gnupg
    Copy the code
  1. Generating a key pair

    gpg --full-gen-key
    Copy the code

    The key type can be selected as required. Validity period of the key. For convenience, you are advised to set it to never expire.

  2. Upload the public key

    The private key must never be disclosed. The public key is used by others to verify that you have signed with your private key. Therefore, the public key needs to be uploaded to the public key server.

    gpg --keyserver keyserver.ubuntu.com --send-keys B5C2D2AF
    Copy the code

    –keyserver: specifies the public keyserver

    –send-keys: the key ID can be specified

    Due to domestic science online, some servers may not be accessible, offering two other options:

    • keys.openpgp.org
    • pgp.mit.edu
  3. Export key

    Export the key for easy use, such as CI.

    gpg --export-secret-keys B5C2D2AF > B5C2D2AF.gpg
    Copy the code

    –export-secret-keys: specifies the key ID

Upload script

  1. Create the scripts directory in the root directory where you want to upload the Module
  2. Create a publish. Gradle file in the scripts directory you created
  3. Copy upload script
project.plugins.apply('maven-publish') project.plugins.apply('signing') def localProps=new Properties() localProps.load(project.file('local.properties').newDataInputStream()) Enumeration enumeration = localProps.keys(); while (enumeration.hasMoreElements()) { def key = enumeration.nextElement(); def value = localProps.get(key); project.properties[key]=value } afterEvaluate { publishing { publications { release(MavenPublication) { groupId = 'io.github. Wslaimin 'artifactId = 'RVAdapter' version = '1.0.0' from components. Release // Generate pom  'RVAdapter' description = 'Simply defining adapters of RecyclerView in Android' url = 'https://github.com/wslaimin/RVAdapter.git' licenses { license { name = 'The Apache License, Version 2.0 developers' url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'}} {developer {id = 'wslaimin' name = 'TFly' email = '[email protected]' } } scm { url = 'https://github.com/wslaimin/RVAdapter.git' } } } } repositories { maven { // change to point to your repo def releaseRepo = 'https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/' def snapshotRepo = 'https://s01.oss.sonatype.org/content/repositories/snapshots/' url = publishing.publications.release.version.endsWith('SNAPSHOT') ? snapshotRepo : releaseRepo credentials { username = project.properties['credencial.username'] password = project.properties['credencial.password'] } } maven { name 'Build' url = layout.buildDirectory.dir('repo') } } } if(project.properties['signing.keyId']&&project.properties['signing.password']&&project.properties['signing.secretKeyRin gFile']){ signing { sign publishing.publications.release } } }Copy the code
  1. Add the password to the local. Properties directory (if not created) in which you want to upload the Module

    credencial.username=
    credencial.password=
    ​
    signing.keyId=
    signing.password=
    signing.secretKeyRingFile=
    Copy the code

    Credencial. Username: Indicates the Sonatype account

    Credencial. Password: Indicates the Sonatype password

    Signing. keyId: ID of the generated key

    Signing. password: indicates the key password

    Signing. SecretKeyRingFile: export key file location

  1. Reference the upload script in build.gradle where the module needs to be uploaded

    apply from: './scripts/publish.gradle'
    Copy the code

After the above steps are complete and the project is synchronized, you can see the publish related tasks:

The build directory publishReleasePublicationToBuildRepository: deployed to the project

PublishReleasePublicationToMavenLocal: deployed to the local maven repository

PublishReleasePublicationToMavenRepository: deployed to a remote maven repository

Refer to this project for script and password configuration

Script templating

It is obviously inconvenient to script and configure passwords for each new project. Therefore, scripts can be templated to simplify the configuration process.

Open the Live Templates configuration for Android Studio.

  1. Create an upload script template

Once the template is created, type pub in the created publish.gradle to quickly introduce the upload script.

  1. Creating a password Template

    The file type is Other.

That’s all, Enjoy it!