To build a Java project in Gradle, you need a Java plugin. To apply the plugin, write the following in build.gradle.
build.gradle
apply plugin: 'java'
Java plug-in has four elements, "task", "convention", "property", and "source set". It is a packaged component. By learning about each element, you will understand how to build with Java plugins.
A source set is a concept that groups ** objects that should be compiled at the same time **. By default for Java plugins
--main: A source set of production code and production resources --test: A source set of test code and test resources
We provide the above two source sets.
The source code has the following features.
--Can be added freely with the build script --Has a unique name --You can specify the set of "Java source" and "resource" to be compiled. --You can specify the path to output the product of the compilation result (class file, etc.) --You can specify compile-time and run-time classpaths --Tasks and properties can be set for each source set
In Gradle, when you add a plugin, the work required for building is added as a task at the same time. The following tasks are also added to the Java plug-in.
task | Description |
---|---|
compileJava | Compile the production code. The target is java of the source set main.The extension stored in the directory specified by the srcDirs property is.java file. |
processResources | Copy the production code resources to the classpath. The target is resources of the source set main.The extension stored in the directory specified by the srcDirs property is.Files other than java. |
classes | Run the compileJava and processResources tasks. |
compileTestJava | Behaves the same as the compileJava task for the source set test. |
processTestResources | Behaves the same as the processResources task for the source set test. |
testClasses | Run the compileTestJava and processTestResources tasks. |
jar | Generate a JAR file. The default archive target is the output property of the main source set.(Output of compileJava and processResources task) |
javadoc | Output Javadoc for the main source set. |
test | test Runs the tests contained in the source set. The default test target is automatically extracted from the output of the testClasses task. |
uploadArchives | Artif(Build artifacts)Upload the archive of. |
clean | Delete the build output. By default the build directory is deleted. |
clean <TaskName> | Specified task<TaskName>Delete only the output of. |
assemble | Generate appropriate artifacts depending on the project content without running tests. By default, Java plugins are equivalent to jar tasks. |
check | Verify the project. The Java plugin defaults to the test task. |
build | Build the project. Run the check and assemble tasks. |
buildNeeded | Build not only the project to be built, but also the project for which the dependency is set. |
buildDependents | Build including the projects that depend on the project to be built. |
In Java plugins, when you add a source set The following tasks are automatically added for each source set.
The convention is "In a Java project, place Java files under the src / main / java directory" It is a promise like that. Java plug-ins have the following conventions.
--Production code source file → Place it under the src / main / java directory --Production code resources → Place it under the src / main / resources directory --Test code source file → Place it under the src / test / java directory --Test code resources → Place it under the src / test / resources directory
Properties are set to control the input / output target of tasks and the directory of conventions.
Properties provided by the Project object.
Property | Description | Default |
---|---|---|
testResultDir | XML file output destination of test results | build/test-results |
testReportDir | Test report(HTML)Output destination | build/reports/tests |
libsDir | Library(JAR file)Output destination | build/libs |
distDir | Distribution(Archives other than JAR files)Output destination | build/distributions |
docsDir | Document output destination such as Javadoc | build/docs |
sourceSets | Project source set | main, test source set container |
sourceCompatibility | Java version when compiling Java source | JVM version of the build script execution environment |
targetCompatibility | Java version to target for compile-time class generation | value of sourceCompatibility |
archivesBaseName | Base name of archive files such as JAR | project.The value of the name property(The default is the root directory name of the project) |
manifest | Manifest to include in all JAR files | None |
Access to properties in build scripts
println project.docsDir
println docsDir
Properties provided by the sourceSets property of the Project object.
Property | Description | Default |
---|---|---|
name | Source set name. Must be unique within the project | None |
output | Source set output | output.classDir、output.value of the resourceDir property |
output.classDir | Source set class file output destination | build/classes/<sourceSet> |
output.resourcesDir | Source set resource output destination | build/resources/<sourceSet> |
compileClasspath | Source set compile-time classpath | Configuration compile<SourceSet>Setting value of |
runtimeClasspath | Source set runtime classpath | output property, configuration runtime<sourceSet>Setting value of |
java | All Java sources in the source set | src/<sourceSet>/The extension under java is.java file |
java.srcDirs | Directory containing Java sources for the source set | src/<sourceSet>/java |
resources | All resources in the source set | src/<sourceSet>/The extension under resources is.Non-java files |
resources.srcDirs | Directory containing resources for the source set | src/<sourceSet>/resources |
allJava | All Java sources in the source set | java property settings(Subject to change due to plug-in application) |
allSource | All Java sources and resources in the source set | java, resources property settings(Subject to change due to plug-in application) |
Access to properties in build scripts
println project.sourceSets.test.compileClasspath
println sourceSets.test.compileClasspath
println sourceSets['test'].compileClasspath
This article is written with reference to the following books.
Reference book: Thorough introduction to Gradle Building an automation platform with next-generation build tools
Recommended Posts