Gradle java-library plugin compared to java plugin
NickName:DragonAssassin Ask DateTime:2017-10-07T13:34:39

Gradle java-library plugin compared to java plugin

In the Gradle documentation for the java-library plugin it states:

The Java Library plugin expands the capabilities of the Java plugin by providing specific knowledge about Java libraries. In particular, a Java library exposes an API to consumers (i.e., other projects using the Java or the Java Library plugin)

This statement implies that only java programs that are meant to be consumed (libraries) should use the java-library plugin; while, java programs that are not meant to be consumed (applications) should use the java plugin.

Prior to using the java-library plugin a root build.gradle file could contain the following:

subprojects {
    apply plugin: 'java'
    sourceCompatibility '1.8'
    targetCompatibility '1.8'

    // other common java stuff
}

However now in multi module projects that has both applications and libraries you cannot delegate the plugin selection to the sub projects and have the following root build.gradle:

subprojects {
    sourceCompatibility '1.8'
    targetCompatibility '1.8'

    // other common java stuff
}

This will fail because sourceCompatibility and targetCompatibility are defined by the java and java-library plugins. Ideally I would like to do the following:

subprojects {
    apply plugin: 'java-library'
    sourceCompatibility '1.8'
    targetCompatibility '1.8'

    // other common java stuff
}

Is there any reason to enforce that java applications use the java plugin and that java libraries use the java-library plugin? Is there any reason that the java plugin should be used instead of the java-library plugin?

Edit

To further clarify my question, in the Gradle samples there is a multi module project for with the java plugin here and for the java-library plugin here. In the sample for the java plugin, the root build.grade uses apply plugin: 'java'. The root build.gradle for the java-library plugin does not use any plugins. The app project uses apply plugin: 'java'; while, the core and utils projects use apply plugin: 'java-library'.

My question is why should some projects use the java plugin and others use the java-library plugin? Its seems to make it more difficult to not violate the DRY principle. I makes it difficult to specify the sourceCompatibility and targetCompatibility only once. I can think of a few ways specify these properties once, but the simplest solution seems to be using the java-library for all projects.

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

Copyright Notice:Content Author:「DragonAssassin」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/46617018/gradle-java-library-plugin-compared-to-java-plugin

Answers
Lukas Körfer 2017-10-07T09:28:07

According to the docs\n\n\n [...] the Java Library plugin is only wired to behave correctly with the java plugin.\n\n\nSo 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).\n\nPlease 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:\n\n\n 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.\n\n\nsubprojects { sub ->\n sub.plugins.withId('java-library') {\n // configure sub\n }\n}\n",


Gabriele Mariotti 2017-10-07T06:26:36

\n The plugin exposes two configurations that can be used to declare dependencies: api and implementation.\n\n\nAnd also:\n\n\n 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.\n\n\nGradle 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.\nIf you want to use the new api configuration instead of compile you have to use the new plugin.",


More about “Gradle java-library plugin compared to java plugin” related questions

Gradle java-library plugin compared to java plugin

In the Gradle documentation for the java-library plugin it states: The Java Library plugin expands the capabilities of the Java plugin by providing specific knowledge about Java libraries. In

Show Detail

android gradle: plugin id java-library not found

The following works fine for a war project: configure([project(':foo'), project(':bar')]) { apply plugin: 'java-library' .... } But for android, plugin id 'java-library&

Show Detail

Use Gradle Witness plugin with java-library

For a long time I was using Gradle with the java plugin, and Gradle Witness to verify Maven dependencies. e.g.: apply plugin: 'java' apply plugin: 'witness' dependencies { compile "io.netty:n...

Show Detail

Does java-library plugin fully cover java plugin functionality?

According to API and implementation separation doc the java-library plugin should be used to declare dependencies as api: apply plugin: 'java-library' Is java-library full replacement for java pl...

Show Detail

java-library plugin leaking dependencies

My project has two modules data(com.android.library) and domain(java-library), data module depends on domain. build.gradle(data) implementation(project(":domain")) In build.gradle of domain ...

Show Detail

Copy all dependencies in a gradle project using java-library plugin

Is there a way to copy all (including transitive) dependencies (jars) of a gradle project that uses java-library plugin to some folder, using a gradle task? I can list all jars from the

Show Detail

spring-boot with the gradle java java-plugin

I use gradle with the new java-library plugin. With this plugin we can use new configurations for dependencies instead of 'compile', 'runtime', 'test' etc. see java-library plugin documentation Bu...

Show Detail

Gradle >=4.6: using java-library & java plugins simultaneously in multi-module build

Since Gradle 4.6: https://docs.gradle.org/4.6/userguide/java_library_plugin.html#sec:java_library_separation https://docs.gradle.org/4.6/release-notes.html#compile/runtime-scope-separation-in-pom-

Show Detail

Gradle Plugin ‘java-library’ not found

I'm having an issue as of late when compiling a java library with Gradle 4.0.1 (this issue was happening with 3.5 as well) on Windows with IntelliJ using the 'java-library' plugin. Here is a snippe...

Show Detail

Junit 5 gradle plugin not found

Try to use junit 5 with gradle: buildscript { repositories { mavenCentral() } dependencies { classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0' } } ...

Show Detail