// Copyright (C) 2024 Gradle, Inc.
//
// Licensed under the Creative Commons Attribution-Noncommercial-ShareAlike 4.0 International License.;
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      https://creativecommons.org/licenses/by-nc-sa/4.0/
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

[[java_library_plugin]]
= The Java Library Plugin

The Java Library plugin expands the capabilities of the <<java_plugin.adoc#java_plugin,Java Plugin (`java`)>> 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).
All the source sets, tasks and configurations exposed by the Java plugin are implicitly available when using this plugin.

[[sec:java_library_usage]]
== Usage

To use the Java Library plugin, include the following in your build script:

====
include::sample[dir="snippets/reference/platforms/jvm/quickstart/kotlin",files="build.gradle.kts[tags=use-plugin]"]
include::sample[dir="snippets/reference/platforms/jvm/quickstart/groovy",files="build.gradle[tags=use-plugin]"]
====

[[sec:java_library_separation]]
== API and implementation separation

The key difference between the standard Java plugin and the Java Library plugin is that the latter introduces the concept of an _API_ exposed to consumers. A library is a Java component meant to be consumed by other components. It's a very common use case in multi-project builds, but also as soon as you have external dependencies.

The plugin exposes two <<dependency_configurations.adoc#sub:what-are-dependency-configurations,configurations>> that can be used to declare dependencies: `api` and `implementation`.
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.

====
include::sample[dir="snippets/reference/platforms/jvm/quickstart/kotlin",files="build.gradle.kts[tags=dependencies]"]
include::sample[dir="snippets/reference/platforms/jvm/quickstart/groovy",files="build.gradle[tags=dependencies]"]
====

Dependencies appearing in the `api` configurations will be transitively exposed to consumers of the library, and as such will appear on the compile classpath of consumers. Dependencies found in the `implementation` configuration will, on the other hand, not be exposed to consumers, and therefore not leak into the consumers' compile classpath. This comes with several benefits:

