使用 Gradle 发布库项目

3,694 阅读4分钟

准备

常用 Maven 仓库

  • Maven central repository - repo1.maven.org/maven2
    搜索查询 mvnrepository.com/
    repositories { 
        mavenCentral() 
    }
    
  • Maven JCenter repository - jcenter.bintray.com
    搜索查询 bintray.com/bintray/jce…
    repositories { 
        jcenter() 
    }
    
  • Maven Google repository - maven.google.comdl.google.com/dl/android/…
    repositories { 
        google() 
    }
    
  • Maven Local repository - 默认地址是 "~/.m2/repository"
    repositories { 
        mavenLocal() 
    }
    
  • Custom Maven repository - 指定自定义仓库
    repositories {  
        maven { url "http://repo.yourcompany.com/maven" }
        // jcenter 官方仓库太慢了,推荐使用阿里云的 jcenter 镜像
        maven { url 'http://maven.aliyun.com/repository/jcenter' }
    }
    
  • Flat directory repository - 指定本地目录作为仓库
    repositories { 
        flatDir {
            dirs 'lib'
        }
        flatDir {
            dirs 'lib1', 'lib2'
        }
    }
    

参考
docs.gradle.org/current/use…

常用的发布相关插件

插件用法

使用plugins SDL应用插件

plugins {
    id "ezy.plugin.example" version "1.0.0"
}

使用buildscript解析插件并应用

buildscript {
  repositories {
    maven {
      url "https://dl.bintray.com/czy1121/maven"
    }
  }
  dependencies {
    classpath "ezy.example:gradleplugin:1.0.0"
  }
}

apply plugin: "ezy.example.gradleplugin"

发布

使用 maven 发布 Library

  1. 在 build.gradle 中添加 maven 插件 发布 Java Library
    plugins {  
        id "maven" 
    }
    
  2. 在 build.gradle 中添加 pom 配置信息
    group='ezy.example'
    version='1.0.0'
    archivesBaseName='publish2local'
    
  3. 使用gradle install命令可以发布到 Maven Local
  4. 使用gradle uploadArchives命令可以发布到自定义的远程仓库(比如~/.m2/repo)
    uploadArchives {
        repositories {
            mavenDeployer { 
                repository(url: 'file://' + System.properties['user.home'] + "/.m2/repo")
            }
        }
    }
    

使用 bintray 发布 Library 到 Maven JCenter repository

  1. 注册Bintray账号(bintray.com/login)
  2. 在Bintray后台添加一个maven类型仓库,名称为 maven
  3. 在Bintray后台获取API Key复制到~/.gradle/gradle.properties文件中
    BINTRAY_USER=你的Bintray用户名
    BINTRAY_KEY=你的Bintray API Key
    
  4. 在 build.gradle 中添加 bintray 和 android-maven 插件
  5. 在 build.gradle 中添加 pom 配置信息(groupId, artifactId, version)
  6. 在 build.gradle 中添加 bintray 配置参数
  7. 执行 gradlew install 生成POM,发布到JCenter需要POM
  8. 执行 gradlew bintrayUpload 将Library上传到Bintray
  9. bintray.com/bintray/jce… 点击按钮“Include My Package”申请将Library添加到JCenter
  10. 等待审核通过后就能在第三方库中引用了

附(build.gradle):

plugins {
    id "com.jfrog.bintray" version "1.8.5"
} 

apply plugin: "com.android.library"

