22

I have a class looking as below :

@Data
@Builder
public class Foo {
    private String param;

    /** My custom builder.*/
    public static FooBuilder builder(String _param){
        return builder().param(_param);
    }
}

I get the following error :

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-javadoc-plugin:2.10.4:javadoc (default-cli) on project foo: An error has occurred in JavaDocs report generation:
[ERROR] Exit code: 1 - /home/workspace/foo/src/main/java/com/foo/Foo.java:34: error: cannot find symbol
[ERROR] public static FooBuilder builder(String _param)
[ERROR] ^
[ERROR] symbol: class FooBuilder
[ERROR] location: class Foo

Naman
  • 23,555
  • 22
  • 173
  • 290
yunandtidus
  • 3,136
  • 2
  • 24
  • 38

2 Answers2

17

In order to solve this issue, I have to use Lombok's delombok feature (cf : https://projectlombok.org/features/delombok).

lombok doesn't cover all tools. For example, lombok cannot plug into javadoc ... which run on java sources. Delombok still allows you to use lombok with these tools by preprocessing your java code into java code with all of lombok's transformations already applied.

I did this using Maven by adding the following plugins :

<plugin>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok-maven-plugin</artifactId>
    <version>1.18.0.0</version>
    <configuration>
        <sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
        <outputDirectory>${delombok.output}</outputDirectory>
        <addOutputDirectory>false</addOutputDirectory>
    </configuration>
    <executions>
        <execution>
            <phase>generate-sources</phase>
            <goals>
                <goal>delombok</goal>
            </goals>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-javadoc-plugin</artifactId>
    <version>2.9</version>
    <configuration>
        <sourcepath>${delombok.output}</sourcepath>
    </configuration>
</plugin>
yunandtidus
  • 3,136
  • 2
  • 24
  • 38
  • Is there no other way to process annotations such as `@Builder` with `javadoc-plugin`? Same as the compiler-plugin has ``? Using `delombok` sounds like an anti-pattern. – Naman Oct 17 '19 at 04:42
  • Yes it sounds, but I don't think there is an other way, even lombok documentation asserts it (cf. my answer), – yunandtidus Oct 17 '19 at 06:45
  • The default delombok.`outputDirectory` is `${project.build.directory}/generated-sources/delombok` so you only need to specify that in the maven-javadoc-plugin – osundblad Sep 18 '20 at 07:07
16

Lombok is actually capable of filling out a partially defined builder class, so you can declare enough of the builder to make Javadoc happy and leave it at that. No need to delombok.

The following worked for me in this situation:

@Data
@Builder
public class Foo {
    private String param;

    /** My custom builder.*/
    public static FooBuilder builder(String _param){
        return builder().param(_param);
    }

    public static class FooBuilder {}

}

Side note: that you can actually use this technique to add some customer builder methods, so it has perks. I like to overload builder methods when I have collections so I can items one at a time. There's probably already some technique that does that, but it's nice to know you can improve the builders manually.

David Blevins
  • 18,358
  • 3
  • 48
  • 65
  • 1
    THX! Works fine (JDK 8, Lombok 1.8.12, Mapstruct 1.3.1) and saves us from Delombok :-) – t0r0X May 11 '20 at 16:22