开发者

Maven plugin to ensure UTF-8 Encoding?

Is there a Maven plugin I can use to fail my build if the builder notices a f开发者_JAVA百科ile that is not encoded with UTF-8?


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>


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