[Gradle-9] Gradle plug-in release guide

1 Introduction

Whether within the company or open source, Gradle plug-in publishing is an essential skill. This article mainly introduces two methods: local publishing and remote publishing.

2. Local release

2.1. Add dependencies

In the plugin>build.gradle file (the plug-in project), first rely on a plug-in published by maven'maven-publish'

plugins {
    
    
    id 'maven-publish'
}

dependencies {
    
    
    implementation 'com.android.tools.build:gradle:7.3.0'
}

2.2. Release configuration

Maven's release configuration is publishing { }set in the closure.
Local publishing mainly consists of two parts:

  • GAV coordinates
  • Warehouse Address
group 'com.yechaoa.plugin'
version '1.0.0'

publishing {
    
    
    // 配置Plugin GAV
    publications {
    
    
        maven(MavenPublication) {
    
    
            groupId = group
            artifactId = 'dependencies'
            version = version

            from components.java
        }
    }
    // 配置仓库地址
    repositories {
    
    
        maven {
    
    
            url layout.buildDirectory.dir("maven-repo")
        }
    }
}

2.3. Execute release

After the above configuration is completed, the publishing operation can be performed:

./gradlew publish

Or click Run in the Gradle visualization panel on the right side of Android Studio publish:
publish.png

2.4. Generate products

unwanted.png
OK, now there is maven-repoa folder for local release configuration under the build folder.
Mainly confirm that the jar/aar file, pom file, metadata, etc. are correct.

2.5. Use

Ok, the local release is completed. If you want to use this plug-in, the process is the same as our normal dependence on plug-ins.
Three steps:

  1. Configure the plug-in warehouse address in the settings.gradle file
pluginManagement {
    
    
    repositories {
    
    
        // ...
        maven {
    
    
            url './maven-repo'
        }
    }
}
  1. Add plugin dependencies in the project>build.gradle file
buildscript {
    
    
    dependencies {
    
    
        classpath('com.yechaoa.plugin:dependencies:1.0.0')
    }
}
  1. Depend on our plugin in the app:build.gradle file
plugins {
    
    
    id 'com.yechaoa.plugin.dependencies'
}

The above configurations are all added in the app module, which is the module that needs to be used.

Note: When using local dependencies, they must be published first and then depend on the plug-in, otherwise cannot foundthe dependencies may not be found.

3. Maven remote release

Maven is a software project management tool based on the concept of the Project Object Model (POM). Maven can manage the project's build, reports and documents through central information, and is also the most commonly used repository.

We have completed local publishing above. Maven remote publishing mainly requires various configurations.
Based on local publishing, remote publishing has several differences:

  • Register a warehouse account;
  • Configure POM;
  • sign;
  • Remote warehouse address;
  • Prepare domain name;

It's more complicated, that's it. . I didn’t accompany you through the entire process.

An example:

plugins {
    id 'java-library'
    id 'maven-publish'
    id 'signing'
}

group = 'com.example'
version = '1.0'

java {
    withJavadocJar()
    withSourcesJar()
}

publishing {
    publications {
        mavenJava(MavenPublication) {
            artifactId = 'my-library'
            from components.java
            versionMapping {
                usage('java-api') {
                    fromResolutionOf('runtimeClasspath')
                }
                usage('java-runtime') {
                    fromResolutionResult()
                }
            }
            pom {
                name = 'My Library'
                description = 'A concise description of my library'
                url = 'http://www.example.com/library'
                properties = [
                    myProp: "value",
                    "prop.with.dots": "anotherValue"
                ]
                licenses {
                    license {
                        name = 'The Apache License, Version 2.0'
                        url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                    }
                }
                developers {
                    developer {
                        id = 'johnd'
                        name = 'John Doe'
                        email = '[email protected]'
                    }
                }
                scm {
                    connection = 'scm:git:git://example.com/my-library.git'
                    developerConnection = 'scm:git:ssh://example.com/my-library.git'
                    url = 'http://example.com/my-library/'
                }
            }
        }
    }
    repositories {
        maven {
            // change URLs to point to your repos, e.g. http://my.org/repo
            def releasesRepoUrl = layout.buildDirectory.dir('repos/releases')
            def snapshotsRepoUrl = layout.buildDirectory.dir('repos/snapshots')
            url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
        }
    }
}

