简体   繁体   English

在 Maven 构建中,如何配置 Nullaway 以排除测试类?

[英]In a Maven build how do I configure Nullaway to exclude test classes?

In a Maven build how do I configure Nullaway to exclude test classes?在 Maven 构建中,如何配置 Nullaway 以排除测试类? Right now it's firing on code that specifically checks that NullPointerExceptions are thrown in the right places.现在它正在触发专门检查 NullPointerExceptions 是否在正确位置抛出的代码。

The code to be excluded lives in the customary src/test/java directory but in the same packages as the model code.要排除的代码位于惯用的 src/test/java 目录中,但与模型代码位于相同的包中。

You can exclude the test sources path from Errorprone checks (including Nullaway) like so:您可以从 Errorprone 检查(包括 Nullaway)中排除测试源路径,如下所示:

<compilerArgs>
  <arg>-Xep:NullAway:ERROR</arg>
  <arg>-XepOpt:NullAway:AnnotatedPackages=[...]</arg>
  <arg>-XepExcludedPaths:.*/src/test/java/.*</arg>
</compilerArgs>

NullAway is an Error Prone plugin. NullAway 是一个容易出错的插件。 Error Prone works with the help of its own compiler (Maven compiler id javac-with-errorprone ). Error Prone 在它自己的编译器(Maven 编译器 ID javac-with-errorprone )的帮助下工作。 I see two ways you can avoid applying Error Prone/NullAway for test classes:我看到两种方法可以避免将 Error Prone/NullAway 应用于测试类:

  1. If all relevant classes contain the @Test annotation then you can use the -XepOpt:NullAway:ExcludedClassAnnotations=depends.on.junit.version.Test option of the NullAway Maven plugin .如果所有相关类都包含@Test注释,那么您可以使用 NullAway Maven 插件-XepOpt:NullAway:ExcludedClassAnnotations=depends.on.junit.version.Test选项。

  2. Try to disable Error Prone by using the standard compiler when compiling Test classes by providing the standard compiler id ( <compilerId>javac</compilerId> ) in the maven-compiler-plugin configuration for the goal testCompile .通过在目标testCompile的 maven-compiler-plugin 配置中提供标准编译器 ID ( <compilerId>javac</compilerId> ),在编译测试类时尝试使用标准编译器禁用容易出错。 See the accepted answer here for an example of how to provide a configuration for the test compilation.有关如何为测试编译提供配置的示例,请参阅此处接受的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM