Gradle 7.0+发布Gradle插件到私有maven仓库笔记

最近用Java写了一个Gradle插件

插件介绍

https://blog.csdn.net/wang382758656/article/details/124236006?spm=1001.2014.3001.5501

然后想在各个项目中使用,发现gradle 7 以后的脚本变化挺大的,折腾了一天,整理出一个完整的脚本,这里做一个笔记供大伙参考。

首先,我的插件是以module方式开发的,然后用nexus(这是一个软件,不是nexus手机,不要懵逼,我用的是nexus-3.38.1-01win64)搭建了私有库,所以所有的配置都在module的build.gradle中配置

配置1.plugins 的配置,新版本是maven-publish以前是maven

plugins {
    id 'java-gradle-plugin'  //Gradle开发插件
    id 'java'              //使用java
    id 'maven-publish'       //插件发布
}

配置2.gradlePlugin 配置

这个主要是配置插件的信息,这里的版本号,并不代表发布的版本号

gradlePlugin {
    plugins {
        //定义的信息不一定和发布信息相同
        MethodTracer {
            id = 'com.sand.mt'                                //定义插件id
            version = '1.0.2'
            implementationClass = 'com.sand.mt.src.plug.MTPlug'  //定义插件实现类
        }
    }
}

配置3.也是最关键的

1.发布到本地仓库的配置,可以看到这里并不需要配置仓库地址,用户名密码之类

publishing {

    // 定义发布什么
    publications {
        plugin(MavenPublication) {
            from components.java //使用默认的配置生成jar包
            groupId = 'com.sand.group'     //这三部分组成了:com.example:plugin:1.0.0-SNAPSHOT
            artifactId = 'mt'
            version = '1.0.0'
        }
    }
    // 定义发布到哪
    // publishPluginPublicationToRepoRepository 发布到 plugin/build/repo
    // publishPluginPublicationToMavenLocal 发布到 ~/.m2/repository
    repositories {
        maven {
            name = 'repo'
            url = layout.buildDirectory.dir("repo")
        }
    }
}

2.远程仓库配置

publishing {
    publications {
        maven(MavenPublication) {
            groupId = "com.sand.group"
            artifactId = "mt"
            version = "1.0.2"
            description "MTPlug is a good apm tool"
            from components.java
        }
    }

    repositories {
        maven {
            allowInsecureProtocol true
            url = '你的maven仓库url'
            credentials {
                it.username = "username"
                it.password = "pwd"
            }
        }
    }
}

引用

如果发布到本地,那么根目录的repositories 中添加mavenLocal()

mavenLocal()

远程仓库配置

        maven {
            allowInsecureProtocol = true
            url '你发布的时候的仓库地址'
            //如果设置了匿名访问,就不需要配置下面的credentials部分
            credentials {
                it.username = "username"
                it.password = "pwd"
            }

        }

完整的module配置文件

plugins {
    id 'java-gradle-plugin'  //Gradle开发插件
    id 'java'              //使用Kotlin
    id 'maven-publish'       //插件发布
}

dependencies {
    implementation "com.android.tools.build:gradle:7.0.3"
    implementation files('libs/asm-9.2.jar')
    implementation files('libs/asm-commons-9.2.jar')
    implementation files('libs/asm-util-9.2.jar')
    implementation files('libs/commons-io-2.6.jar')  //使用com.android.build.api
}

//
gradlePlugin {
    plugins {
        //定义的信息不一定和发布信息相同
        MethodTracer {
            id = 'com.sand.mt'                                //定义插件id
            version = '1.0.2'
            implementationClass = 'com.sand.mt.src.plug.MTPlug'  //定义插件实现类
        }
    }
}


本地发布
//publishing {
//
//    // 定义发布什么
//    publications {
//        plugin(MavenPublication) {
//            from components.java //使用默认的配置生成jar包
//            groupId = 'com.sand.group'     //这三部分组成了:com.example:plugin:1.0.0-SNAPSHOT
//            artifactId = 'mt'
//            version = '1.0.0'
//        }
//    }
//    // 定义发布到哪
//    // publishPluginPublicationToRepoRepository 发布到 plugin/build/repo
//    // publishPluginPublicationToMavenLocal 发布到 ~/.m2/repository
//    repositories {
//        maven {
//            name = 'repo'
//            url = layout.buildDirectory.dir("repo")
//        }
//    }
//}


//远程发布, 发布到我自己的私有库
publishing {
    publications {
        maven(MavenPublication) {
            groupId = "com.sand.group"
            artifactId = "mt"
            version = "1.0.2"
            description "MTPlug is a good apm tool"
            from components.java
        }
    }

    repositories {
        maven {
            allowInsecureProtocol true
            url = 'http://161.117.195.45:6677/repository/sand_repo/'
            credentials {
                it.username = "username"
                it.password = "pwd"
            }
        }
    }
}

完整的根目录biuild.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        //本地依赖的关键代码
        mavenLocal()
        maven {
            allowInsecureProtocol = true
            url 'http://161.117.195.45:6677/repository/sand_repo/'
        }
        google()
        mavenCentral()
        maven { url 'https://dl.google.com/dl/android/maven2/' }
        maven { url 'https://www.jitpack.io' }
        jcenter()
        maven { url 'https://jcenter.bintray.com' }

    }
    dependencies {
        classpath "com.android.tools.build:gradle:7.0.4"
        classpath "com.sand.group:mt:1.0.2" //来自自定义仓库
    }
}

allprojects{

    repositories{
//        //本地依赖的关键代码
        maven {
            allowInsecureProtocol = true
            url 'http://161.117.195.45:6677/repository/sand_repo/'
        }
        jcenter()
        google()
        mavenLocal()
        mavenCentral()
        maven { url 'https://dl.google.com/dl/android/maven2/' }
        maven { url 'https://www.jitpack.io' }
    }


}


task clean(type: Delete) {
    delete rootProject.buildDir
}

工程源码

https://github.com/woshiwzy/MTDemo

猜你喜欢

转载自blog.csdn.net/wang382758656/article/details/124258664
今日推荐