Skip to content

Document on how to add the jspecify dependency: Maven and Gradle #503

Closed

Description

@sdavids

Please add a section to the README similar to the one at @API Guardian.

You might also want to add it to either Start Here and/or Nullness User Guide.

Maven

<dependency>
  <groupId>org.jspecify</groupId>
  <artifactId>jspecify</artifactId>
  <version>0.3.0</version>
</dependency>

or

<dependency>
  <groupId>org.jspecify</groupId>
  <artifactId>jspecify</artifactId>
  <version>0.3.0</version>
  <scope>provided</scope>
</dependency>

Related: #389

Gradle

The Java Library plugin configurations

dependencies {
  api("org.jspecify:jspecify:0.3.0")
}

or

dependencies {
  implementation("org.jspecify:jspecify:0.3.0")
}

or

dependencies {
  compileOnlyApi("org.jspecify:jspecify:0.3.0")
}

or

dependencies {
  compileOnly("org.jspecify:jspecify:0.3.0")
}

If it is not compileOnlyApi you might want to add a link to an appropriate FAQ entry.

Activity

  1. tbroyer commented on Mar 29, 2024

    @tbroyer

    Fwiw, because JSpecify can be used at runtime, I'd favor api in Gradle (and not-provided, not-optional in Maven). Those who don't use the annotations at runtime, and don't want to ship the JSpecify JAR in their application can safely exclude it from their deliverables, but the rule should be that it's "included by default".
    (it is an anti-pattern to mark annotation dependencies as compileOnly or compileOnlyApi, it depends on the annotation retention: source → compileOnly, class → compileOnlyApi, runtime → api; in Maven: source → provided/optional is the closest you can do, class or runtime → compile)

  2. GregDThomas commented on Mar 29, 2024

    @GregDThomas

    I'd favor api in Gradle

    For libraries, yes. For applications, I would think compileOnly would be fine, no?

  3. tbroyer commented on Mar 29, 2024

    @tbroyer

    If you're using the java (or application) plugin in Gradle, rather than java-library, for an application, then you don't have api and compileOnlyApi, and you only have to choose between implementation and compileOnly, which is what I hinted above: if you don't use the annotations at runtime, and don't want to ship the JSpecify JAR in your application, you can safely use compileOnly, otherwise use implementation.

  4. added
    documentation
    on Jun 11, 2024
  5. self-assigned this
    on Jun 27, 2024
  6. bowbahdoe commented on Jul 11, 2024

    @bowbahdoe

    My personal opinion at this point is that jspecify should be declared as a normal library, not provided. If its provided IDEs get confused when showing sources.

    For situations that want to avoid dependencies altogether, that generally implies a non-managed context where someone just downloads a jar. That is still viable so long as org.jspecify is marked requires static when libraries have a module-info.

  7. agentgt commented on Jul 11, 2024

    @agentgt

    I'm with @bowbahdoe on this. JSpecify unlike Checkerframework and Eclipse null analysis does not really specify if checking is static only.

    That is it is not unreasonable for applications to use reflection to check if a field is nullable or not in a similar vain to checking if a parameter/field/return is Optional. That is there could be runtime based soft implementations of null checking especially given that RETENTION is RUNTIME.

    Eclipse annotations are retention CLASS so they can largely be provided (and indeed is preferred when dealing with jlink as it still lacks a module-info).

  8. agentgt commented on Jul 12, 2024

    @agentgt

    I didn't have time to elaborate yesterday but JSpecify in terms of Maven should be

    <dependency>
      <groupId>org.jspecify</groupId>
      <artifactId>jspecify</artifactId>
      <version>0.3.0</version>
      <scope>compile</scope>
    </dependency>

    And here is the controversial part if you are library writer if any part of your public API exposes @Nullable you technically should do:

    requires transitive org.jspecify;

    The reason is imagine if JSpecify was some sort of container analogous to Optional that I will call Opt.

    package exported.pack;
    
    public interface SomeClassInExportedPackage {
       public org.jspecify.annotation.Opt<Image> findImage(UUID id);
    }

    The true analog of course is:

    package exported.pack;
    
    public interface SomeClassInExportedPackage {
       public @org.jspecify.annotation.Nullable Image findImage(UUID id);
    }

    My point is in terms of API exporting they are both requires transitive as the types of jspecify are being exposed. In fact the Eclipse compiler will actually complain if the annotation is on a generic if it is not exposed (this is probably a bug in ECJ but there is a solid chance that javac could omit warnings in the future!).

    Thus the correct module-info.java is:

    module com.mylib {
      exports exported.pack;
      requires transitive java.base; // this is implicit
      requires transitive org.jspecify;
    }

    Maven in terms of Maven project and not poms being published from some other build system has no analog to bare requires. That is <scope>compile</scope> is actually requires transitive. That is requires would work with <scope>runtime</scope> and indeed gradle can produce such pom but with Maven your library that uses JSpecify will not compile. In theory Maven 4 will fix this.

    Consequently and why I added that java.base reference is that JSpecify should be seen as something that if successful like reactive-streams would be put in java.base and therefore would become requires transitive anyway.

  9. netdpb commented on Jul 16, 2024

    @netdpb
    Collaborator

    We've added this documentation on the docs-1.0 branch, which will be merged soon.

  10. nbrugger-tgm commented on Jul 22, 2024

    @nbrugger-tgm

    I have a hard time understanding why you would suggest including jspecify as api in gradle. As far as i know api should only be used when users of the library transitively require the dependency at compile time to use yours (for example when you expose guava specific collections you should use api for guava because guava is part of the public api of your lib), but your nulless checks, annotations & tools are not interesting to your consumers. Important for them is seeing the annotations on the public methods/fields/classes which they would even if you use implementation. Or is there a good reason i haven't seen/realised to use api?

  11. cpovirk commented on Jul 22, 2024

    @cpovirk
    Collaborator

    My understanding is that the annotations would not be on the classpath for your users if you used implementation. I guess this would not be too hard for me to confirm, and I'll try to get around to it someday.

    Even with that said: Tools can sometimes operate correctly even if the annotations aren't on the classpath. (References to the annotations would still appear in the class files for the library; tools just wouldn't necessarily be able to find the class files for the annotations.) But that's not always the case, and even if they do operate correctly, they may generate warnings. We do need to write more up here. At the moment, the best single reference I have is this section of the Guava wiki.

  12. GregDThomas commented on Jul 22, 2024

    @GregDThomas

    My understanding is that the annotations would not be on the classpath for your users if you used implementation.

    implementation means it's on the runtime classpath, but not the compile time classpath.
    api means it's on both the runtime and compile time classpath.

  13. cpovirk commented on Jul 22, 2024

    @cpovirk
    Collaborator

    Oops, yes, sorry. I am mixing up the Gradle configuration with the less flexible Maven configuration :) Thanks for the correction.

  14. sdavids commented on Jul 22, 2024

    @sdavids
    Author

    https://docs.gradle.org/current/userguide/java_library_plugin.html#sec:java_library_configurations_graph

    api

    This is where you declare dependencies which are transitively exported to consumers, for compile time and runtime.

    implementation

    This is where you declare dependencies which are purely internal and not meant to be exposed to consumers (they are still exposed to consumers at runtime).

    compileOnlyApi

    This is where you declare dependencies which are required at compile time by your module and consumers, but not at runtime. This typically includes dependencies which are shaded when found at runtime.

  15. agentgt commented on Jul 22, 2024

    @agentgt

    While you can get away with implementation and module-info.java requires (and requires static) I don't think it is a good idea.

    Important for them is seeing the annotations on the public methods/fields/classes which they would even if you use implementation. Or is there a good reason i haven't seen/realised to use api?

    I'll give three reasons why:

    1. Unless you do not annotate anything public they are exposed. TYPE_USE runtime annotations are an extension of the type. If you return or accept something annotated as Nullable it should be largely considered the same as returning say Guava's Optional and which case is then api (or compile in Maven or requires transitive in module-info).
    2. The build systems and JPMS vary greatly across the board on scope (transitive visibility) but one thing they can all agree on is requires transitive aka api aka compile.
    3. I think / hope in the long run that this library will largely become successful like reactive-streams or slf4j and would be included in java.base. Reactive Streams and SLF4J are API with specifications with the expectation that you will probably be exposing their types (perhaps less for slf4j). Reactive Streams is a great analog because of the coloring of reactive code the types need to be transitive. Ditto for collection libs (and is why Guava version pain exists) and I argue ditto for JSpecify.
  16. cpovirk commented on Aug 20, 2024

    @cpovirk
    Collaborator

    I'll add to the above that implementation doesn't quite fully work even for usages in private members. While I wouldn't go so far as to run around telling people to never use it, I see this as chipping further away at the small number of cases in which implementation could be advisable.

  17. pivovarit commented on Dec 15, 2025

    @pivovarit

    We've added this documentation on the docs-1.0 branch, which will be merged soon.

    I think you're still missing JPMS guidelines - my current understanding is that modular libraries should use requires transitive org.jspecify and not requires transitive static org.jspecify, but one needs to go through a couple of lengthy issues to figure that out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

documentation

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions