【问题标题】:In Eclipse, is it possible to set annotation processing and factory path by default?在Eclipse中,是否可以默认设置注释处理和工厂路径?
【发布时间】:2018-04-07 11:05:57
【问题描述】:
目前对于每个在 Eclipse 中使用 mapstruct 的项目我都必须去:
配置构建路径 > Java 编译器 > 注释处理 > 工厂路径
并检查“使用项目特定设置”并配置工厂路径以每次手动使用mapstruct处理器jar。
短语“使用项目特定设置”暗示某处的全局设置,但我无法在首选项下找到类似的内容。
有什么地方可以配置注释处理的默认行为吗?
【问题讨论】:
标签:
java
eclipse
annotation-processing
mapstruct
【解决方案1】:
查看Eclipse 对 MapStruct 的支持。
您需要将m2e_apt 添加到您的属性中。
<properties>
<!-- automatically run annotation processors within the incremental compilation -->
<m2e.apt.activation>jdt_apt</m2e.apt.activation>
</properties>
还要确保您已正确设置 maven-compiler。
我们建议使用 maven-compiler-plugin 的 annotationProcessorPaths 选项(使用它,不会在您的编译路径上泄漏 mapstruct 处理器)。
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.6</source> <!-- or higher, depending on your project -->
<target>1.6</target> <!-- or higher, depending on your project -->
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
【解决方案2】:
我遵循了 Filip 的建议,但在将项目导入 Eclipse 工作区后,我无法让 Eclipse 添加必要的类路径。我在 pom.xml 中添加了 build-helper-maven-plugin 以帮助 Eclipse 添加所需的类路径:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/generated-sources/annotations/</source>
</sources>
</configuration>
</execution>
</executions>
这导致了 .classpath 文件中所需的类路径条目:
<classpathentry kind="src" output="target/classes" path="target/generated-sources/annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>