22

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?

3 Answers 3

14

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
    }
}
2
  • 1
    That is interesting I have not seen the withPlugin or withId method before. This definitely answers how I can configure the plugins before they are applied. However is this any better than using the java-library plugin for both applications and libraries? Commented Oct 7, 2017 at 14:52
  • Sorry, I don't quite understand it. My project is just general java or java web project, NOT android project, so in this case, what is difference between java plugin and java-library plugin?
    – frank
    Commented Feb 18, 2019 at 6:30
3

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.

3
  • I understand the new features of the java library plugin and want to use them for my libraries, but is there any reason to use java plugin for applications? Applications have no reason to use the api configuration and the java plugin supports the implementation configuration. But I want to know if I should still use the java plugin for applications? Commented Oct 7, 2017 at 6:35
  • @DragonAssassin Currently no reason to use it in applications but it is just an opinion. Commented Oct 7, 2017 at 6:46
  • I have edited my question. Hopefully this makes it more clear why I believe using the both plugins is problematic. It should also explain why I would like to know if there are any drawbacks to using the java library plugin for everything. Commented Oct 7, 2017 at 7:18
3

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.