6

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 Answers1

11

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"))
M.Ricciuti
  • 11,070
  • 2
  • 34
  • 54
  • Thanks so much for the response, going to give it a go right away. –  May 16 '19 at 15:16
  • Worked like a charm. Also your update wrt changing the output directory is very much appreciated. Everything is now in the same place as before. All unit-tests green (gradle and eclipse). I salute you. –  May 16 '19 at 16:24
  • Tried that immediately but got this message: Thanks for the feedback! Votes cast by those with less than 15 reputation are recorded, but do not change the publicly displayed post score. Didn't want to use my private github account and created a new SO account using my firms email address. –  May 16 '19 at 17:22
  • Perhaps you could answer one more question: In the old code we did use the following: `tasks.withType(JavaCompile) { options.compilerArgs = [ '-Amapstruct.suppressGeneratorTimestamp=true'] }` You suggested: `compileJava { options.compilerArgs << "-Amapstruct.defaultComponentModel=spring" }` I can only find an JavaDoc entry for tasks.withType - no user docs. The JavaDoc did'nt make things clearer for me.... May I ask what the difference is between the two and when I should use a specfic one. In my case both work the same. –  May 16 '19 at 17:32
  • both notations are similar. you can see different examples in this SO : https://stackoverflow.com/questions/29593500/how-can-i-set-the-compileoptions-for-my-gradle-java-plugin – M.Ricciuti May 16 '19 at 18:09