group='ezy.example'
version='1.0.0'
archivesBaseName='lib2bintray'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"

    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 24
        versionCode 1
        versionName version

    }
    buildTypes {
        release {
            minifyEnabled false
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

uploadArchives {
    repositories {
        mavenDeployer {
            repository(url: uri('../repo'))
        }
    }
}

bintray {
    user = BINTRAY_USER
    key = BINTRAY_KEY
    configurations = ['archives']
    publish = true

    pkg {
        repo = "maven"                   // 发布到 Bintray 上的仓库名
        name = "ezy.example.lib2bintray" // 发布到 Bintray 上的项目名
        licenses = ["Apache-2.0"]
        vcsUrl = "https://github.com/czy1121/GradleDemo"
    }
}

使用 maven-publish 发布 Library

基本用法

maven-publish 总是会自动生成以下两个任务

  • publish,运行所有发布任务
  • publishToMavenLocal,发布所有出版物到本地默认仓库(通常是~/.m2/repository)

引用 maven-publish 插件并配置POM

plugins {
    id 'maven-publish" 
}

// ...

group='ezy.example'
version='1.0.0'
archivesBaseName='mavenpublish'

此时可以使用gradle publish或发布到本地默认仓库了

配置出版物和目标仓库

maven-publish 会根据出版物和目标仓库按以下规则生成任务

  • publish«PUBNAME»PublicationTo«REPONAME»Repository,用于发布出版物到指定仓库
  • publish«PUBNAME»PublicationToMavenLocal,用于发布出版物到本地默认仓库
  • generatePomFileFor«PUBNAME»Publication,用于生成POM

通过publishing.publications可配置出版物,比如MavenPublication
通过publishing.repositories可配置目标仓库

publishing {
    publications {
        library(MavenPublication) {
            artifactId = project.archivesBaseName
            // android 项目需要配置 aar
            artifact "build/outputs/aar/${artifactId}-release.aar"
        }
    }
    repositories {
        maven {
            name "custom"
            url "../repo"
        }
    }
}

上述代码生成了以下任务

  • publishLibraryPublicationToCustomRepository
  • publishLibraryPublicationToMavenLocal
  • generatePomFileForLibraryPublication

上传到 Bintray

将出版物加入 bintray.publications 就可以使用 gradle bintrayUpload 上传了

bintray {
    user = BINTRAY_USER
    key = BINTRAY_KEY
    publications = ['library']  // 指定出版物
    publish = true

    pkg {
        repo = "maven"                    // 发布到 Bintray 上的仓库名
        name = "ezy.example.mavenpublish" // 发布到 Bintray 上的项目名
        licenses = ["Apache-2.0"]
        vcsUrl = "https://github.com/czy1121/GradleDemo.git"
    }
}

使用 plugin-publish 发布 Gradle Plugin 到 Plugin Portal

plugins.gradle.org/docs/submit
plugins.gradle.org/

  1. 注册一个Gradle账号。
  2. 登录账号获取API Keys,将其复制到~/.gradle/gradle.properties
    gradle.publish.key=XXXXXX
    gradle.publish.secret=YYYYYY
    
  3. 在 build.gradle 中添加 com.gradle.plugin-publish 插件
  4. 在 build.gradle 中添加 pluginBundle 配置参数
  5. 执行 gradle publishPlugin 生成POM,将插件发布到 Gradle Plugin Portal
  6. 发布成功后就可以在Gradle中使用了(部分插件需要经过审核)

注:Gradle Plugin 发布到 Bintray 等仓库与普通库一样。

附(build.gradle):

plugins { 
    id "com.gradle.plugin-publish" version "0.12.0"
    id "java-gradle-plugin"
}
 
version='1.0.0'  
group = "'ezy.plugin"

gradlePlugin {
  plugins {
    gradleDemoPlugin {
      id = 'ezy.plugin.gradledemo'
      implementationClass = 'ezy.plugin.gradledemo.GradleDemoPlugin'
    }
  }
}

// 一个bundle可包含多个插件,plugin block 名称可随意取但必须唯一
// 每个插件包含字段 id, displayName, version, description, tags 
// 其中 displayName 是必填
// id 可从 java-gradle-plugin 获取
// version 不填默认使用项目的 version 
// description, tags 不填时使用 pluginBundle 的值
pluginBundle {
    website = 'http://github.com/czy1121/GradleDemo'
    vcsUrl = 'http://github.com/czy1121/GradleDemo.git'
    description = 'Gradle Demo Plugin'
    tags = ['demo']

    plugins {
        gradleDemoPlugin { 
            displayName = 'Gradle Demo Plugin'
        }
    } 
}