diff --git a/build.gradle.kts b/build.gradle.kts index d3d1c0d..3cebfeb 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -8,7 +8,7 @@ plugins { } group = "io.izzel.taboolib" -version = "2.0.23" +version = "2.0.25" configurations { create("embed") { diff --git a/docs/custom-repositories.md b/docs/custom-repositories.md new file mode 100644 index 0000000..f423916 --- /dev/null +++ b/docs/custom-repositories.md @@ -0,0 +1,194 @@ +# 自定义仓库配置 + +TabooLib Gradle Plugin 支持添加自定义 Maven 仓库,以便从私有仓库或其他第三方仓库获取依赖。 + +## 基本用法 + +在你的 `build.gradle` 或 `build.gradle.kts` 文件中,可以通过 `taboolib` 配置块添加自定义仓库: + +### Groovy DSL + +```groovy +taboolib { + // 添加自定义仓库 + repository("my-repo", "https://my-custom-repo.com/maven") + repository("private-repo", "https://private.company.com/nexus") + + // 其他配置... + version { + taboolib = "6.0.12" + } +} +``` + +### Kotlin DSL + +```kotlin +taboolib { + // 添加自定义仓库 + repository("my-repo", "https://my-custom-repo.com/maven") + repository("private-repo", "https://private.company.com/nexus") + + // 其他配置... + version { + taboolib = "6.0.12" + } +} +``` + +## 方法说明 + +### `repository(name, url)` + +添加一个自定义仓库到项目中。 + +**参数:** +- `name` (String): 仓库名称,用于标识仓库 +- `url` (String): 仓库的 URL 地址 + +**示例:** +```groovy +repository("aliyun", "https://maven.aliyun.com/repository/central") +``` + +## 常见用例 + +### 1. 使用阿里云镜像加速 + +```groovy +taboolib { + repository("aliyun-central", "https://maven.aliyun.com/repository/central") + repository("aliyun-public", "https://maven.aliyun.com/repository/public") +} +``` + +### 2. 添加私有企业仓库 + +```groovy +taboolib { + repository("company-nexus", "https://nexus.company.com/repository/maven-public/") + repository("company-releases", "https://nexus.company.com/repository/maven-releases/") +} +``` + +### 3. 使用 JitPack 仓库 + +```groovy +taboolib { + repository("jitpack", "https://jitpack.io") +} +``` + +### 4. 添加 Sonatype 快照仓库 + +```groovy +taboolib { + repository("sonatype-snapshots", "https://oss.sonatype.org/content/repositories/snapshots/") +} +``` + +## 仓库优先级 + +自定义仓库会在项目评估(`afterEvaluate`)阶段添加到项目中,因此它们的优先级会高于在 `taboolib` 配置块之后添加的仓库。 + +默认情况下,插件会自动添加以下仓库: +1. `https://repo.tabooproject.org/repository/releases/` (TabooLib 官方仓库) +2. `https://repo.spongepowered.org/maven` (Sponge 仓库) + +自定义仓库会在这些默认仓库之后添加。 + +## 认证配置 + +对于需要认证的私有仓库,需要在 Gradle 的全局配置中设置凭据: + +### 方式一:在 `~/.gradle/gradle.properties` 中配置 + +```properties +myRepoUsername=your-username +myRepoPassword=your-password +``` + +然后在 `build.gradle` 中引用: + +```groovy +repositories { + maven { + url = "https://private.company.com/nexus" + credentials { + username = project.findProperty("myRepoUsername") ?: System.getenv("REPO_USERNAME") + password = project.findProperty("myRepoPassword") ?: System.getenv("REPO_PASSWORD") + } + } +} +``` + +### 方式二:使用环境变量 + +```bash +export REPO_USERNAME=your-username +export REPO_PASSWORD=your-password +``` + +**注意:** 当前 `repository()` 方法只支持添加简单的 Maven 仓库 URL。如果需要认证或其他高级配置,请直接在 `repositories` 块中配置。 + +## 完整示例 + +```groovy +plugins { + id 'io.izzel.taboolib' version '2.0.23' +} + +taboolib { + // 添加自定义仓库 + repository("aliyun-central", "https://maven.aliyun.com/repository/central") + repository("jitpack", "https://jitpack.io") + + // 版本配置 + version { + taboolib = "6.0.12" + coroutines = "1.7.3" + } + + // 环境配置 + env { + install("common", "common-util", "platform-bukkit") + } + + // 描述配置 + description { + name = "MyPlugin" + version = "1.0.0" + main = "com.example.MyPlugin" + author = "YourName" + depend = ["TabooLib"] + } +} +``` + +## 故障排除 + +### 1. 仓库无法访问 + +如果遇到仓库无法访问的问题,请检查: +- 网络连接是否正常 +- 仓库 URL 是否正确 +- 是否需要认证凭据 + +### 2. 依赖解析失败 + +如果依赖解析失败,请确保: +- 自定义仓库中确实包含所需的依赖 +- 依赖的坐标(group:artifact:version)是否正确 +- 仓库的访问权限是否正确配置 + +### 3. 构建缓慢 + +如果构建速度较慢,建议: +- 使用地理位置更近的镜像仓库 +- 检查网络连接速度 +- 考虑使用企业级 Maven 仓库管理器 + +## 更多信息 + +- [TabooLib 官方文档](https://docs.tabooproject.org/) +- [Gradle 仓库配置文档](https://docs.gradle.org/current/userguide/declaring_repositories.html) \ No newline at end of file diff --git a/src/main/groovy/io/izzel/taboolib/gradle/TabooLibExtension.groovy b/src/main/groovy/io/izzel/taboolib/gradle/TabooLibExtension.groovy index ee2b5fb..88723ff 100644 --- a/src/main/groovy/io/izzel/taboolib/gradle/TabooLibExtension.groovy +++ b/src/main/groovy/io/izzel/taboolib/gradle/TabooLibExtension.groovy @@ -1,61 +1,69 @@ -//file:noinspection unused -package io.izzel.taboolib.gradle - -import groovy.transform.Canonical -import io.izzel.taboolib.gradle.description.Description -import org.gradle.api.Action - -@Canonical -class TabooLibExtension { - - /** - * 是否为子模块(不进行重定向,不产生描述文件) - */ - boolean subproject = false - - /** 描述文件 */ - Description des = new Description() - - /** 环境文件 */ - Env env = new Env() - - /** 版本文件 */ - Version version = new Version() - - /** 排除文件 */ - List exclude = [] - - /** 重定向 */ - Map relocation = new LinkedHashMap<>() - - /** 根包名 */ - String rootPackage = null - - /** 分类 */ - String classifier = null - - /** 排除文件 */ - def exclude(String match) { - exclude += match - } - - /** 重定向 */ - def relocate(String pre, String post) { - relocation[pre] = post - } - - /** 描述文件构造器 */ - def description(Action action) { - action.execute(des) - } - - /** 环境文件构造器 */ - def env(Action action) { - action.execute(env) - } - - /** 版本文件构造器 */ - def version(Action action) { - action.execute(version) - } +//file:noinspection unused +package io.izzel.taboolib.gradle + +import groovy.transform.Canonical +import io.izzel.taboolib.gradle.description.Description +import org.gradle.api.Action + +@Canonical +class TabooLibExtension { + + /** + * 是否为子模块(不进行重定向,不产生描述文件) + */ + boolean subproject = false + + /** 描述文件 */ + Description des = new Description() + + /** 环境文件 */ + Env env = new Env() + + /** 版本文件 */ + Version version = new Version() + + /** 排除文件 */ + List exclude = [] + + /** 重定向 */ + Map relocation = new LinkedHashMap<>() + + /** 根包名 */ + String rootPackage = null + + /** 分类 */ + String classifier = null + + /** 自定义仓库配置 */ + Map repositories = [:] + + /** 排除文件 */ + def exclude(String match) { + exclude += match + } + + /** 重定向 */ + def relocate(String pre, String post) { + relocation[pre] = post + } + + /** 添加自定义仓库 */ + def repository(String name, String url) { + repositories[name] = url + } + + /** 描述文件构造器 */ + def description(Action action) { + action.execute(des) + } + + /** 环境文件构造器 */ + def env(Action action) { + action.execute(env) + } + + /** 版本文件构造器 */ + def version(Action action) { + action.execute(version) + } } \ No newline at end of file diff --git a/src/main/groovy/io/izzel/taboolib/gradle/TabooLibPlugin.groovy b/src/main/groovy/io/izzel/taboolib/gradle/TabooLibPlugin.groovy index 94b87bf..48008d3 100644 --- a/src/main/groovy/io/izzel/taboolib/gradle/TabooLibPlugin.groovy +++ b/src/main/groovy/io/izzel/taboolib/gradle/TabooLibPlugin.groovy @@ -1,130 +1,138 @@ -package io.izzel.taboolib.gradle - -import io.izzel.taboolib.gradle.description.Platforms -import org.gradle.api.Plugin -import org.gradle.api.Project -import org.gradle.api.file.DuplicatesStrategy -import org.gradle.jvm.tasks.Jar -import org.jetbrains.kotlin.gradle.plugin.KotlinPluginWrapperKt - -class TabooLibPlugin implements Plugin { - - @Override - void apply(Project project) { - // 添加仓库 - project.repositories.maven { - url project.uri("https://repo.tabooproject.org/repository/releases/") - } - project.repositories.maven { - url project.uri("https://repo.spongepowered.org/maven") - } - - // 注册扩展 - def tabooExt = project.extensions.create('taboolib', TabooLibExtension) - // 注册任务 - def tabooTask = project.tasks.maybeCreate('taboolibMainTask', TabooLibMainTask) - tabooTask.group = "taboolib" - // 注册任务 - 刷新依赖 - project.tasks.maybeCreate('taboolibRefreshDependencies') - project.tasks.taboolibRefreshDependencies.group = "taboolib" - project.tasks.taboolibRefreshDependencies.doLast { - def taboolibFile = new File("../../caches/modules-2/files-2.1/io.izzel.taboolib").canonicalFile - taboolibFile.listFiles()?.each { module -> - def file = new File(taboolibFile, "${module.name}/${tabooExt.version.taboolib}") - if (file.exists()) { - file.deleteDir() - System.out.println("Delete $file") - } - } - } - // 注册任务 - 构建 API 版本 - project.tasks.maybeCreate('taboolibBuildApi') - project.tasks.taboolibBuildApi.group = "taboolib" - - // 注册配置 - def taboo = project.configurations.maybeCreate('taboo') // 这个名字起的着实二逼 - def include = project.configurations.maybeCreate('include') // 这个代替 "taboo" - - // 添加依赖以及重定向配置 - project.afterEvaluate { - def api = false - try { - project.tasks.taboolibBuildApi.dependsOn(project.tasks.build) - api = project.gradle.startParameter.taskRequests.args[0][0].toString() == "taboolibBuildApi" - } catch (Throwable ignored) { - } - - // 继承 "taboo", "include" 配置 - project.configurations.implementation.extendsFrom(taboo) - project.configurations.implementation.extendsFrom(include) - - // 自动引入 com.mojang:datafixerupper:4.0.26 - project.dependencies.add('compileOnly', 'com.mojang:datafixerupper:4.0.26') - // 自动引入 org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3 - if (tabooExt.version.coroutines != null) { - project.dependencies.add('compileOnly', 'org.jetbrains.kotlinx:kotlinx-coroutines-core:' + tabooExt.version.coroutines) - project.dependencies.add('testImplementation', 'org.jetbrains.kotlinx:kotlinx-coroutines-core:' + tabooExt.version.coroutines) - } - // 自动引入 TabooLib 模块 - if (tabooExt.version.taboolib != null) { - tabooExt.env.modules.each { - def dependency = project.dependencies.create("io.izzel.taboolib:${it}:${tabooExt.version.taboolib}") - if (api || isCoreModule(it) && !tabooExt.subproject) { - project.configurations.taboo.dependencies.add(dependency) - } else { - project.configurations.compileOnly.dependencies.add(dependency) - project.configurations.testImplementation.dependencies.add(dependency) - } - } - } - - project.tasks.jar.finalizedBy(tabooTask) - project.tasks.jar.configure { Jar task -> - task.from(taboo.collect { // 在这里打包 "taboo" 依赖 - if (it.isDirectory()) { - it - } else if (it.name.endsWith(".jar")) { - project.zipTree(it) - } else { - project.files(it) - } - }) - task.duplicatesStrategy = DuplicatesStrategy.EXCLUDE - if (api) { - task.getArchiveClassifier().set("api") - } - } - - def kotlinVersion = KotlinPluginWrapperKt.getKotlinPluginVersion(project).replaceAll("[._-]", "") - def jarTask = project.tasks.jar as Jar - tabooTask.configure { TabooLibMainTask task -> - task.tabooExt = tabooExt - task.project = project - task.inJar = task.inJar ?: jarTask.archivePath - task.relocations = tabooExt.relocation - task.classifier = tabooExt.classifier - task.api = api - - // 重定向 - if (!tabooExt.version.isSkipTabooLibRelocate()) { - def root = tabooExt.rootPackage ?: project.group.toString() - task.relocations['taboolib'] = root + '.taboolib' - } - if (!tabooExt.version.isSkipKotlinRelocate()) { - task.relocations['kotlin.'] = 'kotlin' + kotlinVersion + '.' - if (tabooExt.version.coroutines != null) { - def coroutinesVersion = tabooExt.version.coroutines.replaceAll("[._-]", "") - task.relocations['kotlinx.coroutines.'] = 'kotlin' + kotlinVersion + 'x.coroutines' + coroutinesVersion + '.' - } - } - } - } - } - - /** - * 是否为必要模块 - */ - static def isCoreModule(String module) { - return module == "common" || module == "platform-application" || Platforms.values().any { p -> p.module == module } - } -} +package io.izzel.taboolib.gradle + +import io.izzel.taboolib.gradle.description.Platforms +import org.gradle.api.Plugin +import org.gradle.api.Project +import org.gradle.api.file.DuplicatesStrategy +import org.gradle.jvm.tasks.Jar +import org.jetbrains.kotlin.gradle.plugin.KotlinPluginWrapperKt + +class TabooLibPlugin implements Plugin { + + @Override + void apply(Project project) { + // 注册扩展 + def tabooExt = project.extensions.create('taboolib', TabooLibExtension) + + // 添加默认仓库 + project.repositories.maven { + url project.uri("https://repo.tabooproject.org/repository/releases/") + } + project.repositories.maven { + url project.uri("https://repo.spongepowered.org/maven") + } + // 注册任务 + def tabooTask = project.tasks.maybeCreate('taboolibMainTask', TabooLibMainTask) + tabooTask.group = "taboolib" + // 注册任务 - 刷新依赖 + project.tasks.maybeCreate('taboolibRefreshDependencies') + project.tasks.taboolibRefreshDependencies.group = "taboolib" + project.tasks.taboolibRefreshDependencies.doLast { + def taboolibFile = new File("../../caches/modules-2/files-2.1/io.izzel.taboolib").canonicalFile + taboolibFile.listFiles()?.each { module -> + def file = new File(taboolibFile, "${module.name}/${tabooExt.version.taboolib}") + if (file.exists()) { + file.deleteDir() + System.out.println("Delete $file") + } + } + } + // 注册任务 - 构建 API 版本 + project.tasks.maybeCreate('taboolibBuildApi') + project.tasks.taboolibBuildApi.group = "taboolib" + + // 注册配置 + def taboo = project.configurations.maybeCreate('taboo') // 这个名字起的着实二逼 + def include = project.configurations.maybeCreate('include') // 这个代替 "taboo" + + // 添加依赖以及重定向配置 + project.afterEvaluate { + // 添加自定义仓库 + tabooExt.repositories.each { name, url -> + project.repositories.maven { + it.name = name + it.url = project.uri(url) + } + } + + def api = false + try { + project.tasks.taboolibBuildApi.dependsOn(project.tasks.build) + api = project.gradle.startParameter.taskRequests.args[0][0].toString() == "taboolibBuildApi" + } catch (Throwable ignored) { + } + + // 继承 "taboo", "include" 配置 + project.configurations.implementation.extendsFrom(taboo) + project.configurations.implementation.extendsFrom(include) + + // 自动引入 com.mojang:datafixerupper:4.0.26 + project.dependencies.add('compileOnly', 'com.mojang:datafixerupper:4.0.26') + // 自动引入 org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3 + if (tabooExt.version.coroutines != null) { + project.dependencies.add('compileOnly', 'org.jetbrains.kotlinx:kotlinx-coroutines-core:' + tabooExt.version.coroutines) + project.dependencies.add('testImplementation', 'org.jetbrains.kotlinx:kotlinx-coroutines-core:' + tabooExt.version.coroutines) + } + // 自动引入 TabooLib 模块 + if (tabooExt.version.taboolib != null) { + tabooExt.env.modules.each { + def dependency = project.dependencies.create("io.izzel.taboolib:${it}:${tabooExt.version.taboolib}") + if (api || isCoreModule(it) && !tabooExt.subproject) { + project.configurations.taboo.dependencies.add(dependency) + } else { + project.configurations.compileOnly.dependencies.add(dependency) + project.configurations.testImplementation.dependencies.add(dependency) + } + } + } + + project.tasks.jar.finalizedBy(tabooTask) + project.tasks.jar.configure { Jar task -> + task.from(taboo.collect { // 在这里打包 "taboo" 依赖 + if (it.isDirectory()) { + it + } else if (it.name.endsWith(".jar")) { + project.zipTree(it) + } else { + project.files(it) + } + }) + task.duplicatesStrategy = DuplicatesStrategy.EXCLUDE + if (api) { + task.getArchiveClassifier().set("api") + } + } + + def kotlinVersion = KotlinPluginWrapperKt.getKotlinPluginVersion(project).replaceAll("[._-]", "") + def jarTask = project.tasks.jar as Jar + tabooTask.configure { TabooLibMainTask task -> + task.tabooExt = tabooExt + task.project = project + task.inJar = task.inJar ?: jarTask.archivePath + task.relocations = tabooExt.relocation + task.classifier = tabooExt.classifier + task.api = api + + // 重定向 + if (!tabooExt.version.isSkipTabooLibRelocate()) { + def root = tabooExt.rootPackage ?: project.group.toString() + task.relocations['taboolib'] = root + '.taboolib' + } + if (!tabooExt.version.isSkipKotlinRelocate()) { + task.relocations['kotlin.'] = 'kotlin' + kotlinVersion + '.' + if (tabooExt.version.coroutines != null) { + def coroutinesVersion = tabooExt.version.coroutines.replaceAll("[._-]", "") + task.relocations['kotlinx.coroutines.'] = 'kotlin' + kotlinVersion + 'x.coroutines' + coroutinesVersion + '.' + } + } + } + } + } + + /** + * 是否为必要模块 + */ + static def isCoreModule(String module) { + return module == "common" || module == "platform-application" || Platforms.values().any { p -> p.module == module } + } +}