Skip to content

Document a warning against static state in Gradle plugins #6818

@big-guy

Description

@big-guy

Gradle attempts to reuse classloaders between build invocations to keep classes "hot" (since Gradle 2.4). This has implications for how Gradle plugins (and their dependencies) are written. Specifically, Gradle plugins, tasks, extensions, etc should in general not keep static state because it can cause problems like resource leaks and make it seem like configuration changes are being ignored.

This leads to other bad advice like:

  • Don't use the daemon at all
  • Delete your GRADLE_USER_HOME
  • Stop/restart the daemon when changing configuration

This would be appropriate in our Gradle guides on plugin development and/or the user manual.

Context

Given a plugin like this:

class MyPlugin implements Plugin<Project> {
    static String prop = System.getProperty("myproperty", "default")
    void apply(Project project) {
       project.logger.lifecycle("myproperty is " + prop)
    }
}

apply plugin: MyPlugin

When you run gradle help, this produces the message "myproperty is default".
When you run gradle help -Dmyproperty=somethingelse, this produces the message "myproperty is default". (surprising!)
When you then stop the daemon and run gradle help -Dmyproperty=somethingelse, this produces the message "myproperty is somethingelse". This is the message you'll get until you stop the daemon/use another daemon.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions