gradle7发布Gradle 插件脚本

最近用 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")
        }
    }
}

Android gradle 插件打印时间戳的方法详解 pdf 0 星 超过 10% 的资源 221KB 下载

  1. 远程仓库配置
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

posted @   mahy  阅读(579)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· 2 本地部署DeepSeek模型构建本地知识库+联网搜索详细步骤
点击右上角即可分享
微信分享提示