mapstruct / mapstruct-examples

Examples for using MapStruct
Other
1.29k stars 508 forks source link

Add example for mapstruct with Gradle 5 #66

Open ghost opened 5 years ago

ghost commented 5 years ago

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 commented 5 years ago

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"))
}
chris922 commented 5 years ago

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

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

filiphr commented 5 years ago

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