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?