* dependencies do not leak into the compile classpath of consumers anymore, so you will never accidentally depend on a transitive dependency
* faster compilation thanks to reduced classpath size
* less recompilations when implementation dependencies change: consumers would not need to be recompiled
* cleaner publishing: when used in conjunction with the new `maven-publish` plugin, Java libraries produce POM files that distinguish exactly between what is required to compile against the library and what is required to use the library at runtime (in other words, don't mix what is needed to compile the library itself and what is needed to compile against the library).

NOTE: The `compile` and `runtime` configurations have been removed with Gradle 7.0.
Please refer to the <<upgrading_version_6.adoc#sec:configuration_removal, upgrade guide>> how to migrate to `implementation` and `api` configurations`.

If your build consumes a published module with POM metadata, the Java and Java Library plugins both honor api and implementation separation through the scopes used in the POM.
Meaning that the compile classpath only includes Maven `compile` scoped dependencies, while the runtime classpath adds the Maven `runtime` scoped dependencies as well.

This often does not have an effect on modules published with Maven, where the POM that defines the project is directly published as metadata.
There, the compile scope includes both dependencies that were required to compile the project (i.e. implementation dependencies) and dependencies required to compile against the published library (i.e. API dependencies).
For most published libraries, this means that all dependencies belong to the compile scope.
If you encounter such an issue with an existing library, you can consider a <<component_metadata_rules.adoc#component-metadata-rules,component metadata rule>> to fix the incorrect metadata in your build.
However, as mentioned above, if the library is published with Gradle, the produced POM file only puts `api` dependencies into the compile scope and the remaining `implementation` dependencies into the runtime scope.

If your build consumes modules with Ivy metadata, you might be able to activate api and implementation separation as described <<variant_attributes.adoc#sec:ivy-mapping-to-variants,here>> if all modules follow a certain structure.

NOTE: Separating compile and runtime scope of modules is active by default in Gradle 5.0+.
In Gradle 4.6+, you need to activate it by adding `enableFeaturePreview('IMPROVED_POM_SUPPORT')` in _settings.gradle_.

[[sec:java_library_recognizing_dependencies]]
== Recognizing API and implementation dependencies

This section will help you identify API and Implementation dependencies in your code using simple rules of thumb. The first of these is:

 * Prefer the `implementation` configuration over `api` when possible

This keeps the dependencies off of the consumer's compilation classpath. In addition, the consumers will immediately fail to compile if any implementation types accidentally leak into the public API.

So when should you use the `api` configuration? An API dependency is one that contains at least one type that is exposed in the library binary interface, often referred to as its ABI (Application Binary Interface). This includes, but is not limited to:

* types used in super classes or interfaces
* types used in public method parameters, including generic parameter types (where _public_ is something that is visible to compilers. I.e. , _public_, _protected_ and _package private_ members in the Java world)
* types used in public fields
* public annotation types

By contrast, any type that is used in the following list is irrelevant to the ABI, and therefore should be declared as an `implementation` dependency:

* types exclusively used in method bodies
* types exclusively used in private members
* types exclusively found in internal classes (future versions of Gradle will let you declare which packages belong to the public API)

The following class makes use of a couple of third-party libraries, one of which is exposed in the class's public API and the other is only used internally. The import statements don't help us determine which is which, so we have to look at the fields, constructors and methods instead:

[[sec:example_making_the_difference_between_api_and_implementation]]
=== Example: Making the difference between API and implementation

[source,java]
.src/main/java/org/gradle/HttpClientWrapper.java
----
include::{snippetsPath}/reference/dependency-management/publishing/javaLibrary/common/src/main/java/org/gradle/HttpClientWrapper.java[tag=sample]
----

The _public_ constructor of `HttpClientWrapper` uses `HttpClient` as a parameter, so it is exposed to consumers and therefore belongs to the API. Note that `HttpGet` and `HttpEntity` are used in the signature of a _private_ method, and so they don't count towards making HttpClient an API dependency.

On the other hand, the `ExceptionUtils` type, coming from the `commons-lang` library, is only used in a method body (not in its signature), so it's an implementation dependency.

Therefore, we can deduce that `httpclient` is an API dependency, whereas `commons-lang` is an implementation dependency. This conclusion translates into the following declaration in the build script:

====
include::sample[dir="snippets/reference/dependency-management/publishing/javaLibrary/kotlin",files="build.gradle.kts[tags=dependencies]"]
include::sample[dir="snippets/reference/dependency-management/publishing/javaLibrary/groovy",files="build.gradle[tags=dependencies]"]
====

[[sec:java_library_configurations_graph]]
== Dependency configurations

The role of each configuration is described in the following tables.

[[sec:java_library_declarable_configurations]]
=== Declarable (Bucket) Configurations

[%header%autowidth,compact]
|===
| Configuration name | Role | Description

| `annotationProcessor`
| Declarable
| This configuration is used to declare annotation processors, ensuring they are available during the compile phase for code generation.

| `api`
| Declarable
| This is where you declare dependencies which are transitively exported to consumers, for compile time and runtime.

| `implementation`
| Declarable
| This is where you declare dependencies which are purely internal and not meant to be exposed to consumers (they are still exposed to consumers at runtime).

| `compileOnly`
| Declarable
| This is where you declare dependencies which are required at compile time, but not at runtime. This typically includes dependencies which are shaded when found at runtime.

| `compileOnlyApi`
| Declarable
| This is where you declare dependencies which are required at compile time by your module and consumers, but not at runtime. This typically includes dependencies which are shaded when found at runtime.

| `runtimeOnly`
| Declarable
| This is where you declare dependencies which are only required at runtime, and not at compile time.

| `testImplementation`
| Declarable
| This is where you declare dependencies which are used to compile tests.

| `testCompileOnly`
| Declarable
| This is where you declare dependencies which are only required at test compile time, but should not leak into the runtime. This typically includes dependencies which are shaded when found at runtime.

| `testRuntimeOnly`
| Declarable
| This is where you declare dependencies which are only required at test runtime, and not at test compile time.
|===

[[sec:java_library_consumable_configurations]]
=== Consumable Configurations

[%header%autowidth,compact]
|===
| Configuration name | Role | Description

| `apiElements`
| Consumable
| This configuration is meant to be used by consumers to retrieve all the elements necessary to compile against this library.

| `runtimeElements`
| Consumable
| This configuration is meant to be used by consumers to retrieve all the elements necessary to run this library.
|===

[[sec:java_library_resolvable_configurations]]
=== Resolvable Configurations

[%header%autowidth,compact]
|===
| Configuration name | Role | Description

| `compileClasspath`
| Resolvable
| This configuration contains the compile classpath of this library and is therefore used when invoking the Java compiler to compile it.

| `runtimeClasspath`
| Resolvable
| This configuration contains the runtime classpath of this library.

| `testCompileClasspath`
| Resolvable
| This configuration contains the test compile classpath of this library.

| `testRuntimeClasspath`
| Resolvable
| This configuration contains the test runtime classpath of this library.
|===

The following graph describes how configurations are setup when the Java Library plugin is in use.
You can use this legend to interpret the colors:

* The configurations in _blue_ are the ones a user should use to declare dependencies.
* The configurations in _purple_ are the ones used when a component compiles, or runs against the library.
* The configurations in _green_ are internal to the component, for its own use.
* The tasks in _grey_.

image::java-library-ignore-deprecated-main.png[]

And the next graph describes the test configurations setup:

image::java-library-ignore-deprecated-test.png[]

[[sec:java_library_modular]]
== Building Modules for the Java Module System

Since Java 9, Java itself offers a https://www.oracle.com/corporate/features/understanding-java-9-modules.html[module system] that allows for strict encapsulation during compile and runtime.
You can turn a Java library into a _Java Module_ by creating a `module-info.java` file in the `main/java` source folder.

[source,text]
----
src
└── main
    └── java
        └── module-info.java
----

In the module info file, you declare a _module name_, which packages of your module you want to _export_ and which other modules you _require_.

[source,java,indent=0]
----
include::{snippetsPath}/reference/platforms/jvm/module/groovy/src/main/java/module-info.java[]
----

To tell the Java compiler that a Jar is a module, as opposed to a traditional Java library, Gradle needs to place it on the so called _module path_.
It is an alternative to the _classpath_, which is the traditional way to tell the compiler about compiled dependencies.
Gradle will automatically put a Jar of your dependencies on the module path, instead of the classpath, if these three things are true:

* `java.modularity.inferModulePath` is *not* turned off
* We are actually building a module (as opposed to a traditional library) which we expressed by adding the `module-info.java` file.
  (Another option is to add the `Automatic-Module-Name` Jar manifest attribute as <<#sec:java_library_modular_auto,described further down>>.)
* The Jar our module depends on is itself a module, which Gradles decides based on the presence of a `module-info.class` -- the compiled version of the module descriptor -- in the Jar.
  (Or, alternatively, the presence of an `Automatic-Module-Name` attribute the Jar manifest)

[[sec:declaring_module_dependencies]]
=== Declaring module dependencies

There is a direct relationship to the dependencies you declare in the build file and the module dependencies you declare in the `module-info.java` file.
Ideally the declarations should be in sync as seen in the following table.

.Mapping between Java module directives and Gradle configurations to declare dependencies
[%header%autowidth,compact]
|===
| Java Module Directive | Gradle Configuration | Purpose

| `requires`
| `implementation`
| Declaring implementation dependencies

| `requires transitive`
| `api`
| Declaring API dependencies

| `requires static`
| `compileOnly`
| Declaring compile only dependencies

| `requires static transitive`
| `compileOnlyApi`
| Declaring compile only API dependencies
|===

Gradle currently does not automatically check if the dependency declarations are in sync.
This may be added in future versions.

For more details on declaring module dependencies, please refer to https://www.oracle.com/corporate/features/understanding-java-9-modules.html[documentation on the Java Module System].

[[sec:declaring_package_visibility_and_services]]
=== Declaring package visibility and services

The Java module system supports additional more fine granular encapsulation concepts than Gradle itself currently does.
For example, you explicitly need to declare which packages are part of your API and which are only visible inside your module.
Some of these capabilities might be added to Gradle itself in future versions.
For now, please refer to https://www.oracle.com/corporate/features/understanding-java-9-modules.html[documentation on the Java Module System] to learn how to use these features in Java Modules.

[[sec:declaring_module_versions]]
=== Declaring module versions

Java Modules also have a version that is encoded as part of the module identity in the `module-info.class` file.
This version can be inspected when a module is running.

====
include::sample[dir="snippets/reference/platforms/jvm/module/kotlin",files="build.gradle.kts[tags=declareVersion]"]
include::sample[dir="snippets/reference/platforms/jvm/module/groovy",files="build.gradle[tags=declareVersion]"]
====

[[sec:using_libraries_that_are_not_modules]]
=== Using libraries that are not modules

You probably want to use external libraries, like OSS libraries from Maven Central, in your modular Java project.
Some libraries, in their newer versions, are already full modules with a module descriptor.
For example, `com.google.code.gson:gson:2.8.9` that has the module name `com.google.gson`.

Others, like `org.apache.commons:commons-lang3:3.10`, may not offer a full module descriptor but will at least contain an `Automatic-Module-Name` entry in their manifest file to define the module's name (`org.apache.commons.lang3` in the example).
Such modules, that only have a name as module description, are called _automatic module_ that export **all** their packages and can read **all** modules on the module path.

A third case are traditional libraries that provide no module information at all -- for example `commons-cli:commons-cli:1.4`.
Gradle puts such libraries on the classpath instead of the module path.
The classpath is then treated as one module (the so called _unnamed_ module) by Java.

====
include::sample[dir="snippets/reference/platforms/jvm/module/kotlin",files="build.gradle.kts[tags=dependencies]"]
include::sample[dir="snippets/reference/platforms/jvm/module/groovy",files="build.gradle[tags=dependencies]"]
====

[source,java,indent=0]
----
include::{snippetsPath}/reference/platforms/jvm/module/kotlin/src/main/java/module-info.java[]
----

While a real module cannot directly depend on the unnamed module (only by adding command line flags), automatic modules can also see the unnamed module.
Thus, if you cannot avoid to rely on a library without module information, you can wrap that library in an automatic module as part of your project.
How you do that is described in the next section.

Another way to deal with non-modules is to enrich existing Jars with module descriptors yourself using <<artifact_transforms.adoc#sec:abm-artifact-transforms,artifact transforms>>.

[[sec:disabling_java_module_support]]
=== Disabling Java Module support

In rare cases, you might want to disable the built-in Java Module support and define the module path by other means.
To achieve this, you can disable the functionality to automatically put any Jar on the module path.
Then Gradle puts Jars with module information on the classpath, even if you have a `module-info.java` in your source set.
This corresponds to the behaviour of Gradle versions <7.0.

To make this work, you need to set `modularity.inferModulePath = false` on the Java extension (for all tasks) or on individual tasks.

====
include::sample[dir="snippets/reference/platforms/jvm/module-disabled/kotlin",files="build.gradle.kts[tags=disableModulePath]"]
include::sample[dir="snippets/reference/platforms/jvm/module-disabled/groovy",files="build.gradle[tags=disableModulePath]"]
====

[[sec:java_library_modular_auto]]
=== Building an automatic module

If you can, you should always write complete `module-info.java` descriptors for your modules.
Still, there are a few cases where you might consider to (initally) only provide a _module name_ for an automatic module:

- You are working on a library that is **not** a module but you want to make it usable as such in the next release.
  Adding an `Automatic-Module-Name` is a good first step (most popular OSS libraries on Maven central have done it by now).
- As discussed in the previous section, an automatic module can be used as an adapter between your real modules and a traditional library on the classpath.

To turn a normal Java project into an _automatic module_, just add the manifest entry with the module name:

====
include::sample[dir="snippets/reference/platforms/jvm/module/kotlin",files="build.gradle.kts[tags=automaticModuleName]"]
include::sample[dir="snippets/reference/platforms/jvm/module/groovy",files="build.gradle[tags=automaticModuleName]"]
====

[NOTE]
===
You can define an automatic module as part of a multi-project that otherwise defines real modules (e.g. as an adapter to another library).
While this works fine in the Gradle build, such automatic module projects are not correctly recognized by IDEA/Eclipse at the moment.
You can work around it by manually adding the Jar built for the automatic module to the dependencies of the project that does not find it in the IDE's UI.
===

[[sec:java_library_classes_usage]]
== Using classes instead of jar for compilation

A feature of the `java-library` plugin is that projects which consume the library only require the classes folder for compilation, instead of the full JAR.
This enables lighter inter-project dependencies as resources processing (`processResources` task) and archive construction (`jar` task) are no longer executed when only Java code compilation is performed during development.

NOTE: The usage or not of the classes output instead of the JAR is a _consumer_ decision.
For example, Groovy consumers will request classes _and_ processed resources as these may be needed for executing AST transformation as part of the compilation process.

[[sub:java_library_known_issues_memory]]
=== Increased memory usage for consumers

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, with the benefit of having the `compileJava` task up-to-date in more cases (e.g. changing resources no longer changes the input for `compileJava` tasks of upstream projects)

[[sub:java_library_known_issues_windows_performance]]
=== Significant build performance drop on Windows for huge multi-projects

Another side effect of the snapshotting of individual class files, only affecting Windows systems, is that the performance can significantly drop when processing a very large amount of class files on the compile classpath.
This only concerns very large multi-projects where a lot of classes are present on the classpath by using many `api` dependencies.
To mitigate this, you can set the `org.gradle.java.compile-classpath-packaging` system property to `true` to change the behavior of the Java Library plugin to use jars instead of class folders for everything on the compile classpath.
Note, since this has other performance impacts and potentially side effects, by triggering all jar tasks at compile time, it is only recommended to activate this if you suffer from the described performance issue on Windows.

[[sec:library_distribution]]
== Distributing a library

Aside from <<publishing_setup.adoc#publishing_components,publishing>> a library to a component repository, you may sometimes need to package a library and its dependencies in a distribution deliverable.
The <<java_library_distribution_plugin.adoc#java_library_distribution_plugin,Java Library Distribution Plugin>> is there to help you do just that.
