简体   繁体   English

如何将自定义 Gradle 插件放在另一个项目构建类路径上?

[英]How to put custom Gradle plugins on an another projects build classpath?

I have a published gradle plugin which looks something like this:我有一个已发布的 gradle 插件,看起来像这样:

|__root
|     |
|     |
|     |__java
|     |    |__SomeJavaClass.java
|     |    |__SomeJavaClass2.java
|     |
|     |__kotlin
|          |__MyPluginClass.kts
|          |__MyTaskClass.kts
|          |__Utils.kts   

I would like to include this plugin as a project in my multi project build instead of publishing it to a repo for easier developement.我想将此插件作为一个项目包含在我的多项目构建中,而不是将其发布到存储库以便于开发。

This plugin has 3 Kotlin files Project A is using.这个插件有 3 个项目 A 正在使用的 Kotlin 文件。 MyPluginClass.kts has my own plugin class, MyTaskClass.kts has my own task class and Utils.kts contains only kotlin functions. MyPluginClass.kts 有我自己的插件类,MyTaskClass.kts 有我自己的任务类,Utils.kts 只包含 kotlin 函数。 The java classes are used in MyPlugin and MyTask. java 类用于 MyPlugin 和 MyTask。

It is being put on Project A's build.gradle.kts classpath as它被放在项目 A 的 build.gradle.kts 类路径中

classpath("com.my:custom.plugin:version")

A very simplified project structure I would like to achieve:我想实现的一个非常简化的项目结构:

root
| 
|__Project A
|     |
|     |__build.gradle.kts
|     |__x.gradle.kts
|     |__y.gradle.kts
|     |__settings.gradle
|
|__Project build-logic
|     |
|     |__build.gradle.kts
|     |
|     |__java
|     |    |__SomeJavaClass.java
|     |    |__SomeJavaClass2.java
|     |
|     |__kotlin
|          |__MyPluginClass.gradle.kts
|          |__MyTaskClass.gradle.kts
|          |__Utils.gradle.kts   

I'm trying to create plugins from those kotlin files and include them in my main build because i need them precompiled, but i cant seem to find a way to put them on Project A's classpath when build.gradle is running there.我正在尝试从这些 kotlin 文件创建插件并将它们包含在我的主构建中,因为我需要对它们进行预编译,但是当 build.gradle 在那里运行时,我似乎无法找到将它们放在项目 A 的类路径中的方法。 Is it even possible?甚至可能吗? what would be the proper solution?什么是正确的解决方案?

I'm using gradle 7.3我正在使用 gradle 7.3

You can combine multiple, independent, Gradle projects using composite builds .您可以使用复合构建组合多个独立的 Gradle 项目。 The same is also true for Gradle plugins. Gradle 插件也是如此。

Including a Gradle plugin from another directory is documented in the Testing Gradle plugins section. 测试 Gradle 插件部分记录了包含来自另一个目录的 Gradle 插件。

So if you have a shared-build-logic project that provides a Gradle plugin with an ID my.shared-build-logic , and you want to use that in project-a .因此,如果您有一个shared-build-logic项目,该项目提供了一个 ID my.shared-build-logic的 Gradle 插件,并且您想在project-a中使用它。

my-projects/
├── shared-build-logic/
│   ├── src/
│   │   └── ...
│   ├── build.gradle.kts
│   └── settings.gradle.kts
└── project-a/
    ├── src/
    │   └── ...
    ├── build.gradle.kts
    └── settings.gradle.kts

Then you need to include shared-build-logic in your project-a Gradle settings file.然后你需要在你的project-a中包含shared-build-logic ——一个 Gradle 设置文件。

// my-projects/project-a/settings.gradle.kts

rootProject.name = "project-a"

pluginManagement {
  includeBuild("../shared-build-logic")
}

Now, so long as shared-build-logic correctly provides a Gradle plugin, in project-a you can reference that plugin.现在,只要shared-build-logic正确地提供了 Gradle 插件,您就可以在project-a中引用该插件。

// my-projects/project-a/settings.gradle.kts

plugins {
  id("my.shared-build-logic")
}

Finnaly figured out.芬纳利想通了。 I didn't create plugins from build-logic, left them as kotlin files.我没有从构建逻辑创建插件,而是将它们保留为 kotlin 文件。 Since it is a separate gradle project, i can just build the project and point project A's classpath to the produced jar file like由于它是一个单独的 gradle 项目,我可以构建项目并将项目 A 的类路径指向生成的 jar 文件,例如

buildscript {
    dependencies {
        classpath(files("path.to.the.jar"))
    }
}

this way i can access methods and the MyTask from Project A.这样我可以从项目 A 访问方法和 MyTask。

The other issue was that i wanted to apply MyPlugin like另一个问题是我想像应用 MyPlugin

plugins{
    id("my-plugin-name")
}

which wasnt working, i guess because it is a class.这不起作用,我猜是因为它是一门课。 But now since i had the whole project jar on my build classpath, i can do this:但是现在由于我的构建类路径中有整个项目 jar,我可以这样做:

apply<my-plugin-name>()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 gradle 项目是否有等效的“mvn 依赖项:build-classpath”? - Is there an equivalent `mvn dependency:build-classpath` for gradle projects? 自定义基于Java的Gradle插件和消费项目的类路径 - Custom Java Based Gradle Plugin and Classpath of Consuming Projects 如何设置Gradle自定义ant任务类路径? - How to set Gradle custom ant task classpath? 在 gradle 中如何禁用单个项目的 findbugs 或 checkstyle 插件 - In gradle how do I disable the findbugs or checkstyle plugins for individual projects 如何为自定义插件修改Gradle插件{}管理存储库? - How to amend Gradle plugins {} management repository for custom plugins? 避免Gradle插件之间的依赖关系/类路径冲突 - Avoid dependency/classpath conflict between Gradle plugins Gradle Multiproject Build:如何在不创建伪项目的情况下将WAR项目的JAR包含到另一个WAR中 - Gradle Multiproject Build: How to include JAR of a WAR project into another WAR without creating pseudo projects 如何修复 android 工作室构建问题中显示类路径异常的 Gradle - How to fix Gradle in android studio build issue showing exceptions of classpath 用gradle构建jar并放入构建到另一个jar中的资源 - Build jar and put in resources that are built into another jar with gradle 将自定义 *.jar 文件添加到 build.gradle 项目依赖项中并设置类路径 - Add custom *.jar file into build.gradle project dependencies and set up the classpath
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM