IDEA-62722 import project source encoding from maven config

This commit is contained in:
Sergey Evdokimov
2012-05-22 20:30:37 +04:00
parent 9e3041ba03
commit 1278181eb3
3 changed files with 107 additions and 0 deletions
@@ -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) {
@@ -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");
@@ -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("""
<groupId>test</groupId>
<artifactId>project</artifactId>
<version>1</version>
<properties>
<project.build.sourceEncoding>koi8-r</project.build.sourceEncoding>
</properties>
""")
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("""
<groupId>test</groupId>
<artifactId>project</artifactId>
<version>1</version>
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>koi8-r</encoding>
</configuration>
</plugin>
</plugins>
</build>
""")
def loadedText = VfsUtil.loadText(file)
assert loadedText == new String(text, "koi8-r")
}
}