#how to generate and use java files in generated sources properly

13 messages · Page 1 of 1 (latest)

bleak crow
#

i have this code snipped

// Write to target/generated-sources/com/asdru/oopack/Version.java
        JavaFile javaFile = JavaFile.builder(packageName, enumType)
                .build();
        Path path = Paths.get("target/generated-sources");
        javaFile.writeTo(path);

that creates a version enum at the mentioned path. however my main project doesnt seem to find it (and neither does the github action i have setup)

rare tapirBOT
#

This post has been reserved for your question.

Hey @bleak crow! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

vague herald
#

You shouldn't do it this way.
Once your application is packaged in a JAR or similar, there will be no generated-sources or target folder

#

these folders only exist while you are developing the application

#

if you want to generate code at compile-time, take a look at annotation processors

#

and if you need to generate code at runtime, you could in principle use class loading to generate it but then you couldn't call it without reflection

#

Also note this other problem with your current approach: If you just write files to generated sources, you would need to recompile your application) (which may delete the generated sources directory) to use these classes

rare tapirBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping.
Warning: abusing this will result in moderative actions taken against you.

bleak crow
#

Oh ok thanks

rare tapirBOT
# bleak crow Oh ok thanks

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.

bleak crow
#

It takes a while to run since its fetching data from the web, thats why i separated it into its own main function

vague herald
#

There are two situations where you are allowed to modify the contents of the generated-sources folder:

  • in an annotation processor
  • in certain phases during the Maven build