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?
There are two general types of plugins in Gradle, binary plugins and script plugins.
Definition. A plugin is a software component that adds a specific feature to an existing computer program. A library is a collection of nonvolatile resources used by computer programs in a software development process.
It is popular for its ability to build automation in languages like Java, Scala, Android, C/C++, and Groovy. The tool supports groovy based Domain Specific Language over XML. Gradle provides building, testing, and deploying software on several platforms. The tool is popular for building any software and large projects.
Android Gradle plugin and Android Studio compatibilityThe Android Studio build system is based on Gradle, and the Android Gradle plugin adds several features that are specific to building Android apps. The following table lists which version of the Android Gradle plugin is required for each version of Android Studio.
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
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With