Maven:UTF-8中的源代码不工作? [英] Maven: Source Encoding in UTF-8 not working?

查看:126
本文介绍了Maven:UTF-8中的源代码不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一个项目从Ant转换为Maven,并且我遇到一个特定的单元测试问题,处理UTF-8字符。问题是关于以下字符串:

  String l_string =ČäÁÓý\\\
€řЖжЦ\\\
ηЯФКЛ;

问题是单元测试失败,因为String被读取如下:

 ?äÁÓý
€????

java类保存为UTF-8,并且还将UTF-8的构建编码指定为pom.xml。



这是我的 pom.xml 的摘录:

  ... 

<属性>
< project.build.sourceEncoding> UTF-8< /project.build.sourceEncoding>
< / properties>

...

< build>
< plugins>
< plugin>
< groupId> org.apache.maven.plugins< / groupId>
< artifactId> maven-compiler-plugin< / artifactId>
< version> 3.1< / version>
< configuration>
< source> 1.6< / source>
< target> 1.6< / target>
< encoding> $ {project.build.sourceEncoding}< / encoding>
< / configuration>
< / plugin>
< plugin>
< artifactId> maven-assembly-plugin< / artifactId>
< version> 2.4< / version>
< configuration>
< descriptorRefs>
< descriptorRef> jar-with-dependencies< / descriptorRef>
< / descriptorRefs>
< / configuration>
< / plugin>
< plugin>
< groupId> org.apache.maven.plugins< / groupId>
< artifactId> maven-surefire-plugin< / artifactId>
< version> 2.15< / version>
< / plugin>
< plugin>
< groupId> org.apache.maven.plugins< / groupId>
< artifactId> maven-surefire-report-plugin< / artifactId>
< version> 2.15< / version>
< / plugin>
< / plugins>
< / build>

我在这里缺少一些东西吗?如果有人可以帮助我,那将是很棒的。



更新



关于测试代码:

  @Test 
public void testTransformation()
{

$ b System.out.println(>>>+ l_string);
c_log.info(l_string);
StringBuffer l_stringBuffer = new StringBuffer();
int l_stringLength = l_string.length();

String l_fileName = System.getProperty(user.dir)+ File.separator +transform+ File.separator +TransformationMap.properties;
Transformation.init(l_fileName);

属性l_props = Transformation.getProps(); (int i = 0; i {
char l_char = l_string.charAt(i);

int l_intValue =(int)l_char;
if(l_intValue< = 255)
{
l_stringBuffer.append(l_char);
}
else
{
l_stringBuffer.append(l_props.getProperty(String.valueOf(l_char),));
}
}
c_log.info(l_stringBuffer.toString());
byte [] l_bytes = l_string.getBytes();
byte [] l_transformedBytes = Transformation.transform(l_bytes);
assertNotNull(l_transformedBytes);

}

以下逻辑并不真正相关(?),因为之后第一个sysout提到?打印而不是正确的字符(因此以下测试失败)。还没有使用默认的平台编码。



测试根据TransformationMap.properties文件转换每个字符,它的格式如下(只是摘录) :

 Ý= Y 
ý= y
Ž= Z
ž= z
°=。
€= EUR

应该注意的是,当我建立测试时没有任何问题

解决方案

我已经找到一个解决方案:



我不得不将编码传递给maven-surefire-plugin,但通常的

 < encoding> $ {project.build.sourceEncoding}< /编码> 

没有工作。我仍然不知道为什么,但是当我将命令行参数传递到插件中时,测试的工作原理应该是:

  <插件> 
< groupId> org.apache.maven.plugins< / groupId>
< artifactId> maven-surefire-plugin< / artifactId>
< version> 2.15< / version>
< configuration>
< argLine> -Dfile.encoding = UTF-8< / argLine>
< / configuration>
< / plugin>

感谢所有回复和其他评论!


i am converting a project from Ant to Maven and i'm having problems with a specific unit test which deals with UTF-8 characters. The problem is about the following String:

String l_string = "ČäÁÓý\n€řЖжЦ\n№ЯФКЛ";

The problem is that the unit test fails, because the String is read as the following:

?äÁÓý
€????
?????

The java class is saved as UTF-8 and i also specify the build encoding to UTF-8 in the pom.xml.

Here is an excerpt of my pom.xml:

...

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

...

<build>
<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
            <source>1.6</source>
            <target>1.6</target>
            <encoding>${project.build.sourceEncoding}</encoding>
        </configuration>
    </plugin>
    <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.4</version>
        <configuration>
            <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
        </configuration>
    </plugin>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>2.15</version>
    </plugin>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-report-plugin</artifactId>
      <version>2.15</version>
    </plugin>
 </plugins>
</build>

Am i missing something here? It would be great, if someone could help me here.

Update

Regarding the test code:

@Test
public void testTransformation()
{

    String l_string = "ČäÁÓý\n€řЖжЦ\n№ЯФКЛ";
    System.out.println( ">>> " + l_string );
     c_log.info( l_string );
    StringBuffer l_stringBuffer = new StringBuffer();
    int l_stringLength = l_string.length();

    String l_fileName = System.getProperty( "user.dir" ) + File.separator + "transformation" + File.separator + "TransformationMap.properties";
    Transformation.init( l_fileName );

    Properties l_props = Transformation.getProps();
    for ( int i = 0; i < l_stringLength; i++ )
    {
        char l_char = l_string.charAt( i );
        int l_intValue = (int) l_char;
        if ( l_intValue <= 255 )
        {
            l_stringBuffer.append( l_char );
        }
        else
        {
            l_stringBuffer.append( l_props.getProperty( String.valueOf( l_char ), "" ) );
        }
    }
    c_log.info( l_stringBuffer.toString() );
    byte[] l_bytes = l_string.getBytes();
    byte[] l_transformedBytes = Transformation.transform( l_bytes );
    assertNotNull( l_transformedBytes );

}

The following logic isn't really relevant(?) because after the first sysout the before mentioned "?" are printed instead of the correct characters (and therefore the following tests fail). There is also no use of a default platform encoding.

The test converts each character according to the TransformationMap.properties file, which is in the following form (just an excerpt):

Ý=Y
ý=y
Ž=Z
ž=z
°=.
€=EUR

It should be noted that the test runs without any problem when i build the project with Ant.

解决方案

I have found a "solution" myself:

I had to pass the encoding into the maven-surefire-plugin, but the usual

<encoding>${project.build.sourceEncoding}</encoding>

did not work. I still have no idea why, but when i pass the command line arguments into the plugin, the tests works as they should:

<plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>2.15</version>
      <configuration>
        <argLine>-Dfile.encoding=UTF-8</argLine>
      </configuration>
</plugin>

Thanks for all your responses and additional comments!

这篇关于Maven:UTF-8中的源代码不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