gpt4 book ai didi

maven - 如何使用 Lombok 消除 Mavenized 项目 Intellij 中的重复类错误

转载 作者:行者123 更新时间:2023-12-01 16:15:39 26 4
gpt4 key购买 nike

我有一个 Maven 管理的 Lombok 项目,我使用 Intellij。构建后,由于 target/generated-sources/delombok 中生成的源,我总是在 Intellij 中遇到很多关于重复类的错误。我可以做些什么来消除这些错误吗?现在我只是删除目标文件夹,但这确实很烦人。

我在 Maven 中有标准配置,Lombok 源代码在 src/main/lombok 中:

       <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>

<plugin>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-maven-plugin</artifactId>
<version>1.16.8.0</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>delombok</goal>
</goals>
</execution>
</executions>
</plugin>

<profiles>
<profile>
<id>lombok-needs-tools-jar</id>
<activation>
<file>
<exists>${java.home}/../lib/tools.jar</exists>
</file>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-maven-plugin</artifactId>
<version>1.16.8.0</version>
<dependencies>
<dependency>
<groupId>sun.jdk</groupId>
<artifactId>tools</artifactId>
<version>1.8</version>
<scope>system</scope>
<systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</profile>
</profiles>

最佳答案

根据delombok goal documentation :默认输出目录是:

${project.build.directory}/generated-sources/delombok

我找到了 JetBrains 团队成员 comment声明:

IDEA automatically excludes the build 'target' folder, providing that there are no generated sources under it, otherwise it excludes all sub-folders but the generated.

If you have some generated code or build artifacts that you want being excluded, you may put it under the 'target' folder.

这意味着 /generated-sources默认情况下不排除目录,如果您打算排除某些文件,则应将它们放在父 /target 下目录而不是 /generated-sources 下.

要实现此目的,您应该配置插件并提供非默认 <outputDirectory> :

<plugin>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-maven-plugin</artifactId>
<version>1.16.18.0</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>delombok</goal>
</goals>
</execution>
</executions>
<configuration>
<sourceDirectory>src/main/java</sourceDirectory>
<outputDirectory>${project.build.directory}/delombok</outputDirectory>
<addOutputDirectory>false</addOutputDirectory>
<encoding>UTF-8</encoding>
</configuration>
</plugin>

这将阻止 IDEA 产生 Duplicate class found in (...)警告。

关于maven - 如何使用 Lombok 消除 Mavenized 项目 Intellij 中的重复类错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37801851/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com