Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to create Builders with java generator? #6659

Open
rafhuys-klarrio opened this issue Nov 25, 2024 · 8 comments
Open

How to create Builders with java generator? #6659

rafhuys-klarrio opened this issue Nov 25, 2024 · 8 comments

Comments

@rafhuys-klarrio
Copy link

rafhuys-klarrio commented Nov 25, 2024

I have a CRD from which I generate the Java Classes. I can add annotations to indicate the Builders of these classes, but is there a way to actually generate the Builder classes as well?

Concretely, I generate the following class based on Traefik Middleware CRD with ./java-gen/java-generator-cli-6.13.4.sh -add-extra-annotations -s traefik.io_middlewares.yaml -t .

package io.traefik.v1alpha1;

@io.fabric8.kubernetes.model.annotation.Version(value = "v1alpha1" , storage = true , served = true)
@io.fabric8.kubernetes.model.annotation.Group("traefik.io")
@io.fabric8.kubernetes.model.annotation.Singular("middleware")
@io.fabric8.kubernetes.model.annotation.Plural("middlewares")
@javax.annotation.processing.Generated("io.fabric8.java.generator.CRGeneratorRunner")
@lombok.ToString()
@lombok.EqualsAndHashCode(callSuper = true)
@io.sundr.builder.annotations.Buildable(editableEnabled = false, validationEnabled = false, generateBuilderPackage = false, builderPackage = "io.fabric8.kubernetes.api.builder", refs = {
    @io.sundr.builder.annotations.BuildableReference(io.fabric8.kubernetes.api.model.ObjectMeta.class),
    @io.sundr.builder.annotations.BuildableReference(io.fabric8.kubernetes.api.model.ObjectReference.class),
    @io.sundr.builder.annotations.BuildableReference(io.fabric8.kubernetes.api.model.LabelSelector.class),
    @io.sundr.builder.annotations.BuildableReference(io.fabric8.kubernetes.api.model.Container.class),
    @io.sundr.builder.annotations.BuildableReference(io.fabric8.kubernetes.api.model.EnvVar.class),
    @io.sundr.builder.annotations.BuildableReference(io.fabric8.kubernetes.api.model.ContainerPort.class),
    @io.sundr.builder.annotations.BuildableReference(io.fabric8.kubernetes.api.model.Volume.class),
    @io.sundr.builder.annotations.BuildableReference(io.fabric8.kubernetes.api.model.VolumeMount.class)
})
public class Middleware extends io.fabric8.kubernetes.client.CustomResource<io.traefik.v1alpha1.MiddlewareSpec, java.lang.Void> implements io.fabric8.kubernetes.api.model.Namespaced, io.fabric8.kubernetes.api.builder.Editable<MiddlewareBuilder> {

    @java.lang.Override
    public MiddlewareBuilder edit() {
        return new MiddlewareBuilder(this);
    }
}

Very nice, however there is no constructor for MiddlewareBuilder created. How can I generate one?

@manusa
Copy link
Member

manusa commented Nov 25, 2024

@rafhuys-klarrio
Copy link
Author

is this possible with the cli as well? or only maven?

@manusa
Copy link
Member

manusa commented Nov 25, 2024

the CLI won't generate the builders, it generates source-code that can be processed by the builder generators.
Where is the code generated by the CLI consumed?
Wherever that code is stored, then the project needs to annotate-process the generated code.

@rafhuys-klarrio
Copy link
Author

Thank you for the link you sent. But when I run https://github.com/marcnuri-demo/kubernetes-client/blob/master/java-generator/src/test/java/com/hashicorp/secrets/v1beta1/VaultConnectionTest.java I get

/~/kubernetes-client-java-generator/java-generator/src/test/java/com/hashicorp/secrets/v1beta1/VaultConnectionTest.java:50:21
java: cannot find symbol
  symbol:   class VaultConnectionBuilder
  location: class com.hashicorp.secrets.v1beta1.VaultConnectionTest

@rafhuys-klarrio
Copy link
Author

ok! didn't know about the annotate-process part! Tx

@manusa
Copy link
Member

manusa commented Nov 25, 2024

Thank you for the link you sent. But when I run https://github.com/marcnuri-demo/kubernetes-client/blob/master/java-generator/src/test/java/com/hashicorp/secrets/v1beta1/VaultConnectionTest.java I get

/~/kubernetes-client-java-generator/java-generator/src/test/java/com/hashicorp/secrets/v1beta1/VaultConnectionTest.java:50:21
java: cannot find symbol
  symbol:   class VaultConnectionBuilder
  location: class com.hashicorp.secrets.v1beta1.VaultConnectionTest

Not sure how you're running it, but should simply work by using Maven (mvn verify)

ok! didn't know about the annotate-process part! Tx

Yes, this is basically the important part. Your builders will be generated as generated-sources after the annotation processor processes them.

@rafhuys-klarrio
Copy link
Author

yeah tx for the pointer! Actually I'm writing an SBT plugin for the java-generator. The basic POJO generation is there, but since I don't use Maven ever it's a bit getting used to. So, if I'm following this correctly it's the sundr.io plugin that (should) kick in after the pojo is generated and then creates the Builder class based on the annotation of the base class. I'll have a look if I can manage that to work but this might become a rabbithole rather quickly. Thanks for your help!

@manusa
Copy link
Member

manusa commented Nov 25, 2024

So, if I'm following this correctly it's the sundr.io plugin that (should) kick in after the pojo is generated and then creates the Builder class based on the annotation of the base class.

That's correct.

I guess you can run the compilation of the classes to kick in the APT (javac -proc:only) and then use some other process to move the generated sources elsewhere.

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

No branches or pull requests

2 participants