mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-62722 import project source encoding from maven config
This commit is contained in:
+18
@@ -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");
|
||||
|
||||
+76
@@ -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")
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user