Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I conditionally run code if a Gradle plugin is applied?

I have a script plugin that I would like to:

  • Check if the ivy-publish applied (via apply plugin: ivy-publish):
  • If it is applied, declare publishing { repositories { ivy { } } }
  • If it's not applied, run some other code

However, I'm unsure of how to actually run code if the ivy-publish plugin is applied, and I couldn't find anything about that in the documentation. Is there any way to do this?

like image 984
Thunderforge Avatar asked Sep 13 '17 18:09

Thunderforge


People also ask

What is apply false Gradle?

The default behavior of the plugins {} block is to immediately resolve and apply the plugins. But, you can use the apply false syntax to tell Gradle not to apply the plugin to the current project and then use the plugins {} block without the version in subprojects' build scripts: Example 3.

Which plugin allows to run Gradle builds?

This plugin adds Gradle Support to Jenkins. Gradle is managed as another tool inside Jenkins (the same way as Ant or Maven), including support for automatic installation and a new build step is provided to execute Gradle tasks.

What is the use of apply plugin in Gradle?

This plugin will change the name of the APK, move the APK to your desired location, and create a text file in which it'll add all the dependencies for all the APKs you've created through the plugin. In the process, you'll learn about the following topics: Gradle tasks.

Which are the two types of plugins in Gradle?

There are two types of plugins one is script plugin and second is binary plugin.


1 Answers

You can use the PluginManager.withPlugin(String id, Action<? super AppliedPlugin> action) method. From the Javadoc:

If a plugin with the specified ID has already been applied, the supplied action will be executed immediately. Otherwise, the action will executed immediately after a plugin with the specified ID is applied.

In your build script you could do something like:

pluginManager.withPlugin('ivy-publish') {
  // Do configuration
}
like image 179
mkobit Avatar answered Oct 13 '22 00:10

mkobit