Java 如何使用 maven-compiler-plugin 配置 Lombok?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42257379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to configure Lombok with maven-compiler-plugin?
提问by O.Zaiats
I have a root module and submodule in maven in the project. I am trying to use Lombok. I have added
我在项目的 maven 中有一个根模块和子模块。我正在尝试使用 Lombok。我已经添加了
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.12</version>
<scope>provided</scope>
</dependency>
to root pom.xml. In submodule I have a class with Lombok annotations. When I am trying to build the project I get a lot of
根 pom.xml。在子模块中,我有一个带有 Lombok 注释的类。当我尝试构建项目时,我得到了很多
cannot find symbol
找不到标志
where I am trying to call getters and setters.
我试图调用 getter 和 setter 的地方。
I have tried to use lombok-maven-pluginwith same version (1.16.12) in root pom and in the sub pom as well with delombok and moving my annotated class to src/main/lombok, I have looked through almost all questions in SO, try all the variants, but not succeed.
我曾尝试在 root pom 和 sub pom 中使用相同版本 (1.16.12) 的 lombok -maven-plugin以及 delombok 并将我的带注释的类移动到 src/main/lombok,我已经查看了几乎所有的问题所以,尝试所有的变体,但没有成功。
I am using Maven 3, Java 8, maven-compiler-plugin with 3.6.1 version.
我正在使用 3.6.1 版本的 Maven 3、Java 8、maven-compiler-plugin。
How should I configure the project to be able to use lombok? Or maybe I am doing smth wrong.
我应该如何配置项目才能使用 lombok?或者也许我做错了。
采纳答案by O.Zaiats
I was using Java 8 and @Getter(onMethod = @__({@NoSerialization}))
and @Getter(onMethod = @__({@Translation(messageKey = "translation.key")}))
onX annotations. And I get duplicate element '<any?>' in annotation @<any?>.
in error output. Looks like guys from Lombok have such issue with Java 8 for a long time link to issue on github. Lombok does not handle annotations with parameters like messageKey
in annotation above. it works only with annotations without parameters and annotations with only value
parameter (when you don't write the name of parameter).
我使用的是 Java 8@Getter(onMethod = @__({@NoSerialization}))
和@Getter(onMethod = @__({@Translation(messageKey = "translation.key")}))
onX 注释。我进入duplicate element '<any?>' in annotation @<any?>.
错误输出。看起来龙目岛的人在很长一段时间内都对 Java 8 有这样的问题,链接到 github。Lombok 不会像messageKey
上面的注解那样处理带有参数的注解。它仅适用于没有参数的注释和只有value
参数的注释(当您不写参数名称时)。
回答by Dan N
I'm not sure what the difference is between lombok and lombok-maven-plugin, but my projects are configured with this dependency:
我不确定 lombok 和 lombok-maven-plugin 之间有什么区别,但我的项目配置了这种依赖项:
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-maven-plugin</artifactId>
<version>1.16.12.0</version>
</dependency>
I haven't experimented with root and submodule poms yet, as my projects all tend to be rather isolated from each other. Not sure if that could be causing an issue for you.
我还没有尝试过 root 和 submodule poms,因为我的项目都倾向于彼此隔离。不确定这是否会给您带来问题。
If you are using Eclipse, have you run the lombok.jar file and pointed it to your eclipse.exe file? it needs to modify the .exe in order for Eclipse to know that those getters and setters are coming, so that Eclipse doesn't complain during development.
如果您使用的是 Eclipse,您是否运行了 lombok.jar 文件并将其指向您的 eclipse.exe 文件?它需要修改 .exe 以便 Eclipse 知道那些 getter 和 setter 即将到来,以便 Eclipse 在开发过程中不会抱怨。
Edit:I'm using maven-compiler-plugin:
编辑:我正在使用 maven-compiler-plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
回答by Rafael Lemes
use: <scope>provided</scope> in pom.xml
like that:
<pre> <code>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
<scope>provided</scope>
</dependency>
</dependencies>
</code>
</pre>
回答by Ore
This is not a direct answer to the question which seems to be solved but acts as reference for future searchers:
这不是对似乎已解决的问题的直接回答,而是作为未来搜索者的参考:
If you're using Dagger (or something else) to process your annotations like
如果您使用 Dagger(或其他东西)来处理您的注释,例如
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>com.google.dagger</groupId>
<artifactId>dagger-compiler</artifactId>
<version>2.15</version>
</path>
</annotationProcessorPaths>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
....
</plugins>
</build>
You have to add lombok as path like
你必须添加 lombok 作为路径
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>com.google.dagger</groupId>
<artifactId>dagger-compiler</artifactId>
<version>2.15</version>
</path>
<!-- SOLUTION -->
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
</path>
</annotationProcessorPaths>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
....
</plugins>
</build>
You still have to list lombok as provided dependency tho.
您仍然必须将 lombok 列为提供的依赖项。
回答by Edgar Asatryan
In case of anyone using JDK 11
如果有人使用 JDK 11
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
</annotationProcessorPaths>
<compilerArgs>
<arg>-sourcepath</arg>
<arg>${project.basedir}/src/main/java${path.separator}${project.basedir}/target/generated-sources/annotations${path.separator}/</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
回答by Karthik Cherala
Maven Groovy and Java + Lombok
The solution on this stack overflow answer worked for me. I missed adding the javaAgentClass earlier
这个堆栈溢出答案的解决方案对我有用。我之前错过了添加 javaAgentClass