Gradle Java Plugin: Difference between revisions
Line 111: | Line 111: | ||
===archives=== | ===archives=== | ||
The "archives" configuration is the [[Gradle_Artifact_Publishing_Concepts#Configuration|standard configuration to assign artifacts to]]. The Java plugin automatically assigns the default jar to this configuration. | The "archives" configuration is the [[Gradle_Artifact_Publishing_Concepts#Configuration|standard configuration to assign artifacts to]]. The Java plugin automatically assigns the default jar to this configuration. If a [[Gradle_Maven_Plugin#Overview|Maven plugin]] is present, it associates its "install" task with this configuration. | ||
===default=== | ===default=== |
Revision as of 19:27, 15 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.
Plugin Conventions
Conventional 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
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. 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.