You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将多仓库通用Gradle任务配置提取为插件或共享脚本?

问题

我们手里有好几个库,每个都带着重复的通用任务配置,不想再把这些配置复制粘贴到每个仓库里,想把它们抽成Gradle插件,但翻官方文档没找到具体怎么实现。

现在每个仓库里都有这段重复的jar任务配置:

jar {
    manifest {
        attributes(
                'OS': "${System.getProperty('os.name')}"
                // 还有一大堆其他配置项
        )
    }
}

我们想要的效果:

  1. 在中央仓库里定义这个插件,每个仓库只要引入就能自动获得配置好的jar任务,引入方式像这样:
plugins {
    id 'my-custom-plugin'
}
  1. 退一步说,用中央仓库里的共享脚本引入也可以,比如:
apply from: 'other.gradle'

解决方案

方案一:自定义Gradle插件(推荐)

1. 新建插件项目

搞一个单独的Gradle项目来做插件载体,结构大概是这样:

my-custom-plugin/
├── build.gradle
├── settings.gradle
└── src/
    └── main/
        ├── groovy/
        │   └── com/example/MyCustomPlugin.groovy
        └── resources/
            └── META-INF/
                └── gradle-plugins/
                    └── my-custom-plugin.properties

2. 编写插件核心逻辑

在MyCustomPlugin.groovy里把通用的jar配置写进去:

package com.example

import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.tasks.bundling.Jar

class MyCustomPlugin implements Plugin<Project> {
    void apply(Project project) {
        // 给所有Jar类型的任务配置通用逻辑
        project.tasks.withType(Jar) { jarTask ->
            jarTask.manifest {
                attributes(
                    'OS': System.getProperty('os.name')
                    // 这里把所有通用配置项都加上
                )
            }
        }
    }
}

3. 配置插件标识

在my-custom-plugin.properties里指定插件的实现类,让Gradle能找到它:

implementation-class=com.example.MyCustomPlugin

4. 把插件发布到中央仓库

在插件项目的build.gradle里配置发布规则(以Maven Central为例):

plugins {
    id 'java-gradle-plugin'
    id 'maven-publish'
}

gradlePlugin {
    plugins {
        myCustomPlugin {
            id = 'my-custom-plugin'
            implementationClass = 'com.example.MyCustomPlugin'
        }
    }
}

publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java
        }
    }
    repositories {
        maven {
            url = uri("https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/")
            credentials {
                username = System.getenv('SONATYPE_USERNAME')
                password = System.getenv('SONATYPE_PASSWORD')
            }
        }
    }
}

执行./gradlew publish就能把插件推去中央仓库了。

5. 业务仓库引入插件

在业务项目的build.gradle里直接引入就行(如果插件在Maven Central,Gradle默认会访问,不用额外配仓库):

plugins {
    id 'java'
    id 'my-custom-plugin' version '1.0.0'
}

方案二:共享脚本方式

1. 编写共享脚本

创建一个common-jar-config.gradle脚本,把通用的jar配置放进去:

jar {
    manifest {
        attributes(
            'OS': "${System.getProperty('os.name')}"
            // 所有通用配置都放这
        )
    }
}

2. 发布脚本到中央仓库

把这个脚本当成资源文件打包成jar,发布到Maven仓库。比如建个简单的Java项目,把脚本放在src/main/resources下,配置好发布逻辑推去中央仓库,发布后脚本的访问路径类似https://repo1.maven.org/maven2/com/example/common-config/1.0.0/common-config-1.0.0.jar!/common-jar-config.gradle。

3. 业务仓库引入脚本

在业务项目的build.gradle里直接通过URL引入:

apply from: 'https://repo1.maven.org/maven2/com/example/common-config/1.0.0/common-config-1.0.0.jar!/common-jar-config.gradle'

内容的提问来源于stack exchange,提问作者Zach

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 14:20:32