Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven plugin to ensure UTF-8 Encoding?

Is there a Maven plugin I can use to fail my build if the builder notices a file that is not encoded with UTF-8?

like image 288
Nathan Feger Avatar asked Feb 25 '11 15:02

Nathan Feger


2 Answers

Yes, there is https://github.com/mikedon/encoding-enforcer

It uses the Maven Enforcer Plugin, and juniversalchardet to do the encoding detection.

UPDATE 2016-10-20: org.codehaus.mojo has an extra-enforcer-rules plugin which introduces a requireEncoding rule. It uses ICU4J for the encoding detection.

Usage is:

<plugin>
  <artifactId>maven-enforcer-plugin</artifactId>
  <!-- find the latest version at http://maven.apache.org/plugins/maven-enforcer-plugin/ -->
  <version>1.0</version> 
  <executions>
    <execution>
      <id>require-utf-8</id>
      <goals>
        <goal>enforce</goal>
      </goals>
      <configuration>
        <rules>
          <requireEncoding>
            <encoding>UTF-8</encoding>
            <includes>src/main/resources/**,src/test/resources/**</includes>
          </requireEncoding>
        </rules>
        <fastFail>false</fastFail>
      </configuration>
    </execution>
  </executions>
  <dependencies>
    <dependency>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>extra-enforcer-rules</artifactId>
      <!-- find the latest version at http://www.mojohaus.org/extra-enforcer-rules/ -->
      <version>1.0-beta-6</version>
    </dependency>
  </dependencies>
</plugin>
like image 198
ericbn Avatar answered Nov 15 '22 07:11

ericbn


Good choice on adopting Maven - no doubt you'll soon be a total convert! :)

You may want to look at the Maven enforcer plugin. As a start you could use the requireProperty rule to ensure that the project.build.sourceEncoding property is set to UTF-8.

As for checking the actual files themselves (i.e. checking whether someone has committed a non-unicode file), you could implement your own custom rule for the enforcer plugin. When this rule is executed, you'd need read all the resources in the project and find some method of detecting the encoding for each (e.g. iconv).

like image 34
joelittlejohn Avatar answered Nov 15 '22 07:11

joelittlejohn