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?
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>

- 10,163
- 3
- 47
- 55
-
I get an error when I run the plugin using configuration from the git site... (Maven v. 3.3.9 + java 7): [ERROR] Failed to execute goal org.apache.maven.plugins:maven-enforcer-plugin:1.2:enforce (default-cli) on project: The parameters 'rules' for goal org.apache.maven.plugins:maven-enforcer-plugin:1.2:enforce are missing or invalid -> [Help 1] ..... Rules configuration is identical from github, just copy/paste – niken Jul 15 '16 at 13:42
-
The readme on your fork says to build from mikedon's url, which is confusing, so I built from ericbn but still get the same error? The parameters 'rules' for goal org.apache.maven.plugins:maven-enforcer-plugin:1.2:enforce are missing or invalid... im running via mvn enforcer:enforce ? – niken Jul 15 '16 at 14:42
-
2Ok, so i finally got this to work based on info in this answer: http://stackoverflow.com/questions/24827194/maven-enforcer-plugin-missing-or-invalid-rules THIS IS WHY I HATE MAVEN AND ANYONE WHO CHOOSES TO USE THIS TOOL... you need to set id for the execution do 'default-cli' AARRRGH BURN IN HELL MAVEN – niken Jul 15 '16 at 15:15
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).

- 11,665
- 2
- 41
- 54