如何使用 checkstyle-plugin 验证源文件中使用的编码?
How to verify used encoding in sourcefiles using checkstyle-plugin?
简单的事情:如果选定的文件(.java
、.xml
)没有正确编码(我想强制执行 UTF-8
,我希望能够通过 checkstyle 中断构建源文件)。
我目前正在为许多其他构建破坏者使用 checkstyle,例如强制使用制表符的正确换行 and/or,但似乎没有 FileEncodingChecker
之类的东西。
问题:如果 checkstyle 根本无法做到这一点:是否有其他插件可以完成这项工作?
Maven 编码(源和资源)由标准 project.build.sourceEncoding
属性 处理,作为一种好的做法,它确实应该存在并设置为 UTF-8
值。
来自 maven-resources-plugin
的官方文档
The best practice is to define encoding for copying filtered resources via the property ${project.build.sourceEncoding}
which should be defined in the pom properties section
此 属性 被选为 maven-resources-plugin
的 encoding
property of the maven-compiler-plugin
and the encoding
属性 的默认值。
为了进一步强制其存在,您可以使用 maven-enforcer-plugin
and its requireProperty
规则,以强制存在 project.build.sourceEncoding
属性 及其在 UTF-8
的值.也就是说,如果未设置 属性 并且没有此确切值,构建将失败。
下面是这样一个配置示例,要添加到您的 pom.xml
文件,build/plugins
部分:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.4.1</version>
<executions>
<execution>
<id>enforce-property</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireProperty>
<property>project.build.sourceEncoding</property>
<message>Encoding must be set and at UTF-8!</message>
<regex>UTF-8</regex>
<regexMessage>Encoding must be set and at UTF-8</regexMessage>
</requireProperty>
</rules>
<fail>true</fail>
</configuration>
</execution>
</executions>
</plugin>
请注意,对于 project.reporting.outputEncoding
属性。
也可以这样做
进一步阅读 Stack Overflow:
- How to configure encoding in maven
- Maven platform encoding
- Maven: Source Encoding in UTF-8 not working?
奖金:由于我们在 Stack Overflow 上,CEO 可能会很高兴再次看到他的旧文章:The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets
测试
给定以下 Java 代码:
package com.sample;
public class Main {
public void 漢字() {
}
}
并在 Maven 中设置以下内容:
<properties>
<project.build.sourceEncoding>US-ASCII</project.build.sourceEncoding>
</properties>
实际上会使构建失败,因为 US-ASCII
是 7 位并且会导致非法字符错误。 UTF-8
不会发生同样的情况,它使用 8 位代替。
简单的事情:如果选定的文件(.java
、.xml
)没有正确编码(我想强制执行 UTF-8
,我希望能够通过 checkstyle 中断构建源文件)。
我目前正在为许多其他构建破坏者使用 checkstyle,例如强制使用制表符的正确换行 and/or,但似乎没有 FileEncodingChecker
之类的东西。
问题:如果 checkstyle 根本无法做到这一点:是否有其他插件可以完成这项工作?
Maven 编码(源和资源)由标准 project.build.sourceEncoding
属性 处理,作为一种好的做法,它确实应该存在并设置为 UTF-8
值。
来自 maven-resources-plugin
The best practice is to define encoding for copying filtered resources via the property
${project.build.sourceEncoding}
which should be defined in the pom properties section
此 属性 被选为 maven-resources-plugin
的 encoding
property of the maven-compiler-plugin
and the encoding
属性 的默认值。
为了进一步强制其存在,您可以使用 maven-enforcer-plugin
and its requireProperty
规则,以强制存在 project.build.sourceEncoding
属性 及其在 UTF-8
的值.也就是说,如果未设置 属性 并且没有此确切值,构建将失败。
下面是这样一个配置示例,要添加到您的 pom.xml
文件,build/plugins
部分:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.4.1</version>
<executions>
<execution>
<id>enforce-property</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireProperty>
<property>project.build.sourceEncoding</property>
<message>Encoding must be set and at UTF-8!</message>
<regex>UTF-8</regex>
<regexMessage>Encoding must be set and at UTF-8</regexMessage>
</requireProperty>
</rules>
<fail>true</fail>
</configuration>
</execution>
</executions>
</plugin>
请注意,对于 project.reporting.outputEncoding
属性。
进一步阅读 Stack Overflow:
- How to configure encoding in maven
- Maven platform encoding
- Maven: Source Encoding in UTF-8 not working?
奖金:由于我们在 Stack Overflow 上,CEO 可能会很高兴再次看到他的旧文章:The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets
测试
给定以下 Java 代码:
package com.sample;
public class Main {
public void 漢字() {
}
}
并在 Maven 中设置以下内容:
<properties>
<project.build.sourceEncoding>US-ASCII</project.build.sourceEncoding>
</properties>
实际上会使构建失败,因为 US-ASCII
是 7 位并且会导致非法字符错误。 UTF-8
不会发生同样的情况,它使用 8 位代替。