简体   繁体   English

使用 Lombok 的 @Builder 注释时出现 Javadoc“找不到符号”错误

[英]Javadoc "cannot find symbol" error when using Lombok's @Builder annotation

I have a class looking as below:我有一个 class,如下所示:

@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: [错误] 无法在项目 foo 上执行目标 org.apache.maven.plugins:maven-javadoc-plugin:2.10.4:javadoc (default-cli):JavaDocs 报告生成中发生错误:
[ERROR] Exit code: 1 - /home/workspace/foo/src/main/java/com/foo/Foo.java:34: error: cannot find symbol [错误] 退出代码:1 - /home/workspace/foo/src/main/java/com/foo/Foo.java:34:错误:找不到符号
[ERROR] public static FooBuilder builder(String _param) [错误] public static FooBuilder builder(String _param)
[ERROR] ^ [错误] ^
[ERROR] symbol: class FooBuilder [错误] 符号:class FooBuilder
[ERROR] location: class Foo [错误] 位置:class Foo

In order to solve this issue, I have to use Lombok's delombok feature (cf : https://projectlombok.org/features/delombok ).为了解决这个问题,我必须使用 Lombok 的delombok功能(参见: https : delombok )。

lombok doesn't cover all tools. lombok 并未涵盖所有工具。 For example, lombok cannot plug into javadoc ... which run on java sources.例如,lombok 无法插入在 java 源代码上运行的 javadoc ...。 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. Delombok 仍然允许您将 lombok 与这些工具一起使用,方法是将您的 java 代码预处理为 java 代码,并且已经应用​​了所有 lombok 的转换。

I did this using Maven by adding the following plugins :我通过添加以下插件使用 Maven 做到了这一点:

<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>

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. Lombok 实际上能够填充一个部分定义的构建器类,因此您可以声明足够的构建器以使 Javadoc 满意,然后将其保留。 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.可能已经有一些技术可以做到这一点,但很高兴知道您可以手动改进构建器。

Update更新

If you use maven-javadoc-plugin 3.2.0+ you can configure it like this:如果你使用 maven-javadoc-plugin 3.2.0+ 你可以这样配置:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-javadoc-plugin</artifactId>
    <version>3.2.0</version>
    <configuration>
      <doclint>none</doclint>
    </configuration>
  </plugin>

The doclint configuration will make javadoc plugin not throw an error anymore. doclint配置将使 javadoc 插件不再抛出错误。 It will also disable the lint but if you are ok with this probably the best way to go instead of delombok.它也会禁用 lint,但如果您同意这可能是最好的方法,而不是 delombok。

If you use any CI tool to build and compile your project you can create a separated job to check for javadoc lint.如果您使用任何 CI 工具来构建和编译您的项目,您可以创建一个单独的作业来检查 javadoc lint。

For me disabling lint in the build is not a bad thing.对我来说,在构建中禁用 lint 并不是一件坏事。 Javadoc is important but shouldn't keep me from building my application just because I'm using Lombok. Javadoc 很重要,但不应该因为我使用 Lombok 而阻止我构建我的应用程序。

Another solution to this would be to not include the BuilderClass in your imports.另一种解决方案是不在您的导入中包含 BuilderClass。 Instead just import the parent class and change your declaration of the builder type to parentClass.builderClass .相反,只需导入父级 class 并将构建器类型的声明更改为parentClass.builderClass

@Getter
@RequiredArgsConstructor
@Builder
public class Foo {
    private final String param;
}
import com.Foo;
//import com.Foo.FooBuilder;

public class Bar {

    public Foo newFoo(String paramValue) {
        Foo.FooBuilder builder = Foo.builder();
        return builder.param(paramValue)
                  .build();
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM