【问题标题】:Turn off @Generate annotation in MapStruct在 MapStruct 中关闭 @Generate 注解
【发布时间】:2021-03-10 07:03:26
【问题描述】:

我想在生成的(通过 mapstruct)类中禁用 @Generate 注释。

我试试 args -Amapstruct.suppressGeneratorTimestamp=true-Amapstruct.suppressGeneratorVersionInfoComment=true,但是还是不要关闭注解。

在mapstruct的源代码中,我找到了类GeneratedType,它在构造函数中声明了一个字段generatedTypeAvailable,这会影响模板GeneratedType.ftl

<#if !generatedTypeAvailable>/*</#if>
@Generated(
    value = "org.mapstruct.ap.MappingProcessor"<#if suppressGeneratorTimestamp == false>,
    date = "${.now?string("yyyy-MM-dd'T'HH:mm:ssZ")}"</#if><#if suppressGeneratorVersionComment == false>,
    comments = "version: ${versionInformation.mapStructVersion}, compiler: ${versionInformation.compiler}, environment: Java ${versionInformation.runtimeVersion} (${versionInformation.runtimeVendor})"</#if>
)<#if !generatedTypeAvailable>
*/</#if>

那么,如何将字段值generatedTypeAvailable设置为false

【问题讨论】:

    标签: java mapstruct


    【解决方案1】:

    目前无法禁用@Generated 注释的创建。为什么需要这样做?

    你可以做的是:

    • 使用-Amapstruct.suppressGeneratorTimerstamp=true抑制时间戳值
    • 使用-Amapstruct.suppressGeneratorVersionInfoComment抑制版本

    【讨论】:

    • 如果你想在你的测试覆盖中包含 mapstruct 实现,这将是有用的。虽然代码是生成的(因此通常不想对其进行测试),但在其下存在相当程度的业务逻辑,人们可能希望对其进行强制测试。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-19
    • 2022-07-17
    • 2012-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多