Wednesday, 24 June 2015

Unmappable Character for Encoding

Hmmm, so I was working on a module of a big project. One of its modules has been developed by some other folks. Now, I checked out the code and as the recommended first step, tried to do a Maven Build of the whole project. But lo! It threw an error:

[FileName] error: unmappable character for encoding Cp1252

Now this file belonged to the other module written by the other team. After some Googling, I learnt that this kind of an Error is encountered due to Windows encoding issues (read not using the standard UTF8). Anyways, here's the two fixes available.

1. Approach 1: In the pom.xml of the file in error specify the encoding to be used as shown below
under <build/>:
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
</build> 


2. Approach 2: I prefer this approach as I frequently use Maven Build from the command prompt and this does not involve touching any file. So what one has to do is simply include "-Dproject.build.sourceEncoding=UTF-8" in the build command as shown below:

mvn clean install  -Dproject.build.sourceEncoding=UTF-8



And that's it! Works like a charm! :)