Home > Enterprise >  How to properly add gradle plugin to multiproject?
How to properly add gradle plugin to multiproject?

Time:09-27

I have a multi-project build.gradle: https://gist.github.com/iva-nova-e-katerina/7c72399ede83b5f78dbae19582974f35

and this script fails with error:

> Plugin with id 'com.github.blindpirate.osgi' not found.

Why? Maybe buildscript {} should be outside allprojects ? How to properly add gradle plugin to multiproject?

CodePudding user response:

As you suggested, you can fix the issue by moving buildscript outside of allprojects. e.g.

buildscript {
    repositories {
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }
    dependencies {
        classpath "gradle.plugin.com.github.blindpirate:gradle-legacy-osgi-plugin:0.0.4"
    }
}

subprojects {
    apply plugin: "com.github.blindpirate.osgi"
}

The reason this works is explained by the Gradle docs:

For multi-project builds, the dependencies declared with a project’s buildscript() method are available to the build scripts of all its sub-projects.

This approach was also recommended by Gradle when someone asked a similar question on the official forum.

Now to answer your question How to properly add gradle plugin to multiproject?. There is a better way to apply the gradle-legacy-osgi-plugin plugin, which is to use the plugins DSL e.g.

plugins {
  id "com.github.blindpirate.osgi" version "0.0.6"
}

Using the plugins DSL has some significant advantages over the apply syntax you're currently using, so is generally the recommended approach.

Since you're working in a multi-project setup, the plugins DSL won't be compatible with the subprojects syntax you're currently using. I suggest one of the following approaches:

  1. apply the plugin in the root project with a version, then in subprojects without a version. Use apply false to avoid the plugin getting applied to the root project. The docs have a full example of this.
plugins {
    id "com.github.blindpirate.osgi" version "0.0.6" apply false
}
  1. create a convention plugin which you apply to every subproject

Using the allprojects and subprojects syntax is discouraged as it couples projects together, causing a problem for features like parallel build execution. Using the above suggestions help you avoid this legacy syntax.

CodePudding user response:

It seems like plugin should be applied in each subproject's build.gralde but not in the root's "subprojects {" element. As here

git clone github.com/bobbylight/RSyntaxTextArea.git

(plugin: "biz.aQute.bnd.builder")

  • Related