从 Gradle 4 迁移到 5。如何让 mapstruct 1.20.final 使用它

Migrating from Gradle 4 to 5. How to get mapstruct 1.20.final working with it

我们使用 mapstruct 1.20.final 大约 1.5 年,其中包含各种 Gradle 版本 - 最新的 gradle 4.10.2。我们想切换到 Gradle 5.4.1,它适用于除 mapstruct 之外的所有内容。我们的工作设置不干净。因此决定重新开始。旧的工作设置是 example on Github and the now obsolete setup.

的混合形式

http://mapstruct.org/news/2013-07-08-using-mapstruct-with-gradle as a base. Have this strong feeling this is NOT compatible with Gradle 5. Release notes Gradle 5 states: Gradle will no longer automatically apply annotation processors that are on the compile classpath — use CompileOptions.annotationProcessorPath instead. Tried to do it as described in https://blog.gradle.org/incremental-compiler-avoidance#about-annotation-processors 重新开始。这适用于 4.10.2。使用 Gradle 5 这会导致以下错误: 任务“:eu.educator.rest:compileJava”执行失败。 无法通过 CompileOptions.compilerArgs 指定 -processorpath 或 --processor-path。请改用 CompileOptions.annotationProcessorPath 属性。

我们有一个多项目设置。在项目 'rest' 中,经过清理的 build.gradle 看起来像这样:

plugins {
    id 'net.ltgt.apt' version '0.21'
}

configurations {
        apt
}

dependencies {
    apt libraries.mapstruct_processor
    compileOnly libraries.mapstruct_processor
}

compileJava {
    options.annotationProcessorPath = configurations.apt
}

在过去 1.5 天内尝试了多种设置。不能让它工作。因此,如果有人有 mapstruct 与 Gradle 5 一起工作,我真的很感激工作 build.gradle、提示、指针。

PS。如何将以下内容替换为 Gradle 5 兼容版本。

tasks.withType(JavaCompile) {
    options.compilerArgs = [
            '-Amapstruct.suppressGeneratorTimestamp=true'
    ]
}

自最新 Gradle 版本(我会说 >= 4.8)以来,您可以按如下方式简化您的构建脚本;您不再需要 apt 插件,只需使用 annotationProcessor Gradle 配置:

ext{
    mapstructVersion = "1.2.0.Final"
}
dependencies{
    // ...
    // --- Mapstruct ---------------------------------
    compileOnly("org.mapstruct:mapstruct-jdk8:${mapstructVersion}")
    annotationProcessor "org.mapstruct:mapstruct-processor:${mapstructVersion}"
}
compileJava {
    options.annotationProcessorPath = configurations.annotationProcessor

    // if you need to configure mapstruct component model
    options.compilerArgs << "-Amapstruct.defaultComponentModel=spring" 
}

注意:默认情况下,Gradle 会将源生成到目录中:build/generated/sources/annotationProcessor/java/main

但这是可配置的,例如:

compileJava { 
   // ...
   options.setAnnotationProcessorGeneratedSourcesDirectory( file("$projectDir/src/generated/java"))