OpenAPITools / OpenAPITools/openapi-generator

[BUG] [JAVA] [MVN] maven plugin won't copy files in src/main/resources when addCompileSourceRoot is set

Open
#15,954 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Description

When java generator is used with a the maven plugin, and addCompileSourceRoot is set, the generated source directory (target/generated-sources/.../src/main/java) is added as a project source directory, but the generated resource directory (target/generated-sources/.../src/main/resources) is ignored.

This will make all files generated at target/.../src/main/java to get compiled and placed in the target/classes directory (the desired outcome), but the files that are generated at target/.../src/main/resources will get ignored, and won't be found in the target/classes file (which is not the desired outcome) - so the final JAR file will miss these files.

openapi-generator version

6.6.0 and 7.0.0-SNAPSHOT

Generation Details

java (or other jvm based generator), used by the maven plugin. Specifically reproduced with the java + native generator.

Steps to reproduce

Run the following pom file (mvn clean compile):
https://gist.github.com/yonatang/04150af8e0d4d3eef2c779173a441abb

The target/generated-sources/openapi/src/main/resources will have a file named openapi/openapi.yaml file, but that file will
be missing from the target/classes folder.
The expected behavior is to see in target/classes a file named openapi/openapi.yaml file.

Related issues/PRs

Couldn't fine any.

Suggest a fix

In this method at the Maven's CodeGenMojo class, the project.addResource() should be invoked as well.

The fix should be straight forward for most cases (when using the default source folder - src/main/java), but the main challenge is to handle cases where a non-default sourceFolder was defined - the java generator doesn't have a resourceFolder option and therefore it is unclear what is the actual resource folder. Notice that src/main/resource even seems to be hardcoded in some cases in JavaClientCodegen.

I think this issue calls out a flaw in the design about resources for java based plugins - you can configure the sources directory but can't configure the resources directory.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in modules/openapi-generator-maven-plugin/src/main/java/org/openapitools/codegen/plugin/CodeGenMojo.java at the referenced method, then inspect JavaClientCodegen.java around the hardcoded resource path. Reproduce with the linked POM and mvn clean compile; done means generated files under src/main/resources, including openapi/openapi.yaml, appear in target/classes for default and non-default sourceFolder cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
build-system
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.