Repository: quarkusio/quarkus
Issue: Hot reload: the sources encoding used during recompilation should be dynamic

**Describe the bug**
Since #3467 got fixed, we're using a hardcoded `UTF-8` encoding during a hot reload compilation.

**Expected behavior**
Ideally, the encoding should come dynamically from the `project.build.sourceEncoding` Maven property or a Gradle equivalent.

Changed files:
devtools/gradle/src/main/java/io/quarkus/gradle/tasks/QuarkusDev.java, core/devmode/src/main/java/io/quarkus/dev/CompilationProvider.java, core/devmode/src/main/java/io/quarkus/dev/ClassLoaderCompiler.java, core/devmode/src/main/java/io/quarkus/dev/JavaCompilationProvider.java, devtools/maven/src/main/java/io/quarkus/maven/DevMojo.java, core/devmode/src/main/java/io/quarkus/dev/DevModeContext.java

diff --git a/core/devmode/src/main/java/io/quarkus/dev/ClassLoaderCompiler.java b/core/devmode/src/main/java/io/quarkus/dev/ClassLoaderCompiler.java
index b1cd0b2fce4..bd0b58c0a17 100644
--- a/core/devmode/src/main/java/io/quarkus/dev/ClassLoaderCompiler.java
+++ b/core/devmode/src/main/java/io/quarkus/dev/ClassLoaderCompiler.java
@@ -133,7 +133,8 @@ public ClassLoaderCompiler(ClassLoader classLoader,
                                     classPathElements,
                                     new File(i.getProjectDirectory()),
                                     new File(sourcePath),
-                                    new File(i.getClassesPath())));
+                                    new File(i.getClassesPath()),
+                                    context.getSourceEncoding()));
                 });
             }
         }
diff --git a/core/devmode/src/main/java/io/quarkus/dev/CompilationProvider.java b/core/devmode/src/main/java/io/quarkus/dev/CompilationProvider.java
index b3f3f689ab7..471dd2f714e 100644
--- a/core/devmode/src/main/java/io/quarkus/dev/CompilationProvider.java
+++ b/core/devmode/src/main/java/io/quarkus/dev/CompilationProvider.java
@@ -1,6 +1,8 @@
 package io.quarkus.dev;
 
 import java.io.File;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
 import java.nio.file.Path;
 import java.util.Collections;
 import java.util.Set;
