uber / NullAway

A tool to help eliminate NullPointerExceptions (NPEs) in your Java code with low build-time overhead
MIT License
3.62k stars 291 forks source link

Doc: how to exclude tests in Maven build #162

Open elharo opened 6 years ago

elharo commented 6 years ago

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.

jeandersonbc commented 6 years ago

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:

elharo commented 6 years ago

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

jeandersonbc commented 6 years ago

Oh, sorry for the misunderstanding.

msridhar commented 6 years ago

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 < notifications@github.com> 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 .

kageiit commented 6 years ago

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

msridhar commented 6 years ago

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.

cbruegg commented 4 years ago

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>
msridhar commented 4 years ago

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

laukerta commented 2 years ago

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>