Search code examples
springencodingmaven-3maven-resources-plugin

maven encoding resources spring messageSource


Having a spring project with this messageSource:

@Bean
    public MessageSource messageSource() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setBasenames("Language", "original/Language");
        messageSource.setDefaultEncoding("UTF-8");
        messageSource.setFallbackToSystemLocale(true);
        return messageSource;
    }

And a test as:

@Test
    public void getMessageFromOriginal() {
        String message = messageSource.getMessage("javax.portlet.description.2", null, new Locale.Builder().setLanguage("es").build());
        Assert.assertNotNull(message);
        Assert.assertEquals("Mi Cuenta organiza toda tu información en una ubicación sencilla de utilizar. Los usuarios pueden editar sus perfiles y ver la membresía del sitio y las organizaciones y grupos de usuarios a los que pertenecen.", message);
    }

If I run the test from IDE it works as expected, but when I use mvn to execute the tests I get:

LanguageTest.getMessageFromOriginal:53 expected:<...za toda tu informaci[??n en una ubicaci??n sencilla de utilizar. Los usuarios pueden editar sus perfiles y ver la membres??]a del sitio y las or...> but was:<...za toda tu informaci[?n en una ubicaci?n sencilla de utilizar. Los usuarios pueden editar sus perfiles y ver la membres?]a del sitio y las or...>

On my parent pom.xml I'm using:

<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>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.7</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>

What I'm doing wrong?


Solution

  • The default is to define the following:

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

    This results in that the above property will automatically being picked up by maven-resources-plugin, maven-compiler-plugin etc. which means you can remove this part of your pom:

    <configuration>
      <encoding>UTF-8</encoding>
    </configuration>