Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
122 views
in Technique[技术] by (71.8m points)

java - Adding local plugin to a Gradle project

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.

question from:https://stackoverflow.com/questions/35302414/adding-local-plugin-to-a-gradle-project

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

I couldn't get a project dependency like classpath project(':plugin-project') to work inside 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...