mapstruct-examples icon indicating copy to clipboard operation
mapstruct-examples copied to clipboard

Add example for mapstruct with Gradle 5

Open ghost opened this issue 6 years ago • 3 comments

The example for Gradle 5 does NOT seem to work with Gradle 5. From the release notes: Release notes Gradle 5 states: Gradle will no longer automatically apply annotation processors that are on the compile classpath — use CompileOptions.annotationProcessorPath instead. Specifically the "options.compilerArgs = [ '-Amapstruct.suppressGeneratorTimestamp=true" part.

See also my post on StackOverflow: https://stackoverflow.com/questions/56170222/migrating-from-gradle-4-to-5-how-to-get-mapstruct-1-20-final-working-with-it

ghost avatar May 16 '19 14:05 ghost

Courtesy of M.Ricciuti on Stack Overflow who supplied me with a very good answer. It works like a charm. Setting the options and where to create the source files was a very nice bonus. Something I was hoping to see in the original Gradle examples as well.

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

ghost avatar May 20 '19 14:05 ghost

Hey @bessels , thanks for reporting this issue and providing a possible solution!

Do you like to update the gradle example and provide a PR?

chris922 avatar May 21 '19 20:05 chris922

I think that this is covered by PR https://github.com/mapstruct/mapstruct-examples/pull/54

filiphr avatar May 27 '19 22:05 filiphr