Gradle Java Plugin

From NovaOrdis Knowledge Base
Jump to navigation Jump to search

External

Internal

Overview

The Java plugin adds tasks to your project which will compile and unit test your Java source code, and bundle it into a JAR file. The Java plugin is convention based. This means that the plugin defines default values for many aspects of the project, such as where the Java source files are located. If you follow the convention in your project, you generally don’t need to do much in your build script to get a useful build. To use it, build.gradle must include:

apply plugin: 'java'

This is all you need to define a Java project, provided that plugin's conventions are followed.

Plugin Conventions

Conventional Project Layout

https://docs.gradle.org/current/userguide/java_plugin.html#sec:java_project_layout

The Java plugin expects to find the production source code under:

src/main/java

Tests are expected under:

src/test/java

All files under:

src/main/resources 

are considered resources and they will be include in the JAR, as resources.

The files available under:

src/test/resources

are considered test resources and will be included in the classpath used to run tests.

All output files are created under the:

build 

directory.

The JAR file is created in:

build/libs

Dependency Configurations

https://docs.gradle.org/current/userguide/java_plugin.html#tab:configurations

The Java plugin adds the following dependency configurations to the project:

api

This dependency configuration defines dependencies required to compile the production source of the project which are part of the API exposed by the project.

compile

Deprecated, replaced by implementation.

implementation

This dependency configuration defines dependencies required to compile the production source of the project which are not part of the API exposed by the project.

compileOnly

compileClasspath

annotationProcessor

runtime

Deprecated.

runtimeOnly

runtimeClasspath

testCompile

Deprecated.

testImplementation

This dependency configuration defines dependencies required to compile and run the test source of the project. For example the project decided to write test code with the test framework JUnit.

testCompileOnly

testCompileClasspath

testRuntime

Deprecated.

testRuntimeOnly

testRuntimeClasspath

archives

default

Properties

The Java plugin adds a number of properties to your project. These properties have default values which are usually sufficient to get started.

Tasks

Build Tasks

clean

Deletes the build directory, removing all built files.

assemble

Compiles code and builds the JAR but does not run tests. Other plugins, such as the War Plugin, add more artifacts to this task.

build

jar

Verification Tasks

check

Compiles and tests the code.

Documentation Tasks

javadoc