signing {
    sign publishing.publications.mavenJava
}


javadoc {
    if(JavaVersion.current().isJava9Compatible()) {
        options.addBooleanOption('html5', true)
    }
}

For the whole process, you can refer to:

4、JitPack

If your project is hosted using GitHub, then the most commonly used publishing method is to use JitPack . It is a simple and easy-to-use Git package repository. It is also very simple to use and publish. There is no need to register a website account and perform a lot of verifications. What's more, it is also non-intrusive to the project and does not require a server or domain name.

4.1. Use GitHub hosting

The most basic thing is to use GitHub to host the project, that is, upload the project to GitHub, either Public or Private.
For example, my GradleX project:
github.png

4.2. Release

You don't even do anything, you just upload a project and you can go directly to the release process.
release.png

In the menu list on the right of the project warehouse homepage, find Releases and click "Create a new release".
If it has been released, here is the release record. Click on it to "Draft a new release".
publish.png

  • Set tag, which is the version number. If it is not an official version, you can add alpha at the end, such as 1.0-alpha;
  • Select the release branch;
  • publish title;
  • Release description;
  • Click "Publish release" to publish;

There will be products and records after the release is completed
Products and records.png

4.3. Use

The release above is complete, so how to use it?
Open the Jitpack official website: https://www.jitpack.io/yechaoa/GradleX and enter username+project in the input box .
Then click Look upStart Find and Build.
get it.png
After the construction is completed and the Log log is available, you can click Get itto obtain the usage method.
How to use:

  1. First, add the warehouse address of jitpack;
  2. Then add project dependencies, because it is released based on GitHub, the domain name prefix will be com.github, and customization needs to add mapping;

When adding dependencies, since we are publishing the entire project, which contains multiple modules, we can also select submodules for individual dependencies.
For example, in my project:
dependencies.png
my project is GradleX, the plug-in module is plugin, and the plug-in is dependencies. According to the principle of minimum, we can directly choose to rely on plug-in dependencies. When there are multiple plug-ins later, we can choose to depend on the plugin module.

4.4. Verification

Now that it’s released, let’s verify it and give it a try.

First add the warehouse in settings.gradle:

	repositories {
    
    
        maven {
    
     url 'https://jitpack.io' }
    }

Then add dependencies:

	dependencies {
    
    
        classpath('com.github.yechaoa.GradleX:dependencies:1.0')
    }

Because we rely on plug-ins, the implementation needs to be replaced with classpath.

Finally, depend on the plug-in and open the plug-in configuration:

plugins {
    id 'com.yechaoa.plugin.dependencies'
}

printDependencies {
    enable = true
}

Then let's sync and take a look.

> Configure project :app
DependenciesPlugin >>>>> com.yechaoa.plugin.DependenciesPlugin
---hasProperty isTest no
DependenciesPlugin >>>>> 已开启依赖打印
DependenciesPlugin >>>>> applicationVariant.getName() = debug
Run with --info for a stacktrace.
--------------官方库 start--------------
com.google.android.material:material:1.8.0
androidx.appcompat:appcompat:1.5.0
org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.7.10
androidx.core:core-ktx:1.8.0
......

As you can see, the expected output is already there.
Because sync will trigger the Gradle build, the plug-in that prints dependencies during compilation will take effect and print the dependencies in the project.

ok, that’s all the release methods of jitpack, isn’t it very simple~

5、Gradle Plugin Portal

The Gradle plug-in portal is a centralized, searchable repository dedicated to Gradle plug-ins. The release process and configuration are much simpler than Maven.

5.1. Register an account

First, you need to register an account on the Gradle plug-in portal, https://plugins.gradle.org/user/login . We can log in directly with our Github account.
gradle protal.png
After GitHub performs 2FA double verification, you need to verify it again when using GitHub to log in. Just follow the process, it is not complicated.

