javapoet如何生成注释并使用lombok?

时间:2021-07-08 16:29:35

标签: java lombok javapoet

当我使用 javapoet 时,有没有办法(和任何意义)使用 Lombok?
示例如下:

TypeSpec typeSpec = TypeSpec
        .classBuilder("MyDtoWithLombok")
        .addModifiers(Modifier.PUBLIC)
        //.addAnnotation(NoArgsConstructor.class)
        //.addAnnotation(AllArgsConstructor.class)
        //.addAnnotation(Data.class)
        //.addAnnotation(Builder.class)
        .addField(...)
        .build();

当我尝试添加 Lombok 注释时 (像那样 -> Data.class”) 我收到以下错误:

Exception in thread "main" java.lang.NoClassDefFoundError: lombok/NoArgsConstructor
...
Caused by: java.lang.ClassNotFoundException: lombok.NoArgsConstructor
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    ... 3 more

1 个答案:

答案 0 :(得分:2)

可能是因为通常为 Lombok 提供了提供的作用域 - 您通常不希望在类路径中使用它,因为它应该在编译时被处理和删除。

from http.client import responses
import httplib2
from bs4 import BeautifulSoup, SoupStrainer

#url =  'https://10minuteschool.com/skills'

urls = ['https://10minuteschool.com/skills','https://10minuteschool.com/skills','https://10minuteschool.com/skills']

http = httplib2.Http()
links = []

for url in urls:
    responses, content = http.request(url)    

    for link in BeautifulSoup(content,features="lxml").find_all('a', href=True, target=True, rel=True,):
        links.append(link['href'])

for link in links:
    print(link)

删除 <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.20</version> <scope>provided</scope> </dependency> 行,或将其更改为 <scope>provided</scope>。两者是等价的,因为 compile 是默认值。

我个人会明确使用 compile,甚至可能添加注释解释您为什么不使用 compile,因为不经意的读者可能会认为这是一个错误,并可能尝试将其更改为再次provided