Gradle java-library plugin compared to java plugin

According to the docs

[...] the Java Library plugin is only wired to behave correctly with the java plugin.

So you should not run into any problems if you apply both plugins to a project. E.g. you could apply the java plugin to each project via the subprojects closure and later apply the java-library plugin to those subprojects that require the additional functionality of the plugin (via their build.gradle file).

Please note, that you can specify plugin-dependent configuration via the withPlugin method of the PluginManager or the withId method of the PluginContainer. For both methods applies:

If the plugin was already applied, the action is executed. If the plugin is applied sometime later the action will be executed after the plugin is applied. If the plugin is never applied, the action is never executed.

subprojects { sub ->
    sub.plugins.withId('java-library') {
        // configure sub
    }
}

The plugin exposes two configurations that can be used to declare dependencies: api and implementation.

And also:

The api configuration should be used to declare dependencies which are exported by the library API, whereas the implementation configuration should be used to declare dependencies which are internal to the component.

Gradle 3.4 introduced new Java Library plugin configurations that allow you to control whether a dependency is published to the compile and runtime classpaths of projects that consume that library.
If you want to use the new api configuration instead of compile you have to use the new plugin.


I have had the same question(s) and found answers at the end of the official plugin documentation: Known issues.

Your question

Is there any benefit to using the java plugin for some sub projects and the java-library plugin for others?

is answered by

Increased memory usage for consumers

When a project uses the Java Library plugin, consumers will use the output classes directory of this project directly on their compile classpath, instead of the jar file if the project uses the Java plugin. An indirect consequence is that up-to-date checking will require more memory, because Gradle will snapshot individual class files instead of a single jar. This may lead to increased memory consumption for large projects.

There is also a paragraph about problems with other plugins and how you can workaround these problems.

In Gradle 5.3 or earlier, some plugins, such as the Groovy plugin, may not behave correctly.

Tags:

Gradle