diff --git a/plugins/maven/src/main/java/org/jetbrains/idea/maven/importing/MavenModuleImporter.java b/plugins/maven/src/main/java/org/jetbrains/idea/maven/importing/MavenModuleImporter.java
index af2a3127887d..4b11cb43148b 100644
--- a/plugins/maven/src/main/java/org/jetbrains/idea/maven/importing/MavenModuleImporter.java
+++ b/plugins/maven/src/main/java/org/jetbrains/idea/maven/importing/MavenModuleImporter.java
@@ -20,12 +20,15 @@ import com.intellij.openapi.module.ModuleType;
import com.intellij.openapi.roots.DependencyScope;
import com.intellij.openapi.roots.ModifiableRootModel;
import com.intellij.openapi.util.Pair;
+import com.intellij.openapi.vfs.encoding.EncodingManager;
import com.intellij.pom.java.LanguageLevel;
import org.jetbrains.idea.maven.model.MavenArtifact;
import org.jetbrains.idea.maven.model.MavenConstants;
import org.jetbrains.idea.maven.project.*;
import org.jetbrains.idea.maven.utils.MavenUtil;
+import java.nio.charset.Charset;
+import java.nio.charset.UnsupportedCharsetException;
import java.util.List;
import java.util.Map;
@@ -65,6 +68,7 @@ public class MavenModuleImporter {
configFolders();
configDependencies();
configLanguageLevel();
+ configEncoding();
}
public void preConfigFacets() {
@@ -138,6 +142,20 @@ public class MavenModuleImporter {
return DependencyScope.COMPILE;
}
+ private void configEncoding() {
+ if (Boolean.parseBoolean(System.getProperty("maven.disable.encode.import"))) return;
+
+ String encoding = myMavenProject.getEncoding();
+ if (encoding != null) {
+ try {
+ EncodingManager.getInstance().setEncoding(myMavenProject.getDirectoryFile(), Charset.forName(encoding));
+ }
+ catch (UnsupportedCharsetException ignored) {
+
+ }
+ }
+ }
+
private void configLanguageLevel() {
final LanguageLevel level = LanguageLevel.parse(myMavenProject.getSourceLevel());
if (level != null) {
diff --git a/plugins/maven/src/main/java/org/jetbrains/idea/maven/project/MavenProject.java b/plugins/maven/src/main/java/org/jetbrains/idea/maven/project/MavenProject.java
index 5f3f6e99157a..b49f872d8fcd 100644
--- a/plugins/maven/src/main/java/org/jetbrains/idea/maven/project/MavenProject.java
+++ b/plugins/maven/src/main/java/org/jetbrains/idea/maven/project/MavenProject.java
@@ -748,6 +748,19 @@ public class MavenProject {
return null;
}
+ @Nullable
+ public String getEncoding() {
+ String encoding = myState.myProperties.getProperty("project.build.sourceEncoding");
+ if (encoding != null) return encoding;
+
+ Element pluginConfiguration = getPluginConfiguration("org.apache.maven.plugins", "maven-resources-plugin");
+ if (pluginConfiguration != null) {
+ return pluginConfiguration.getChildTextTrim("encoding");
+ }
+
+ return null;
+ }
+
@Nullable
public String getSourceLevel() {
return getCompilerLevel("source");
diff --git a/plugins/maven/src/test/java/org/jetbrains/idea/maven/importing/EncodingImportingTest.groovy b/plugins/maven/src/test/java/org/jetbrains/idea/maven/importing/EncodingImportingTest.groovy
new file mode 100644
index 000000000000..8a296154799d
--- /dev/null
+++ b/plugins/maven/src/test/java/org/jetbrains/idea/maven/importing/EncodingImportingTest.groovy
@@ -0,0 +1,76 @@
+/*
+ * Copyright 2000-2012 JetBrains s.r.o.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.jetbrains.idea.maven.importing
+
+import org.jetbrains.idea.maven.MavenImportingTestCase
+import com.intellij.openapi.vfs.VirtualFile
+import com.intellij.openapi.vfs.VfsUtil
+
+/**
+ * @author Sergey Evdokimov
+ */
+class EncodingImportingTest extends MavenImportingTestCase {
+
+ public void testEncodingDefinedByProperty() {
+ byte[] text = [-12, -59, -53, -45, -44] // Russian text in koi8-r encoding.
+
+ VirtualFile file = createProjectSubFile("src/main/resources/A.txt")
+ file.setBinaryContent(text)
+
+ importProject("""
+test
+project
+1
+
+
+koi8-r
+
+""")
+
+ def loadedText = VfsUtil.loadText(file)
+
+ assert loadedText == new String(text, "koi8-r")
+ }
+
+ public void testEncodingDefinedByPluginConfig() {
+ byte[] text = [-12, -59, -53, 45, -44] // Russian text in koi8-r encoding.
+
+ VirtualFile file = createProjectSubFile("src/main/resources/A.txt")
+ file.setBinaryContent(text)
+
+ importProject("""
+test
+project
+1
+
+
+
+
+ maven-resources-plugin
+
+ koi8-r
+
+
+
+
+""")
+
+ def loadedText = VfsUtil.loadText(file)
+
+ assert loadedText == new String(text, "koi8-r")
+ }
+
+}