Gradle Java Plugin: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
(Replaced content with "=External= * https://docs.gradle.org/current/userguide/java_plugin.html =TODEPLETE= {{Internal|Gradle Java Plugin TODELETE|Gradle Java Plugin TODELETE}}")
Line 4: Line 4:
=TODEPLETE=
=TODEPLETE=
{{Internal|Gradle Java Plugin TODELETE|Gradle Java Plugin TODELETE}}
{{Internal|Gradle Java Plugin TODELETE|Gradle Java Plugin TODELETE}}
=TODO=
<br>
<br>
<br>
<br>
<center>REFACTOR</center>
<br>
<br>
<br>
<br>
=Internal=
* [[Gradle_Plugins#Overview|Gradle Plugins]]
=Overview=
The Java plugin adds a standardized way of compiling, testing and bundling Java code. 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'>
apply plugin: 'java'
</syntaxhighlight>
This is all you need to define a Java project, provided that [[#Plugin_Conventions|plugin's conventions]] are followed.
The plugin adds "main" and "test" sourceSets, and initializes its variables as runtimeClasspath.
The plugin adds a "java" [[Gradle_Concepts#Software_Component|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|runtime]] configurations.
=Java Plugin Concepts=
==SourceSet==
{{External|[https://docs.gradle.org/current/userguide/building_java_projects.html#sec:java_source_sets Gradle Docs - Declaring your sources files via source sets]}}
{{External|https://docs.gradle.org/current/dsl/org.gradle.api.tasks.SourceSet.html}}
<font color=darkgray>Explore this, start here:
* https://docs.gradle.org/current/userguide/building_java_projects.html#sec:java_source_sets
* https://docs.gradle.org/current/dsl/org.gradle.api.tasks.SourceSet.html
* https://docs.gradle.org/current/userguide/building_java_projects.html#sec:java_source_sets
</font>
===The main Source Set===
The main source set contains the production source code of the project, which is compiled and assembled into a JAR. The classes and resources attached to the main source set are packaged as the [[#Bytecode_JAR|bytecode JAR artifact]] of the project.
src/main
      |
      +-- java
      |
      +--resources
===The test Source Set===
The test source set contains the test source code, which is compiled and executed using JUnit or TestNG. These are typically unit tests, but any test can be include in this source set as long as they all share the same compilation and runtime classpaths.
src/test
      |
      +-- java
      |
      +--resources
To add files from non-standard location to a source set:
<syntaxhighlight lang='groovy'>
sourceSets {
    main {
        resources {
            srcDirs "src/main/resources", "src/main/configs"
        }
    }
}
</syntaxhighlight>
===TODO===
<font color=darkgray>
Adding an extra directory to the main sourceset:
<syntaxhighlight lang='groovy'>
sourceSets.main.java.srcDirs += ['src/gen/java']
</syntaxhighlight>
</font>
==Artifacts==
Also see: {{Internal|Gradle_Artifacts#Artifact|Gradle Artifacts}}
===Bytecode JAR===
The Java plugin produces a byte code JAR by default, without any configuration. The [[#jar|jar]] task performs the mechanics of building the JAR. The JAR content is based on the classes and resources attached to the [[#The_main_Source_Set|main source set]]. The JAR is placed in <tt>build/libs</tt> subdirectory, and its name, unless explicitly set as described in the "[[#Custom_JAR_Name|Custom JAR Name]]" section, is given by the following pattern:
[''archiveBaseName'']-[''archiveAppendix'']-[''archiveVersion'']-[''archiveClassifier''].[''archiveExtension'']
The mechanics of building the JAR is explained below in the [[#jar|jar task]]  section.
The plugin creates by default a "thin" JAR, which only includes the project classes. A "fat" JAR, or an uber-JAR, is a self-sufficient archive which contains both classes and dependencies needed to run an application. To create a "fat" JAR, see:
{{Internal|Create a Fat JAR with Gradle|Create a Fat JAR with Gradle}}
===Source JAR===
<font color=darkgray>TODO</font>
==Testing with Gradle Java Plugin==
{{Internal|Testing with Gradle Java Plugin|Testing with Gradle Java Plugin}}
=Plugin Conventions=
==Conventional Project Layout==
{{External|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=
{{External|https://docs.gradle.org/current/userguide/java_plugin.html#tab:configurations}}
The Java plugin adds the following [[Gradle_Dependencies_and_Dependency_Configurations#Dependency_Configuration|dependency configurations]] to the project. The configurations are assigned to tasks such as compileJava and test.
===<font color=darkgray>api</font>===
<font color=darkgray>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."</font>
===apiElements===
===compile===
Deprecated, replaced by [[#implementation|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|compile]], [[#compileOnly|compileOnly]] and [[#implementation|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|runtimeOnly]].
"runtime" has a special signification when it comes to [[Gradle_Artifact_Publishing_Concepts#Configuration|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]].
===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|testImplementation]].
===testRuntimeOnly===
Extends [[#runtimeOnly|runtimeOnly]], everything available on [[#runtimeOnly|runtimeOnly]] will also be available on testRuntimeOnly. Runtime only dependencies for running tests. Used by task test.
===testRuntimeClasspath===
===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, as the only element. If a [[Gradle_Maven_Plugin#Overview|Maven plugin]] is present, it associates its "[[Gradle_Maven_Plugin#Overview|install]]" task with this configuration.
===providedRuntime===
<font color=darkgray>?</font>
=Properties=
The Java plugin adds a number of [[Gradle_Variables_and_Properties#Overview|properties]] to your project. These properties have default values which are usually sufficient to get started.
===sourceCompatibility===
===targetCompatibility===
=<span id='Plugin_Tasks'></span>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 [[Gradle War Plugin#Overview|War Plugin]], add more artifacts to this task.
===build===
===jar===
<font color=darkgray>TODO</font>
{{External|[https://docs.gradle.org/current/dsl/org.gradle.api.tasks.bundling.Jar.html The Jar Task]}}
The "jar" task assembles the production [[#Bytecode_JAR|bytecode JAR artifact]].
====jar Task Configuration====
<font color=darkgray>TODO</font>
====Custom JAR Name====
<font color=darkgray>TODO</font>
===compileJava===
===compileTestJava===
===processResources===
===processTestResources===
===testClasses===
==Verification Tasks==
===check===
Compiles and tests the code.
==Documentation Tasks==
===javadoc===
==Test Tasks==
===test===
See: {{Internal|Testing with Gradle Java Plugin|Testing with Gradle Java Plugin}}

Revision as of 02:41, 22 October 2020