Skip to content

Instantly share code, notes, and snippets.

@rponte
Last active February 20, 2024 06:44
Show Gist options
  • Save rponte/d660919434d094bbd35a1aabf7ef1bf0 to your computer and use it in GitHub Desktop.
Save rponte/d660919434d094bbd35a1aabf7ef1bf0 to your computer and use it in GitHub Desktop.
Configuring Gradle compiler encoding
apply plugin: 'java'
apply plugin: 'eclipse'
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
eclipseJdt << {
ant.propertyfile(file: ".settings/org.eclipse.core.resources.prefs") {
ant.entry(key: "eclipse.preferences.version", value: "1")
ant.entry(key: "encoding/<project>", value: "utf-8")
}
}
/**
* 1st approach: Setting encoding during compilation in Java and Test classes
*/
compileJava.options.encoding = "UTF-8"
compileTestJava.options.encoding = "UTF-8"
/**
* 2nd approach: Setting encoding during compilation in Java and Test classes
*
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}
tasks.withType(Test) {
systemProperty "file.encoding", "UTF-8"
}
*/
repositories {
jcenter()
maven {
url "https://oss.sonatype.org/content/groups/public/"
}
}
def springVersion = "4.3.2.RELEASE"
def dbunitVersion = "2.5.1"
dependencies {
/**
* Dependências das classes de produção
*/
compile "org.slf4j:slf4j-api:1.7.21"
compile "org.slf4j:jul-to-slf4j:1.7.21"
compile "org.slf4j:jcl-over-slf4j:1.7.21"
compile "org.slf4j:slf4j-log4j12:1.7.21"
/**
* Spring
*/
compile "org.springframework:spring-core:${springVersion}"
compile "org.springframework:spring-context:${springVersion}"
compile "org.springframework:spring-tx:${springVersion}"
compile "org.springframework:spring-jdbc:${springVersion}"
/**
* Dependências dos testes automatizados
*/
testCompile "junit:junit:4.12"
testCompile "org.dbunit:dbunit:${dbunitVersion}"
testCompile "br.com.triadworks:dbunitmanager:1.0.1-SNAPSHOT"
testCompile "org.springframework:spring-test:${springVersion}"
/**
* Dependências fixas
*/
compile fileTree(dir: "lib/oracle", include: '*.jar')
}
task wrapper(type: Wrapper) {
description = "Generates gradlew[.bat] scripts"
gradleVersion = "2.14.1"
}
@rponte
Copy link
Author

rponte commented Dec 21, 2017

@rybak
Copy link

rybak commented Feb 19, 2020

I would also recommend adding task Javadoc:

// 1st approach
javadoc.options.encoding = 'UTF-8'

// 2nd approach
tasks.withType(Javadoc) {
    options.encoding = 'UTF-8'
}

@rponte
Copy link
Author

rponte commented Feb 19, 2020

thanks @rybak

@FlyinPancake
Copy link

Hi all

For anyone using a Kotlin based project put this in yout build.gradle.kts

tasks.withType<JavaCompile> {
    options.encoding = "UTF-8"
}

tasks.withType<Test> {
    systemProperty("file.encoding", "UTF-8")
}

tasks.withType<Javadoc>{
    options.encoding = "UTF-8"
}

@hakanai
Copy link

hakanai commented Feb 9, 2022

There's also a more direct way for Test:

tasks.withType<Test> {
    defaultCharacterEncoding = "UTF-8"
}

And the same for JavaExec.

@chkpnt
Copy link

chkpnt commented Jan 9, 2024

Just a note: In order to benefit from the configuration avoidance API, it's advised to use the configureEach(Action)-method:

tasks.withType<JavaCompile>().configureEach() {
    options.encoding = "UTF-8"
}

@SmushyTaco
Copy link

tasks {
    withType<JavaCompile>().configureEach { options.encoding = "UTF-8" }
    withType<JavaExec>().configureEach { defaultCharacterEncoding = "UTF-8" }
    withType<Javadoc>().configureEach { options.encoding = "UTF-8" }
    withType<Test>().configureEach { defaultCharacterEncoding = "UTF-8" }
}

Here's the build.gradle.kts code for all your UTF-8 needs using the configuration avoidance API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment