如何在maven-compiler-plugin中使用多个注解处理器

huangapple 未分类评论45阅读模式
英文:

How to use multiple annotation processors with maven-compiler-plugin

问题

以下是翻译好的部分:

我有一个使用Spring Boot的项目,其中使用了Lombok和MapStruct,并且使用Maven作为构建工具。我需要在编译时处理注解,并将生成的源代码与最终的JAR包一起打包。构建成功了。然而,最终的JAR包缺少MapStruct的实现类。当我尝试启动Spring Boot应用程序时,出现以下错误:


应用程序启动失败


描述:

在 com.some_org.service.salesforce.object.processor.SalesforceObjectProcessor 中,字段 salesforceObjectMapper 需要一个类型为 com.some_org.service.salesforce.object.mapper.SalesforceObjectMapper 的 bean,但找不到该类型的 bean。

动作:

考虑在您的配置中定义一个类型为 com.some_org.service.salesforce.object.mapper.SalesforceObjectMapper 的 bean。

这是我的 maven-compiler-plugin 配置:

<plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.7.0</version>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
        <annotationProcessorPaths>
            <path>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.18.12</version>
            </path>
            <path>
                <groupId>org.mapstruct</groupId>
                <artifactId>mapstruct-processor</artifactId>
                <version>1.2.0.Final</version>
            </path>
        </annotationProcessorPaths>
        <compilerArgs>
            <compilerArg>-Amapstruct.suppressGeneratorTimestamp=true</compilerArg>
            <compilerArg>-Amapstruct.suppressGeneratorVersionInfoComment=true</compilerArg>
        </compilerArgs>
    </configuration>
</plugin>
英文:

I have a spring boot project that has lombok and mapstruct and uses maven as the build tool. I need the annotations to be processed at compile time with the resultant generated sources packaged with the final jar. The build is successful. However, the final jar is missing the mapstruct implementation class. The error when I try to start the spring boot application is:

> ***************************
> APPLICATION FAILED TO START
> ***************************
>
> Description:
>
> Field salesforceObjectMapper in
> com.some_org.service.salesforce.object.processor.SalesforceObjectProcessor
> required a bean of type
> 'com.some_org.service.salesforce.object.mapper.SalesforceObjectMapper'
> that could not be found.
>
>
> Action:
>
> Consider defining a bean of type
> 'com.some_org.service.salesforce.object.mapper.SalesforceObjectMapper'
> in your configuration.

Here is my maven-compiler-plugin setup:

&lt;plugin&gt;
    &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
    &lt;version&gt;3.7.0&lt;/version&gt;
    &lt;configuration&gt;
        &lt;source&gt;1.8&lt;/source&gt;
        &lt;target&gt;1.8&lt;/target&gt;
        &lt;annotationProcessorPaths&gt;
            &lt;path&gt;
                &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
                &lt;artifactId&gt;lombok&lt;/artifactId&gt;
                &lt;version&gt;1.18.12&lt;/version&gt;
            &lt;/path&gt;
            &lt;path&gt;
                &lt;groupId&gt;org.mapstruct&lt;/groupId&gt;
                &lt;artifactId&gt;mapstruct-processor&lt;/artifactId&gt;
                &lt;version&gt;1.2.0.Final&lt;/version&gt;
            &lt;/path&gt;
        &lt;/annotationProcessorPaths&gt;
        &lt;compilerArgs&gt;
            &lt;compilerArg&gt;-Amapstruct.suppressGeneratorTimestamp=true&lt;/compilerArg&gt;
            &lt;compilerArg&gt;-Amapstruct.suppressGeneratorVersionInfoComment=true&lt;/compilerArg&gt;
        &lt;/compilerArgs&gt;
    &lt;/configuration&gt;
&lt;/plugin&gt;

答案1

得分: 0

最终通过在编译阶段使用执行来覆盖默认的编译目标来解决了此问题,如下所示:

<plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.7.0</version>
    <executions>
        <execution>
            <id>Compile With Annotation Processing</id>
            <phase>compile</phase>
            <goals>
                <goal>compile</goal>
            </goals>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <annotationProcessorPaths>
                    <path>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                        <version>1.18.12</version>
                    </path>
                    <path>
                        <groupId>org.mapstruct</groupId>
                        <artifactId>mapstruct-processor</artifactId>
                        <version>1.2.0.Final</version>
                    </path>
                </annotationProcessorPaths>
                <compilerArgs>
                    <compilerArg>-Amapstruct.suppressGeneratorTimestamp=true</compilerArg>
                    <compilerArg>-Amapstruct.suppressGeneratorVersionInfoComment=true</compilerArg>
                </compilerArgs>
            </configuration>
        </execution>
    </executions>
</plugin>
英文:

Eventually resolved this by overriding the default compile goal during the compile phase using execution as shown below:

&lt;plugin&gt;
    &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
    &lt;version&gt;3.7.0&lt;/version&gt;
    &lt;executions&gt;
        &lt;execution&gt;
            &lt;id&gt;Compile With Annotation Processing&lt;/id&gt;
            &lt;phase&gt;compile&lt;/phase&gt;
            &lt;goals&gt;
                &lt;goal&gt;compile&lt;/goal&gt;
            &lt;/goals&gt;
            &lt;configuration&gt;
                &lt;source&gt;1.8&lt;/source&gt;
                &lt;target&gt;1.8&lt;/target&gt;
                &lt;annotationProcessorPaths&gt;
                    &lt;path&gt;
                        &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
                        &lt;artifactId&gt;lombok&lt;/artifactId&gt;
                        &lt;version&gt;1.18.12&lt;/version&gt;
                    &lt;/path&gt;
                    &lt;path&gt;
                        &lt;groupId&gt;org.mapstruct&lt;/groupId&gt;
                        &lt;artifactId&gt;mapstruct-processor&lt;/artifactId&gt;
                        &lt;version&gt;1.2.0.Final&lt;/version&gt;
                    &lt;/path&gt;
                &lt;/annotationProcessorPaths&gt;
                &lt;compilerArgs&gt;
                    &lt;compilerArg&gt;-Amapstruct.suppressGeneratorTimestamp=true&lt;/compilerArg&gt;
                    &lt;compilerArg&gt;-Amapstruct.suppressGeneratorVersionInfoComment=true&lt;/compilerArg&gt;
                &lt;/compilerArgs&gt;
            &lt;/configuration&gt;
        &lt;/execution&gt;
    &lt;/executions&gt;
&lt;/plugin&gt;

答案2

得分: 0

lombokmapstruct之间存在冲突时,你可以使用lombok-mapstruct-binding来解决这个问题(不需要将其放在execution标签中)。关键是将绑定库与lombokmapstruct的注解处理器配置放在同一个位置。在我这里表现得非常出色!

<build>
  <plugins>
    <plugin>
      ..
      <configuration>
        ..
        <annotationProcessorPaths>
          <path>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.20</version>
          </path>
          <path>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok-mapstruct-binding</artifactId>
            <version>0.2.0</version>
          </path>
          <path>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-processor</artifactId>
            <version>1.4.2.Final</version>
          </path>
        </annotationProcessorPaths>
      </configuration>
    </plugin>
  </plugins>
</build>
英文:

If your lombok and mapstruct don't play well with each other, you can use lombok-mapstruct-binding to resolve the conflict (You don't need to put it in the execution tag). The key is to include the binding artifact in the same place with the lombok and mapstruct annotation processors configuration. It works splendid in my case!

&lt;build&gt;
  &lt;plugins&gt;
    &lt;plugin&gt;
      ..
      &lt;configuration&gt;
        ..
        &lt;annotationProcessorPaths&gt;
        &lt;path&gt;
          &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
          &lt;artifactId&gt;lombok&lt;/artifactId&gt;
          &lt;version&gt;1.18.20&lt;/version&gt;
        &lt;/path&gt;
        &lt;path&gt;
          &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
          &lt;artifactId&gt;lombok-mapstruct-binding&lt;/artifactId&gt;
          &lt;version&gt;0.2.0&lt;/version&gt;
        &lt;/path&gt;
        &lt;path&gt;
          &lt;groupId&gt;org.mapstruct&lt;/groupId&gt;
          &lt;artifactId&gt;mapstruct-processor&lt;/artifactId&gt;
          &lt;version&gt;1.4.2.Final&lt;/version&gt;
        &lt;/path&gt;
      &lt;/annotationProcessorPaths&gt;
    &lt;/plugin&gt;
  &lt;plugins&gt;
&lt;build&gt;

huangapple
  • 本文由 发表于 2020年4月10日 22:59:15
  • 转载请务必保留本文链接:https://java.coder-hub.com/61143001.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定