Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Custom gradle plugin module in another module

I am developing a custom plugin that I want to be able to deploy to a repository at a later stage so I've created a stand alone module for it.

Before I do any formal TDD on it, I wanna do certain exploratory tests manually so, I've created a demo module that uses the given plugin in.

The only way I've found so far to do this was to deploy the plugin into a local repository and then import it on the other module. But this is very tedious and it's quite easy to forget to deploy it.

I was wondering if there is a way to do this more straight forward.

like image 794
pablisco Avatar asked Feb 06 '23 02:02

pablisco


2 Answers

Update(2020-09-07):

It's possible to use includeBuild("plugin/module") to add a module into the classpath of the project so the plugin can be applied. With one caveat, it doesn't work yet if you want to apply it on settings.gradle[.kts] as I found out on this thread.

Old answer:

The best solution I've found was to include the folders of the plugin module as part of the buildSrc module (used for local scripting) within it's source sets like:

sourceSets {
    main {
        groovy.srcDirs += "../custom-plugin/src/main/groovy"
        resources.srcDirs += "../custom-plugin/src/main/resources"
    }
}

This will let you use the plugin as if it was in the remote repository but without having to deploy it every time. Next unit tests :)

like image 151
pablisco Avatar answered Feb 07 '23 17:02

pablisco


Option 1:

If you are using gradle 3.1 or later, you can use the composite build feature. Build your standalone module like this: gradle build --include-build /path/to/plugin-root

Assuming you run this command from the root of a standalone gradle project that applies the plugin, this would first build the plugin, then build your standalone project and apply the just built plugin to it.

Option 2:

If you use an older gradle version (which does not support composite builds), you can apply the maven plugin to your plugin project. This defines the install task, which you can now call by running gradle install in the root of your plugin project. This task builds your plugin and puts the resulting jar in a local cache directory (on Linux the default path is ~/.m2). Notice that after every change you make in the plugin code, you have to run gradle install again.

Now, for your standalone project to use the local plugin jar, you need to edit the build script and put mavenLocal() under buildscript.repositories. Make sure that mavenLocal() is above anything else in the repositories block.

Example of the buildscript block of your standalone project:

buildscript {
    repositories {
        mavenLocal()
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }
    dependencies {
        classpath "com.mycompany.plugins:my-awsome-plugin:1.0.0"
    }
}
like image 37
Doron Gold Avatar answered Feb 07 '23 17:02

Doron Gold