Java library path gradle

Building Java Applications with libraries Sample

To prepare your software project for growth, you can organize a Gradle project into multiple subprojects to modularize the software you are building. In this guide, you’ll learn how to structure such a project on the example of a Java application. However, the general concepts apply for any software you are building with Gradle. You can follow the guide step-by-step to create a new project from scratch or download the complete sample project using the links above.

What you’ll build

You’ll build a Java application that consists of an application and multiple library projects.

What you’ll need

  • A text editor or IDE — for example IntelliJ IDEA
  • A Java Development Kit (JDK), version 8 or higher — for example AdoptOpenJDK
  • The latest Gradle distribution

Create a project folder

Gradle comes with a built-in task, called init , that initializes a new Gradle project in an empty folder. The init task uses the (also built-in) wrapper task to create a Gradle wrapper script, gradlew .

The first step is to create a folder for the new project and change directory into it.

Run the init task

From inside the new project directory, run the init task using the following command in a terminal: gradle init . When prompted, select the 2: application project type and 3: Java as implementation language. Afterwards, select 2: Add library projects . Next you can choose the DSL for writing buildscripts — 1 : Groovy or 2: Kotlin . For the other questions, press enter to use the default values.

The output will look like this:

$ gradle init Select type of project to generate: 1: basic 2: application 3: library 4: Gradle plugin Enter selection (default: basic) [1..4] 2 Split functionality across multiple subprojects?: 1: no - only one application project 2: yes - application and library projects Enter selection (default: no - only one application project) [1..2] 2 Select implementation language: 1: C++ 2: Groovy 3: Java 4: Kotlin 5: Scala 6: Swift Enter selection (default: Java) [1..6] 3 Select build script DSL: 1: Groovy 2: Kotlin Enter selection (default: Groovy) [1..2] 1 Select test framework: 1: JUnit 4 2: TestNG 3: Spock 4: JUnit Jupiter Enter selection (default: JUnit 4) [1..4] Project name (default: demo): Source package (default: demo): BUILD SUCCESSFUL 2 actionable tasks: 2 executed

The init task generates the new project with the following structure:

├── gradle (1) │ └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew (2) ├── gradlew.bat (2) ├── settings.gradle.kts (3) ├── buildSrc │ ├── build.gradle.kts (4) │ └── src │ └── main │ └── kotlin (5) │ ├── demo.java-application-conventions.gradle.kts │ ├── demo.java-common-conventions.gradle.kts │ └── demo.java-library-conventions.gradle.kts ├── app │ ├── build.gradle.kts (6) │ └── src │ ├── main (7) │ │ └── java │ │ └── demo │ │ └── app │ │ ├── App.java │ │ └── MessageUtils.java │ └── test (8) │ └── java │ └── demo │ └── app │ └── MessageUtilsTest.java ├── list │ ├── build.gradle.kts (6) │ └── src │ ├── main (7) │ │ └── java │ │ └── demo │ │ └── list │ │ └── LinkedList.java │ └── test (8) │ └── java │ └── demo │ └── list │ └── LinkedListTest.java └── utilities ├── build.gradle.kts (6) └── src └── main (7) └── java └── demo └── utilities ├── JoinUtils.java ├── SplitUtils.java └── StringUtils.java
├── gradle (1) │ └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew (2) ├── gradlew.bat (2) ├── settings.gradle (3) ├── buildSrc │ ├── build.gradle (4) │ └── src │ └── main │ └── groovy (5) │ ├── demo.java-application-conventions.gradle │ ├── demo.java-common-conventions.gradle │ └── demo.java-library-conventions.gradle ├── app │ ├── build.gradle (6) │ └── src │ ├── main (7) │ │ └── java │ │ └── demo │ │ └── app │ │ ├── App.java │ │ └── MessageUtils.java │ └── test (8) │ └── java │ └── demo │ └── app │ └── MessageUtilsTest.java ├── list │ ├── build.gradle (6) │ └── src │ ├── main (7) │ │ └── java │ │ └── demo │ │ └── list │ │ └── LinkedList.java │ └── test (8) │ └── java │ └── demo │ └── list │ └── LinkedListTest.java └── utilities ├── build.gradle (6) └── src └── main (7) └── java └── demo └── utilities ├── JoinUtils.java ├── SplitUtils.java └── StringUtils.java
1 Generated folder for wrapper files
2 Gradle wrapper start scripts
3 Settings file to define build name and subprojects
4 Build script of buildSrc to configure dependencies of the build logic
5 Source folder for convention plugins written in Groovy or Kotlin DSL
6 Build script of the three subprojects — app , list and utilities
7 Java source folders in each of the subprojects
8 Java test source folders in the subprojects
Читайте также:  Создать свою анимацию css

You now have the project setup to build a Java application which is modularized into multiple subprojects.

Источник

The Java Library Plugin

The Java Library plugin expands the capabilities of the 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.

Usage

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

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 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.

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).
Читайте также:  Python if not isset

The compile and runtime configurations have been removed with Gradle 7.0. Please refer to the 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 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 here if all modules follow a certain structure.

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.

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:

Читайте также:  How to format date in javascript

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:

Example: Making the difference between API and implementation

// The following types can appear anywhere in the code // but say nothing about API or implementation usage import org.apache.commons.lang3.exception.ExceptionUtils; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; public class HttpClientWrapper < private final HttpClient client; // private member: implementation details // HttpClient is used as a parameter of a public method // so "leaks" into the public API of this component public HttpClientWrapper(HttpClient client) < this.client = client; >// public methods belongs to your API public byte[] doRawGet(String url) < HttpGet request = new HttpGet(url); try < HttpEntity entity = doGet(request); ByteArrayOutputStream baos = new ByteArrayOutputStream(); entity.writeTo(baos); return baos.toByteArray(); >catch (Exception e) < ExceptionUtils.rethrow(e); // this dependency is internal only >finally < request.releaseConnection(); >return null; > // HttpGet and HttpEntity are used in a private method, so they don't belong to the API private HttpEntity doGet(HttpGet get) throws Exception < HttpResponse response = client.execute(get); if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) < System.err.println("Method failed: " + response.getStatusLine()); >return response.getEntity(); > >

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:

Источник

Оцените статью