Gradle Java Plugin: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 194: Line 194:
====jar Task Configuration====
====jar Task Configuration====


Each of the "jar" task properties can be overridden in the [[Gradle_Concepts#Script_Block|script block]] invoked by "jar", as shown below:
<font color=darkgray>TODO</font>


<syntaxhighlight lang='groovy'>
====Custom JAR Name====
jar {
 
    project.archivesBaseName = "blah"
<font color=darkgray>TODO</font>
}
</syntaxhighlight>


===compileJava===
===compileJava===

Revision as of 01:43, 24 February 2019

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.

Java Plugin Concepts

SourceSet

https://docs.gradle.org/current/dsl/org.gradle.api.tasks.SourceSet.html

Explore this.

The main Source Set

The classes and resources attached to the main source set are packaged as the bytecode JAR artifact of the project.

Testing with Gradle Java Plugin

Testing with Gradle Java Plugin

Artifacts

Also see:

Gradle Artifacts

Bytecode JAR

The Java plugin produces a byte code JAR by default, without any configuration. The jar task performs the mechanics of building the JAR. The JAR content is based on the classes and resources attached to the main source set. The JAR is placed in build/libs subdirectory, and its name, unless explicitly set How?, is given by the following pattern:

[archiveBaseName]-[archiveAppendix]-[archiveVersion]-[archiveClassifier].[archiveExtension]

The mechanics of building the JAR is explained below in the jar task section.

Source JAR

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. The configurations are assigned to tasks such as compileJava and test.

api

I am not sure how this got here, it is not reported by gradle dependencies. "This dependency configuration defines dependencies required to compile the production sources of the project which are part of the API exposed by the project."

apiElements

compile

Deprecated, replaced by implementation. Dependencies for source set 'main'.

implementation

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

compileOnly

Compile time only dependencies, not used at runtime.

compileClasspath

The compile classpath for source set 'main'. Extends compile, compileOnly and implementation. It is the compile classpath, used when compiling sources. Used by task compileJava.

default

annotationProcessor

Annotation processors used during compilation.

runtime

Deprecated by runtimeOnly.

"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

Runtime only dependencies.

runtimeClasspath

testCompile

Deprecated. Superseded by testImplementation.

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

Additional dependencies only for compiling tests, not used at runtime.

testCompileClasspath

testRuntime

Deprecated . Includes all dependencies required to compile the project and tests and then run the tests. Replaced with testImplementation.

testRuntimeOnly

Extends runtimeOnly, everything available on runtimeOnly will also be available on testRuntimeOnly. Runtime only dependencies for running tests. Used by task test.

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.

providedRuntime

?

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

The Jar Task

The "jar" task assembles the production bytecode JAR artifact.

jar Task Configuration

TODO

Custom JAR Name

TODO

compileJava

compileTestJava

processResources

processTestResources

testClasses

Verification Tasks

check

Compiles and tests the code.

Documentation Tasks

javadoc

Test Tasks

test

See:

Testing with Gradle Java Plugin