openapi-tools / swagger-maven-plugin

Maven plugin to activate the Swagger Core library to generate OpenAPI documentation.
MIT License
70 stars 45 forks source link

Some project dependencies are missing when scanning the project #25

Closed tosix1988 closed 5 years ago

tosix1988 commented 5 years ago

When the swagger-maven-plugin scans the project for swagger resources, the classloader it uses to load classes is missing the scanned project's dependencies, and thus may fail with ClassNotFoundException or similar. This can be worked around by explicitly listing all dependencies of the project in plugin's dependencies section, but for large projects there can be many dependencies, so adding all required ones may be tedious, and the plugin should handle this automatically if possible.

I have put up a simple project that demonstrates this: swagger-maven-project-with-dependencies.zip

I think this can be solved by not adding just project.getBuild().getOutputDirectory() to the URLClassLoader, but also the resources provided by project.getCompileClasspathElements() and project.getRuntimeClasspathElements().

langecode commented 5 years ago

Hmm... Been a while since I looked into this but the Mojo is actually declared as @Mojo(name = "generate", defaultPhase = LifecyclePhase.PREPARE_PACKAGE, requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME) I would have thought the requiresDependencyResolution also included classpath but from your observation this seems wrong. I do think I previously did an example where endpoints came from submodules but it may have been broken along the way.

I will look into this - but I agree that these should be included by default.

tosix1988 commented 5 years ago

I think that requiresDependencyResolution just guarantees that if you will request the classpath artifacts during the MOJO execution, Maven will resolve the artifacts in respective scopes before the MOJO executes (so that they are already resolved when you ask for them), otherwise various classpath resource methods MavenProject provides might throw DependencyResolutionRequiredException.

Btw. I have created a pull request that attempts to fix this issue (it fixed the issue on my test project, I hope it would work in general).