[Gradle] Build operations often performed in Java projects

Escape property files

Of the resources, property files containing multibyte characters such as Japanese It is common to escape with the native2ascii command. To escape property files in Gradle Depending on whether the property file encoding is the same as the project default settings There are two setting methods.

Prior knowledge

In Gradle's Java plugin, the elements related to resources are as follows.

--Terms Place resources under the src / main / resources and srt / test / resources directories --Task processResources and processTestResources tasks are in the directory set by the above rules Build / resources / main and build / resources / test of the placed resource files, respectively. Copy to a directory.

Same as the project default encoding

Specifies a filter that escapes Copy-type tasks such as the processResources task. = processResources Escape when the task makes a copy of the resource

import org.apache.tools.ant.filters.EscapeUnicode

processResources {
    filter(EscapeUnicode)
}

If it is different from the project default encoding

Escape Unicode filters set the default encoding, so Encoding cannot be specified. In this case, use Ant's native2ascii task and specify the encoding for each execution.

task native2ascii {
  doLast {
    ant.native2ascii(src: 'src/main/resources',
        dest: processResources.destinationDir,
        encoding: '<Arbitrary encoding>')
  }
}

Various settings of JAR file

Specifying the JAR file name

The JAR file name defaults to <project root folder name> .java. There are two ways to specify the file name.

jar {
    archiveName = 'sample.jar'
}

//If archiveName is set, that is prioritized.
//The JAR file name generated by the following settings is "sample"-bin-1.0-jdk17.It becomes "jar".
jar {
    baseName = 'sample'
    appendix = 'bin'
    version = '1.0'
    classifier = 'jdk17'
}

Specifying Javadoc output

Javadoc output is in the build / docs / javadoc directory by default Titled (-\ <version property setting >) Javadoc is generated. The output destination and title of Javadoc can be changed as follows.

javadoc {
    destinationDir = "example/dist/javadoc"
    title = 'example docs V1.0'
}

Javadoc xref

If you want to refer to an existing Javadoc, such as the Java standard API, You can generate a link to the Javadoc.

javadoc {
    options.links << 'http://docs.oracle.com/javase/jp/7/api/'
}

reference

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

[Gradle] Build operations often performed in Java projects
First gradle build (Java)
Control tasks performed in Gradle
Syntax examples often used in Java
Do HelloWorld in Java / IntelliJ / Gradle
Commands often used in MySQL operations
Hello world in Java and Gradle
Build Spring Boot + Docker image in Gradle
CICS-Run Java applications-(3) Build management with Gradle
Use OpenCV_Contrib (ArUco) in Java! (Part 1-Build) (OpenCV-3.4.4)
Static code analysis with Checkstyle in Java + Gradle
Build and test Java + Gradle applications with Wercker
Build Java x Spring x VSCode x Gradle on Docker (1)
Resolve "Cannot resolve symbol" in IntelliJ projects using Gradle
Using Gradle with VS Code, build Java → run
Java tips-Create a Spring Boot project in Gradle
Tips for generating files for eclipse projects in Gradle
vagrant java build
Partization in Java
Changes in Java 11
Rock-paper-scissors in Java
Pi in Java
FizzBuzz in Java
Build VS Code + WSL + Java + Gradle environment from scratch
[Gradle] Generate Javadoc including JavaScript in Java 1.8.0_121 or later
How to Git manage Java EE projects in Eclipse
Check coverage with Codecov in Java + Gradle + Wercker configuration