Gradle plugin upload Maven library configuration details

Gradle plugin upload Maven library configuration details


There are two ways to upload the Maven library by the Gradle plug-in, which are introduced in this article.

Old Maven Plugin

The original, outdated release mechanism provided in Gradle 1.0 deployed artifacts to Maven repositories.

If our project only generates the default jar file. Now, want to deploy this jar file to local Maven repository, how to do it?

build.gradle declaration

To use the Maven plugin plugin, you need to add a plugin reference statement to the project's build script build.gradle:

apply plugin: 'maven'

upload configuration

Local Maven library, upload configuration:

apply plugin: 'groovy'
apply plugin: 'maven'
 
dependencies {
    implementation gradleApi()
    implementation localGroovy()
}
uploadArchives{
    repositories.mavenDeployer {
        //本地仓库路径,url的值为存储仓库地址
        repository(url: "file://localhost/tmp/myRepo/") 
        //groupId ,自行定义
        pom.groupId = 'com.sensorsdata.myplugin'
        //发布到仓库的构建id
        pom.artifactId = 'MyPlugin'
        //发布到仓库的插件版本号
        pom.version = '1.0.0'
    }
}

Calling the uploadArchives task will generate the POM and deploy the artifacts and POM to the specified repository.

Gradle automatically generates a POM for a plugin when it is deployed to a Maven repository.

  • Defaults generated by Maven POM:

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-YG6ktGPD-1614254915170) (evernotecid://6FE75482-54A0-433A-9625-A01F7FEE92EC/appyinxiangcom/9896050/ENResource /p3164)]

  • apply plugin: The 'groovy' plugin is applied because our project is developed using the Groovy language, and the 'maven' plugin will be used when the plugin is released later.
  • dependencies: Declare dependencies.
  • uploadArchive: Here are some maven-related configurations, including the location of the release warehouse, groupId, artifactId, and version number. Here, the location is selected as the repo folder under the project root directory for debugging convenience.

Three elements of a Maven release:

  • groupId: group id, usually the company domain name reversed
  • artifactId: project name, you can also leave it blank, the default is the project name
  • version: version number

Maven Publish Plugin

In Gradle 1.3, a new publishing mechanism was introduced. This new mechanism introduces new concepts and features that make Gradle publishing much more powerful and is now the preferred option for publishing artifacts.

build.gradle declaration

Using the Maven Publish Plugin, you need to add code to the build script to enable the following plugins:

apply plugin: 'maven-publish'

upload configuration

Local Maven library, upload configuration:

apply plugin: 'maven-publish'

publishing {
    repositories {
        maven {
            credentials {
                username 'username' // 仓库发布用户名
                password 'password' // 仓库发布用户密码
            }
            url 'trunk_url' // 仓库地址
        }
    }
    publications {
        maven(MavenPublication) {
            groupId 'group_id' // groupId
            artifactId 'artifact_id' // artifactId
            version '1.0.0' // 发布版本
            description 'This is a liberary to test v1.0.0' // 说明描述
        }
    }
}

For example, an example of publishing to local is as follows:

apply plugin: 'maven-publish'
publishing {
    publications {
        mavenJava(MavenPublication) {
            //定义插件的在本地 maven 中的 id
            groupId 'com.budaye.plugin'
            artifactId 'aop-plugin'
            //定义插件的在本地 maven 中的版本号
            version '1.0.3'
            from components.java
        }
    }
}
publishing {
    repositories {
        maven {
            // 发布位置
            url uri('/Users/apple/Documents/localMaven')
        }
    }
}

Then, I can select and run the "publish" task under "publishing" in the Gradle task list, so that our Gradle plugin will be published locally.


**PS: For more performance optimization related articles, please view --> "Android Performance Optimization"
**PS: For more performance optimization related articles, please view --> "Android Performance Optimization"
**PS: For more performance optimization For related articles, please view --> "Android Performance Optimization"

Guess you like

Origin blog.csdn.net/u011578734/article/details/114104495