NullAway icon indicating copy to clipboard operation
NullAway copied to clipboard

Doc: how to exclude tests in Maven build

Open elharo opened this issue 6 years ago • 9 comments

For gradle the docs have:

tasks.withType(JavaCompile) {
  // remove the if condition if you want to run NullAway on test code
  if (!name.toLowerCase().contains("test")) {
    options.compilerArgs += ["-Xep:NullAway:ERROR", "-XepOpt:NullAway:AnnotatedPackages=com.uber"]
  }
}

It's not immediately obvious how to do this in a maven project. Please expand the maven docs to demonstrate.

elharo avatar May 08 '18 14:05 elharo

If you want to skip tests, I believe that adding -DskipTests will bypass the Maven surefire plugin. I'm not sure about the "verify" phase, though.

More info:

  • https://maven.apache.org/surefire/maven-surefire-plugin/examples/skipping-tests.html
  • https://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html#skipTests

jeandersonbc avatar May 08 '18 15:05 jeandersonbc

The question is not how to skip running the tests. Rather it is how to keep NullAway from checking the tests for null problems.

elharo avatar May 08 '18 17:05 elharo

Oh, sorry for the misunderstanding.

jeandersonbc avatar May 08 '18 17:05 jeandersonbc

I'm not a Maven expert. @kageiit any ideas or thoughts on who would know this? Basically need a way to tweak the "main" javac args but not those for test code.

On Tue, May 8, 2018, 10:58 Jeanderson Barros Candido < [email protected]> wrote:

Oh, sorry for the misunderstood.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/uber/NullAway/issues/162#issuecomment-387489101, or mute the thread https://github.com/notifications/unsubscribe-auth/AALyUZReJbRtbMFiOVR0Gp_Q9OlkBbQtks5twdzagaJpZM4T2w9H .

msridhar avatar May 08 '18 23:05 msridhar

Seems like the maven compiler plugin allows configuration of main and test compiler arguments separately

http://maven.apache.org/plugins-archives/maven-compiler-plugin-2.5.1/compile-mojo.html#compilerArguments

http://maven.apache.org/plugins-archives/maven-compiler-plugin-2.5.1/testCompile-mojo.html#testCompilerArguments

kageiit avatar May 09 '18 02:05 kageiit

Thanks @kageiit. We actually need the compilerArgs parameter from more recent compiler plugin versions to pass strings as arguments:

https://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html#compilerArgs

https://maven.apache.org/plugins/maven-compiler-plugin/testCompile-mojo.html#compilerArgs

Our Maven example currently sets compilerArgs in the global configuration, which I think will apply to both the compile and testCompile phases. Does someone know of an example somewhere that shows how to set compilerArgs differently for these two phases? If someone gets me an example I can update the docs.

msridhar avatar May 09 '18 16:05 msridhar

There actually seems to be no need to configure it for the two phases separately. I've had success just excluding the test sources path:

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

cbruegg avatar May 25 '20 10:05 cbruegg

@cbruegg yes that will work if you are using the default filesystem layout. And it could be adapted if you're doing something else. I still suspect there is some configuration magic for passing different compiler args for test vs. main code, but we are not Maven experts and haven't figured it out.

msridhar avatar May 27 '20 01:05 msridhar

The snippet below just adds the compiler arguments for the main compilation step. Tests are thus excluded from the validation via NullAway.

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <executions>
        <execution>
          <!-- overwrite the default compile step, any other ID will result in an additional compile step -->
          <id>default-compile</id>
          <configuration>
            <compilerArgs>
              <arg>-XDcompilePolicy=simple</arg>
              <arg>
                -Xplugin:ErrorProne
                -Xep:NullAway:ERROR
                -XepOpt:NullAway:AnnotatedPackages=my.example.package
              </arg>
            </compilerArgs>
            <annotationProcessorPaths>
              <path>
                <groupId>com.google.errorprone</groupId>
                <artifactId>error_prone_core</artifactId>
                <version>2.9.0</version>
              </path>
              <path>
                <groupId>com.uber.nullaway</groupId>
                <artifactId>nullaway</artifactId>
                <version>0.9.2</version>
              </path>
            </annotationProcessorPaths>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

laukerta avatar Oct 20 '21 10:10 laukerta