java Lombok 的 Maven 范围(编译与提供)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29385921/
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
Maven Scope for Lombok (Compile vs. Provided)
提问by mkraemerx
I recently found out that the lombok.jar ends up in our final artifact, which shouldn't be necessary. In my understanding lombok is compile-time only.
我最近发现 lombok.jar 最终出现在我们的最终工件中,这应该是不必要的。在我的理解 lombok 只是编译时。
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.14.4</version>
</dependency>
But when I set it to scope provided, I get strange behaviour in unit tests. They crash with ClassNotFoundExceptions then when trying to resolve
但是当我将它设置为提供的范围时,我在单元测试中得到了奇怪的行为。当他们尝试解决时,他们会因 ClassNotFoundExceptions 而崩溃
java.lang.NoClassDefFoundError: com/svv/esp/serviceimpl/dataimport/common/validation/LongValidator
Which maven scope is in general used for lombok?
龙目岛通常使用哪个 Maven 范围?
I'm using Oracle JDK build 1.8.0_25-b17 on MacOSX 10.9
我在 MacOSX 10.9 上使用 Oracle JDK build 1.8.0_25-b17
回答by agentgonzo
Lombok should be used at the provided
scope (see the official docs).
应该在provided
范围内使用 Lombok (请参阅官方文档)。
The reason (as has been stated in the comments) is that lombok is a compile-time-only tool. That is, it is not needed at runtime at all. By making the scope provided
, you make the lombok libraries available to the compiler but it is nota dependency of your compiled jar. As such, your final jar will not depend on Lombok and it does not need to be included in any deployment, which reduces the dependencies and size of your deployables.
原因(如评论中所述)是 lombok 是仅编译时工具。也就是说,它并不需要在运行时在所有。通过设置 scope provided
,您可以使 lombok 库可供编译器使用,但它不是您编译的 jar 的依赖项。因此,您的最终 jar 将不依赖于 Lombok,并且不需要包含在任何部署中,这减少了可部署的依赖项和大小。
回答by EmirCalabuch
Usually compile
. provided
is for jars that are usually shipped with the application server that will host the application. If you don't want the jar in the final application, it is maybe best to use the maven plugin rather than the jar directly: http://awhitford.github.io/lombok.maven/lombok-maven-plugin/index.html
通常compile
。provided
用于通常与将托管应用程序的应用程序服务器一起提供的 jar。如果您不希望在最终应用程序中使用 jar,最好使用 maven 插件而不是直接使用 jar:http: //awhitford.github.io/lombok.maven/lombok-maven-plugin/index。 html
回答by Jin Kwon
One can work with compile
and true
for <optional/>
.
可以与compile
和 一起true
工作<optional/>
。
<scope>compile</scope>
<optional>true</optional>
See Maven – Optional Dependencies and Dependency Exclusions.