Maven clean install: Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources

Maven clean install: Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources

Resolving Encoding Issues in Spring Boot

If you’re experiencing encoding issues with Spring Boot, particularly with properties files, you’re not alone. This problem often arises due to the way Spring Boot handles character encoding. Here’s a comprehensive guide to addressing this issue.

Temporary Workaround

If you encounter a bug with Spring Boot 2.4.x that results in an error like:

Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources) on project application-etude: Input length = 1

You can temporarily resolve this by modifying your pom.xml to use an earlier version of the Maven Resources Plugin:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>3.1.0</version>
</plugin>

Permanent Solution: Ensure Proper Encoding

The root cause of many encoding issues is often related to how properties files are encoded. Spring Boot defaults to reading properties files as UTF-8, which can lead to problems if the files are actually encoded in ISO-8859-1 or another format. Here’s how to ensure your properties files are correctly encoded:

  1. Check File Encoding: Make sure that your properties files are saved in UTF-8 encoding. You can do this in your IDE (like IntelliJ IDEA) by setting the file encoding to UTF-8.
  2. Configure Spring Boot: In your Spring Boot application, ensure that you configure the @PropertySource annotation to specify the encoding:
@Configuration
@PropertySource(value = "classpath:application.properties", encoding = "UTF-8")
public class AppConfig {
    // Configuration beans
}
  1. Set Encoding in Application Properties: Additionally, you can set the encoding in your application.properties file:
spring.messages.encoding=UTF-8
server.tomcat.uri-encoding=UTF-8
spring.http.encoding.charset=UTF-8
spring.thymeleaf.encoding=UTF-8
  1. Use Character Encoding Filter: Implement a character encoding filter in your configuration to ensure that all requests and responses are treated as UTF-8:
@Bean
public FilterRegistrationBean<CharacterEncodingFilter> characterEncodingFilter() {
    CharacterEncodingFilter filter = new CharacterEncodingFilter();
    filter.setEncoding("UTF-8");
    filter.setForceEncoding(true);
    return new FilterRegistrationBean<>(filter);
}

Additional Considerations

  • Check for “Junk Characters”: If you see unexpected characters (like “” or “�”), this usually indicates a mismatch between the expected and actual encoding. Ensure that your properties files do not contain junk characters by re-saving them in UTF-8.
  • Testing: After making these changes, test your application to ensure that all properties are loaded correctly and that accented characters display as expected.

By following these steps, you should be able to resolve the encoding issues in your Spring Boot application effectively. If problems persist, consider reviewing your IDE settings and the encoding of all related files.

Citations:
[1] https://spring.io/blog/2021/11/18/spring-boot-2-4-13-available-now
[2] https://stackoverflow.com/questions/28112852/springboot-utf-8-doesnt-work-in-messages-properties
[3] https://stackoverflow.com/questions/26861864/spring-and-character-encoding-in-properties-files
[4] https://github.com/spring-projects/spring-boot/issues/5361
[5] https://stackoverflow.com/questions/27882191/spring-boot-default-properties-encoding-change
[6] https://codingpieces.wordpress.com/2017/02/20/how-to-fix-spring-properties-encoding-issues-iso-8859-1-utf-8-and-so-on/
[7] https://dzone.com/articles/resolve-encoding-issues-of-resource-files-in-java

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *