Java 将本地插件添加到 Gradle 项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35302414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 16:31:45  来源:igfitidea点击:

Adding local plugin to a Gradle project

javagradle

提问by Bobbake4

I have a Gradle plugin that compiles and works as expected. I would like to distribute with the source code an example application using the Gradle plugin which would also allow me to test changes to the plugin easily. Now to do this I must add a classpath dependency to the buildScriptblock. Is it possible to add a dependent local plugin that will be compiled with the example project? The main issue I'm having now is that the plugin does not exist when trying to sync the example project resulting in a failure.

我有一个 Gradle 插件,它可以按预期编译和工作。我想用源代码分发一个使用 Gradle 插件的示例应用程序,这也可以让我轻松地测试对插件的更改。现在要做到这一点,我必须向buildScript块添加类路径依赖项。是否可以添加将与示例项目一起编译的依赖本地插件?我现在遇到的主要问题是,在尝试同步导致失败的示例项目时,该插件不存在。

回答by RaGe

If writing integration tests is your eventual goal, I still recommend using ProjectBuilder. You can see an example of this in Section 39.5.4 here. There are a lot more real-world test implementation examples in gradle source. See here for instance.

如果编写集成测试是您的最终目标,我仍然建议使用 ProjectBuilder。您可以在此处查看第 39.5.4 节中的示例。gradle 源中有更多真实世界的测试实现示例。例如,请参见此处



That being said, I got curious about the literal question you posted and tried a few things. What did work for me is this:

话虽如此,我对您发布的字面问题很好奇并尝试了一些方法。对我有用的是:

buildscript{
    repositories{
        ...
    }

    dependencies{
        classpath files('relative/path/to/plugin.jar')
    }
}

apply plugin: fully.qualified.package.PluginClassName

Note that the class name is not enclosed in 'quotes'to make it a string.

请注意,类名没有用'引号括起来'以使其成为字符串。

This is not very useful since this declaration requires the plugin.jarto be built and available before the project consuming the plugin is built - but at this point, you might as well be depending on an external repo dependency.

这不是很有用,因为此声明要求在plugin.jar构建使用插件的项目之前构建并可用 - 但此时,您可能还依赖于外部 repo 依赖项。

A second problem with this is that the transitive dependencies of the plugin project are not added to your plugin-consumer project automatically.

与此有关的第二个问题是插件项目的传递依赖项不会自动添加到您的插件消费者项目中。

I couldn't get a project dependency like classpath project(':plugin-project')to work inside buildscript.

我无法获得像classpath project(':plugin-project')在 buildscript 中工作的项目依赖项。



One (extremely hacky) way to ensure that the plugin project is always build and available while building the plugin-consumer project is to have a "parent" build.gradle that always builds the plugin first before building the consumer project. I uploaded an example here.

在构建插件消费者项目时,确保插件项目始终构建和可用的一种(非常hacky)方法是拥有一个“父” build.gradle ,它总是在构建消费者项目之前首先构建插件。我在这里上传了一个例子

回答by Doron Gold

With the current Gradle version (3.3 as of writing) you can use the Composite Build feature to substitute plugin dependencies.

使用当前的 Gradle 版本(撰写本文时为 3.3),您可以使用复合构建功能来替换插件依赖项。

For example, you can build your example project with the following command (run in the root of your example project): gradle build --include-build /path/to/local-plugin

例如,您可以使用以下命令构建示例项目(在示例项目的根目录中运行): gradle build --include-build /path/to/local-plugin

This would pick up your plugin from your local directory. It would even build your plugin first, then build your example project with the just built plugin as dependency.

这将从您的本地目录中获取您的插件。它甚至会先构建您的插件,然后使用刚刚构建的插件作为依赖项构建您的示例项目。

This feature is also supported by IDEs: Simple instructions for configuring Composite Build in IntelliJ

IDE 也支持此功能: 在 IntelliJ 中配置复合构建的简单说明

See docs here: https://docs.gradle.org/current/userguide/composite_builds.html

请参阅此处的文档:https: //docs.gradle.org/current/userguide/composite_builds.html

Notice that substituting plugins only works with the buildscript block but not with the plugins block.

请注意,替换插件仅适用于 buildscript 块,而不适用于 plugins 块。

回答by Egos Zhang

I encounter when i want to debug my gradle pluginin Android Studio, I solve it finally.

当我想gradle plugin在Android Studio中调试我的时候遇到了,我终于解决了。

First, upload your gradle pluginto local maven.

首先,将您的文件上传gradle plugin到本地 Maven。

apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'maven'

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile gradleApi()
}

sourceCompatibility = "1.7"
targetCompatibility = "1.7"


group='com.egos.gradle.plugins'
name='plugintest'
version='0.0.1'

uploadArchives {
    repositories {
        mavenDeployer {
            repository(url: uri('../repo'))
        }
    }
}

execute gradle uploadArchives. Then use local plugin.

执行gradle uploadArchives。然后使用本地插件。

buildscript {
    repositories {
        maven {
            url uri('../repo')
        }
    }
    dependencies {
        classpath group: 'com.egos.gradle.plugins',
                  name: 'plugintest',
                  version: '0.0.1'
    }
}

apply plugin: 'com.egos'

回答by Siddharth Yadav

Addition to above answer. You need to also add "gradlePlugin" along with "uploadArchives" something like below:

除了上面的答案。您还需要添加“gradlePlugin”和“uploadArchives”,如下所示:

gradlePlugin {
    plugins {
        testgen {
            id = "com.egos.gradle.plugins"
            implementationClass = "com.egos.gradle.plugins.<PluginClassName>"
        }
    }
}

instead of this set your plugin classname. Rest remains same as above answer. Thanks!

而不是设置你的插件类名。其余与上述答案相同。谢谢!

回答by Cristian

Found a clean way to do it with composite builds, as Doron noticed. These are the steps:

正如 Doron 所注意到的,找到了一种使用复合构建的简洁方法。这些是步骤:

  • Define your plugin with a build.gradlethat has group:
  • 使用build.gradle具有group以下内容的插件定义您的插件:
// some-plugin/build.gradle
...
group = "some.group.id"
...
  • Reference your project in the settings.gradlelike this:
  • settings.gradle像这样引用您的项目:
// settings.gradle
...
includeBuild 'some-plugin'
  • In your projects you can make use of the plugin by:
  • 在您的项目中,您可以通过以下方式使用该插件:
// some-project/build.gradle
buildscript {
  dependencies {
    classpath "some.group.id:some-plugin"
  }
}

apply(plugin: "your.plugin.id")

Notice how the classpath is made out of the group defined in the plugin's gradle file and the name of its directory.

请注意类路径是如何由插件的 gradle 文件中定义的组及其目录名称组成的。