module reads package javax.annotation from both jsr305 and java.annotation

391 Views Asked by At

I go the error message:

module reads package javax.annotation from both jsr305 and java.annotation

In my build.gradle I used these dependencies:`

implementation "javax.annotation:jsr305:1.0"                  //  for @javax.annotation.Nonnull & Nullable
implementation "javax.annotation:javax.annotation-api:1.3.2"  //  for @javax.annotation.Generated`

In my OpenAPI generated source these annotations are used:

@javax.annotation.Nonnull & Nullable`
@javax.annotation.Generated`

I tried to use an exclude but it didn't work:

implementation ('javax.annotation:jsr305:1.0') {  
  exclude group:"javax.annotation", module: "javax.annotation-api" 
}
1

There are 1 best solutions below

0
David Miller On

Solution

Move from javax.annotation to jakarta.annotation in source code:

@jakarta.annotation.Generated
@jakarta.annotation.Nonnull
@jakarta.annotation.Nullable

and in my build.gradle include this dependency:

dependencies {
 implementation 'one.gfw:jakarta.annotation-api:2.1.1'
}

And in my module-info.java I included:

requires jakarta.annotation;

n.b. The other dependency I found jakarta.annotation:jakarta.annotation-api:1.3.5 didn't have all the annotations I wanted, only @javax.annotation.Generated and not Nonnull & Nullable, and so I used one.gfw:jakarta.annotation-api:2.1.1


As an aside, my source code was generated by the OpenAPI generator and I included the option useJakartaEe: 'true' in the generator's config to generate "jakarta.annotation" in place of "javax.annotation". In my OpenAPI generator's project - separate from the above project - here's my config in its build.gradle:

openApiGenerate {
 generatorName  = "java"
 inputSpec      = "${rootDir}/specs/${project.specName}"
 outputDir      = "${buildDir}/${project.apiName}"
 apiPackage     = "${project.packageName}.controllers"
 modelPackage   = "${project.packageName}.models"
 configOptions.set([
  dateLibrary: "java8",
  useJakartaEe: 'true'
])
}

Hope this helps.