@@ -24,19 +26,22 @@ class Context {
         private final File projectDirectory;
         private final File sourceDirectory;
         private final File outputDirectory;
+        private final Charset sourceEncoding;
 
         public Context(
                 String name,
                 Set<File> classpath,
                 File projectDirectory,
                 File sourceDirectory,
-                File outputDirectory) {
+                File outputDirectory,
+                String sourceEncoding) {
 
             this.name = name;
             this.classpath = classpath;
             this.projectDirectory = projectDirectory;
             this.sourceDirectory = sourceDirectory;
             this.outputDirectory = outputDirectory;
+            this.sourceEncoding = sourceEncoding == null ? StandardCharsets.UTF_8 : Charset.forName(sourceEncoding);
         }
 
         public String getName() {
@@ -58,5 +63,9 @@ public File getSourceDirectory() {
         public File getOutputDirectory() {
             return outputDirectory;
         }
+
+        public Charset getSourceEncoding() {
+            return sourceEncoding;
+        }
     }
 }
diff --git a/core/devmode/src/main/java/io/quarkus/dev/DevModeContext.java b/core/devmode/src/main/java/io/quarkus/dev/DevModeContext.java
index 66abc322235..e2ab7d443cb 100644
--- a/core/devmode/src/main/java/io/quarkus/dev/DevModeContext.java
+++ b/core/devmode/src/main/java/io/quarkus/dev/DevModeContext.java
@@ -22,6 +22,7 @@ public class DevModeContext implements Serializable {
     private final List<ModuleInfo> modules = new ArrayList<>();
     private final Map<String, String> systemProperties = new HashMap<>();
     private final Map<String, String> buildSystemProperties = new HashMap<>();
+    private String sourceEncoding;
 
     private final List<File> classesRoots = new ArrayList<>();
     private File frameworkClassesDir;
@@ -45,6 +46,14 @@ public Map<String, String> getBuildSystemProperties() {
         return buildSystemProperties;
     }
 
+    public String getSourceEncoding() {
+        return sourceEncoding;
+    }
+
+    public void setSourceEncoding(String sourceEncoding) {
+        this.sourceEncoding = sourceEncoding;
+    }
+
     public List<File> getClassesRoots() {
         return classesRoots;
     }
diff --git a/core/devmode/src/main/java/io/quarkus/dev/JavaCompilationProvider.java b/core/devmode/src/main/java/io/quarkus/dev/JavaCompilationProvider.java
index 4d278df56fe..a4accd940e2 100644
--- a/core/devmode/src/main/java/io/quarkus/dev/JavaCompilationProvider.java
+++ b/core/devmode/src/main/java/io/quarkus/dev/JavaCompilationProvider.java
@@ -3,7 +3,6 @@
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
-import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
@@ -44,7 +43,7 @@ public void compile(Set<File> filesToCompile, Context context) {
         }
         DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
         try (StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null,
-                StandardCharsets.UTF_8)) {
+                context.getSourceEncoding())) {
 
             fileManager.setLocation(StandardLocation.CLASS_PATH, context.getClasspath());
             fileManager.setLocation(StandardLocation.CLASS_OUTPUT, Collections.singleton(context.getOutputDirectory()));
diff --git a/devtools/gradle/src/main/java/io/quarkus/gradle/tasks/QuarkusDev.java b/devtools/gradle/src/main/java/io/quarkus/gradle/tasks/QuarkusDev.java
index 690974dcc50..5f186ace540 100644
--- a/devtools/gradle/src/main/java/io/quarkus/gradle/tasks/QuarkusDev.java
+++ b/devtools/gradle/src/main/java/io/quarkus/gradle/tasks/QuarkusDev.java
@@ -30,12 +30,14 @@
 
 import org.gradle.api.GradleException;
 import org.gradle.api.Project;
+import org.gradle.api.Task;
 import org.gradle.api.artifacts.Configuration;
 import org.gradle.api.artifacts.Dependency;
 import org.gradle.api.artifacts.DependencySet;
 import org.gradle.api.artifacts.ProjectDependency;
 import org.gradle.api.artifacts.ResolvedDependency;
 import org.gradle.api.plugins.Convention;
+import org.gradle.api.plugins.JavaPlugin;
 import org.gradle.api.plugins.JavaPluginConvention;
 import org.gradle.api.tasks.Input;
 import org.gradle.api.tasks.InputDirectory;
@@ -43,6 +45,7 @@
 import org.gradle.api.tasks.SourceSet;
 import org.gradle.api.tasks.SourceSetContainer;
 import org.gradle.api.tasks.TaskAction;
+import org.gradle.api.tasks.compile.JavaCompile;
 import org.gradle.api.tasks.options.Option;
 
 import io.quarkus.bootstrap.model.AppArtifact;
@@ -140,6 +143,7 @@ public void startDev() {
                     "Does the project have any source files?");
         }
         DevModeContext context = new DevModeContext();
+        context.setSourceEncoding(getSourceEncoding());
         try {
             List<String> args = new ArrayList<>();
             args.add(JavaBinFinder.findBin());
@@ -331,6 +335,14 @@ public void run() {
         }
     }
 
+    private String getSourceEncoding() {
+        Task javaCompile = getProject().getTasks().getByName(JavaPlugin.COMPILE_JAVA_TASK_NAME);
+        if (javaCompile != null) {
+            return ((JavaCompile) javaCompile).getOptions().getEncoding();
+        }
+        return null;
+    }
+
     private void copyOutputToConsole(InputStream is) {
         try (InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
                 BufferedReader br = new BufferedReader(isr)) {
diff --git a/devtools/maven/src/main/java/io/quarkus/maven/DevMojo.java b/devtools/maven/src/main/java/io/quarkus/maven/DevMojo.java
index caafb049be5..74c880cd77c 100644
--- a/devtools/maven/src/main/java/io/quarkus/maven/DevMojo.java
+++ b/devtools/maven/src/main/java/io/quarkus/maven/DevMojo.java
@@ -225,6 +225,7 @@ public void execute() throws MojoFailureException, MojoExecutionException {
                 devModeContext.getSystemProperties().put(e.getKey().toString(), (String) e.getValue());
             }
             devModeContext.getBuildSystemProperties().putAll((Map) project.getProperties());
+            devModeContext.setSourceEncoding(getSourceEncoding());
 
             final AppModel appModel;
             try {
@@ -368,6 +369,14 @@ public void run() {
         }
     }
 
+    private String getSourceEncoding() {
+        Object sourceEncodingProperty = project.getProperties().get("project.build.sourceEncoding");
+        if (sourceEncodingProperty != null) {
+            return (String) sourceEncodingProperty;
+        }
+        return null;
+    }
+
     private void addProject(DevModeContext devModeContext, LocalProject localProject) {
 
         String projectDirectory = null;