We've use mapstruct 1.20.final for approx 1.5 years with various Gradle versions - latest gradle 4.10.2. We want to switch to Gradle 5.4.1, which works with everything except mapstruct. Our working setup was not clean. Hence decided to start over. Old working setup was a hybrid form of the example on Github and the now obsolete setup.

Started again with 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. This works for 4.10.2. With Gradle 5 this results in the following error: Execution failed for task ':eu.educator.rest:compileJava'. Cannot specify -processorpath or --processor-path via CompileOptions.compilerArgs. Use the CompileOptions.annotationProcessorPath property instead.

We have a multi-project setup. In the project 'rest' the sanitized build.gradle looks like this:

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

configurations {
        apt
}

dependencies {
    apt libraries.mapstruct_processor
    compileOnly libraries.mapstruct_processor
}

compileJava {
    options.annotationProcessorPath = configurations.apt
}

Have tried multiple setups in the last 1.5 days. Can NOT get it to work. So if anyone has mapstruct working with Gradle 5 I really would appreciate a working build.gradle, hints, pointers.

PS. How can I replace the following with a Gradle 5 compliant version.

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

1 Answers

1
M.Ricciuti On Best Solutions

Since latest Gradle version ( >= 4.8 I would say) you can simplify your build script as follows ; you don't need apt plugin anymore, just use annotationProcessor Gradle configuration :

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" 
}

Note: by default, Gradle will generate sources into directory :build/generated/sources/annotationProcessor/java/main

But this is configurable, for example:

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