Gradle Java Plugin: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 20: Line 20:
==Source Set==
==Source Set==
A source set is a logical grouping of source files and resources, typically associated on the base of their purpose: application production code, test code, etc. Each source set contains sources and resources from typically one, but possibly more directories, which are listed as part of the source set. The source set concept was introduced by the Java plugin, which also instantiates by default two source sets: [[#main_SourceSet|main]] and [[#test_SourceSet|test]].
A source set is a logical grouping of source files and resources, typically associated on the base of their purpose: application production code, test code, etc. Each source set contains sources and resources from typically one, but possibly more directories, which are listed as part of the source set. The source set concept was introduced by the Java plugin, which also instantiates by default two source sets: [[#main_SourceSet|main]] and [[#test_SourceSet|test]].
Each source set is internally maintained in the runtime project hierarchy as child of the Java plugin. However, it can be configured via a top-level script blocks:
<syntaxhighlight lang='groovy'>
plugins {
  id 'java'
}
sourceSets {
  main {
    java {
      srcDirs = ['src/main/java']
    }
  }
  test {
    java {
      srcDirs = ['src/test/java']
    }
  }
}
</syntaxhighlight>


Also see: {{Internal|Gradle_Dependencies_and_Dependency_Configurations#Configurations|Dependency Configurations}}
Also see: {{Internal|Gradle_Dependencies_and_Dependency_Configurations#Configurations|Dependency Configurations}}

Revision as of 02:27, 28 March 2021

External

Internal

TODEPLETE

Gradle Java Plugin TODELETE

Overview

The Java plugin adds Java compilation, testing and packaging capabilities to a project. It also serves as the basis for many of the other JVM language Gradle plugins.

plugins {
  id 'java'
}

However, Java Library plugin should be used by default with Java projects instead of Java plugin. Java Library plugin extends the Java plugin, so it offers all the features of the Java plugin, plus a set of additional ones:

Java Library Plugin

The Java plugin instantiates by default two source sets ("main" and "test"). Each of these source sets has an associated compile task: compileJava (which should have been called compileMainJava) for the "main" source set and compileTestJava for the "test" source set.

Concepts

Source Set

A source set is a logical grouping of source files and resources, typically associated on the base of their purpose: application production code, test code, etc. Each source set contains sources and resources from typically one, but possibly more directories, which are listed as part of the source set. The source set concept was introduced by the Java plugin, which also instantiates by default two source sets: main and test.

Each source set is internally maintained in the runtime project hierarchy as child of the Java plugin. However, it can be configured via a top-level script blocks:

plugins {
  id 'java'
}
sourceSets {
  main {
    java {
      srcDirs = ['src/main/java']
    }
  }
  test {
    java {
      srcDirs = ['src/test/java']
    }
  }
}

Also see:

Dependency Configurations

The "main" Source Set

The "main" source set is conventionally associated with all source files found under src/main/java and all resource files from src/main/resources. The sources managed by the "main" source set are compiled by the compileJava task. The jar task packages the "main" source set compiled classes and resources.

The "test" Source Set

The "test" source set is conventionally associated with all the source file under src/test/java. The sources managed by the "test" source set are compiled by the compileTestJava task, and the tests are executed by the test task.

Resource Files

Java Toolchain

The toolchain options protect against problems with the project being built with different Java versions.

Configuration

Various aspects related to Java compilation are set in the "java" script block:

java {
  toolchain {
    languageVersion = JavaLanguageVersion.of(11)
  }
}

Tasks

assemble

build

buildDependents

buildNeeded

check

classes

clean

compileJava

The "compileJava" task compiles the sources managed by the "main" source set. The task is backed by the JavaCompile type.

compileTestJava

The "compileTestJava" task compiles the sources managed by the "test" source set. The task is backed by the JavaCompile type.

jar

Assembles the JAR file from the classes produced by the "main" source set source files and "main" source set resources. It produces a single JAR conventionally named <project>-<version>.jar. Depends on the classes task. The task is backed by the Jar type. Configuration details in:

Jar Task Type

Note that Java Library plugin integrates the "jar" task with the standard lifecycle task assemble.

javadoc

Generates Javadoc for the "main" source set classes.

processResources

processTestResources

test

The "test" task runs all the tests from the "test" source set (src/test/java). Note that Java Library plugin integrates the "test" task with the standard lifecycle task check.

testClasses

Java Plugin Predefined Task Types

JavaCompile

JavaCompile exposed as "compileJava" task

Jar

Jar exposed as "jar" task

JavaExec

JavaExec