将本地插件添加到Gradle项目 [英] Adding local plugin to a Gradle project

查看:621
本文介绍了将本地插件添加到Gradle项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 buildScript block. 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.

推荐答案

如果编写集成测试是您最终的目标,我仍然建议您使用ProjectBuilder.您可以在第39.5.4节中看到一个示例. gradle源代码中有许多现实世界中的测试实现示例. 例如在这里.

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.

话虽如此,我对您发布的字面问题感到好奇,并尝试了一些尝试.对我有用的是:

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

请注意,类名未包含在' quotes '中,以使其成为字符串.

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

这不是很有用,因为此声明要求在构建使用该插件的项目之前先构建plugin.jar并使其可用-但在这一点上,您最好还是依赖于外部回购依赖项.

This is not very useful since this declaration requires the plugin.jar to 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.

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

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

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

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

一种确保插件项目在构建插件-消费者项目时始终可以构建且可用的(极端hacky)方式是拥有一个父" build.gradle,该父项始终在构建使用者项目之前首先构建插件.我在此处上传了一个示例.

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.

这篇关于将本地插件添加到Gradle项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