5.2. Obtain the secret key

Generate a secret key under the API Keys option.
secret key.png

5.3. Configure secret key

The secret key is used for publishing and is usually placed HOME_DIR/.gradle/gradle.properties (~/.gradle/gradle.properties)in a file. If it does not exist, create a new one. Of course, it can also be placed in other effective places, but I still recommend putting it here so that it can be used globally.
If you do not configure this step, you can also manually execute the release command and then bring the secret key by passing parameters:

$ ./gradlew publishPlugins -Pgradle.publish.key=<key> -Pgradle.publish.secret=<secret>

5.4. Release configuration

5.4.1. Dependence on publishing plug-ins

If the gradle plug-in portal is published, it has its own publishing plug-in.

plugins {
    id 'com.gradle.plugin-publish' version '1.1.0'
}

The usage of publishing plug-in version 1.0.0 and above requires Gradle 7.6 and above.
However, version 1.0.0 and above also bring many conveniences, such as:

  • It contains 'java-gradle-plugin' and 'maven-publish' plugins;
  • Automatically generate and publish source code and documentation;
  • Signing is also automatic;

5.4.2. Configure release information

Because we relied on the plug-in when publishing locally id 'java-gradle-plugin', it saves trouble here. Just continue to configure it directly in gradlePlugin { }.

gradlePlugin {
    // 项目地址
    website = 'https://github.com/yechaoa/GradleX'
    // 项目仓库URI
    vcsUrl = 'https://github.com/yechaoa/GradleX.git'

    // 可以有多个plugin配置
    plugins {
        // register 这个名字可以随便填
        register {
            // 插件id
            id = 'com.yechaoa.plugin.dependencies'
            // 插件全路径
            implementationClass = "com.yechaoa.plugin.DependenciesPlugin"
            // 插件名称
            displayName = 'DependenciesPlugin'
            // 描述、简介
            description = 'Show Dependencies Plugin'
            // 插件类别,即标签、关键词的意思
            tags.addAll('Dependencies', 'yechaoa', 'plugin')
        }
    }
}

The above configuration requires Gradle version 7.6 and above. Older versions use Gradle pluginBundleinstead gradlePlugin.
@since 7.6:

    /**
     * Returns the property holding the URL for the plugin's website.
     *
     * @since 7.6
     */
    @Incubating
    public Property<String> getWebsite() {
        return website;
    }

Post-release example:
image.png

5.5. Execute release

After the above configuration is completed, sync it.
Issue the command:

./gradlew publishPlugins

If there is no secret key:

./gradlew publishPlugins -Pgradle.publish.key=<key> -Pgradle.publish.secret=<secret>

Or double-click publishPlugins in the Gradle panel to execute:
Double-click to execute.png
the next step is to wait for review. There are some requirements for review, such as versions that are too simple and meaningless helle word, SNAPSHOTetc.
Once approved, you can search it on the Gradle plug-in portal .

5.6. Reference method

After 7.0:

plugins {
  id "com.yechaoa.plugin.dependencies" version "1.0.0"
}

Before 7.0:

buildscript {
  repositories {
    maven {
      url "https://plugins.gradle.org/m2/"
    }
  }
  dependencies {
    classpath "com.yechaoa.plugin:dependencies:1.0.0"
  }
}

apply plugin: "com.yechaoa.plugin.dependencies"

After 7.0, because the address pluginManagement{ }is added by default gradlePluginPortal(), the configuration is simpler.

7. Summary

This ends the introduction to Gradle plug-in release. Although the Maven release method is very cumbersome, it is only the first time. After the configuration is completed, it can be extracted into a script and executed every time it is released. However, JitPack is still recommended first. After all, it’s so convenient.

8. Finally

Writing is not easy, thank you for your support~

9、GitHub

https://github.com/yechaoa/GradleX

10. Related documents

Guess you like

Origin blog.csdn.net/yechaoa/article/details/133256952