Generate docs with picocli when using Guice and lombok

405 views Asked by At

I have created a cli using picocli(4.5.2), lombok and guice. All of my commands classes has private final fields(services) and they are initialized with lombok and guice. For example:

@Command(name = "example", helpCommand = true,
    description = "example controller")
@RequiredArgsConstructor(onConstructor = @__({@Inject}))
public class ExampleCommand implements Callable<Integer> {
    private final MyFacade myFacade;

    @Setter
    @ParentCommand
    private ParentCommandHere parentCommandHere;
    
        @Override
    public Integer call() {
        // do something
        return 0;
    }
}

Also I have GuiceFactory as suggested in the picocli docs.

When I try to use the ManPageGenerator in order to generate docs( using the following docs )

I see this errors:

picocli.CommandLine$InitializationException: Cannot instantiate path.to.my.command.ExampleCommand : the class has no constructor
   at picocli.CommandLine$DefaultFactory.create(CommandLine.java:5199)
    at picocli.CommandLine$Model$CommandUserObject.getInstance(CommandLine.java:11160)
    at picocli.CommandLine$Model$CommandSpec.userObject(CommandLine.java:5928)
    at picocli.codegen.docgen.manpage.ManPageGenerator.traceAllSpecs(ManPageGenerator.java:312)
    at picocli.codegen.docgen.manpage.ManPageGenerator.generateManPage(ManPageGenerator.java:285)
    at picocli.codegen.docgen.manpage.ManPageGenerator.generateManPage(ManPageGenerator.java:300)
    at picocli.codegen.docgen.manpage.ManPageGenerator.generateManPage(ManPageGenerator.java:300)
    at picocli.codegen.docgen.manpage.ManPageGenerator$App.call(ManPageGenerator.java:215)
    at picocli.codegen.docgen.manpage.ManPageGenerator$App.call(ManPageGenerator.java:166)
    at picocli.CommandLine.executeUserObject(CommandLine.java:1933)
    at picocli.CommandLine.access$1200(CommandLine.java:145)
    at picocli.CommandLine$RunLast.executeUserObjectOfLastSubcommandWithSameParent(CommandLine.java:2332)
    at picocli.CommandLine$RunLast.handle(CommandLine.java:2326)
    at picocli.CommandLine$RunLast.handle(CommandLine.java:2291)
    at picocli.CommandLine$AbstractParseResultHandler.execute(CommandLine.java:2159)
    at picocli.CommandLine.execute(CommandLine.java:2058)
    at picocli.codegen.docgen.manpage.ManPageGenerator.main(ManPageGenerator.java:239)
Caused by: java.lang.NoSuchMethodException: path.to.my.command.ExampleCommand.<init>()
    at java.lang.Class.getConstructor0(Class.java:3082)
    at java.lang.Class.getDeclaredConstructor(Class.java:2178)
    at picocli.CommandLine$DefaultFactory.create(CommandLine.java:5174)
    at picocli.CommandLine$DefaultFactory.create(CommandLine.java:5197)

Is there a way to fix this issue?

1

There are 1 answers

0
Remko Popma On BEST ANSWER

To get Guice dependency injection to work with picocli, your application likely has a custom factory. This factory is necessary when running the application, but it is also necessary when using the ManPageGenerator to generate documentation.

The ManPageGenerator application has a -c or --factory=<factoryClass> option where you can specify the class name of this custom factory. Add this to the arguments when invoking the javaExec task in the build script.