Gradle Java Plugin: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 9: Line 9:
=Overview=
=Overview=


The Java plugin adds [[Gradle_Task#Overview|tasks]] to your [[Gradle_Concepts_ToDeplete#Project|project]] which will compile and unit test your Java source code, and bundle it into a JAR file. The Java plugin is [[#Plugin_Conventions|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, [[Gradle_Project_and_Build_Script#Overview|build.gradle]] must include:
The Java plugin adds [[Gradle_Task#Overview|tasks]] to your [[Gradle_Project_and_Build_Script#Overview|project]] which will compile and unit test your Java source code, and bundle it into a JAR file. The Java plugin is [[#Plugin_Conventions|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, [[Gradle_Project_and_Build_Script#Overview|build.gradle]] must include:


<syntaxhighlight lang='groovy'>
<syntaxhighlight lang='groovy'>

Revision as of 01:34, 20 May 2018

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.

The plugin adds "main" and "test" sourceSets, and initializes its variables as runtimeClasspath.

The plugin adds a "java" software component accessible via components.java. The artifact associated with the Java component is the generated JAR file, and the dependencies are those of the runtime configurations.

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 by api and implementation.

"runtime" has a special signification when it comes to artifact publishing. It the project is used as a library, the "runtime" configuration is used to declare the artifacts of the library and their dependencies. The dependencies declared here are assumed to be the dependencies of the published artifact.

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

The "archives" configuration is the standard configuration to assign artifacts to. The Java plugin automatically assigns the default jar to this configuration, as the only element. If a Maven plugin is present, it associates its "install" task with this configuration.

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.

sourceCompatibility

targetCompatibility

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

compileJava

compileTestJava

processResources

processTestResources

testClasses

Verification Tasks

check

Compiles and tests the code.

Documentation Tasks

javadoc