|
| 1 | +[[setup]] |
| 2 | +== Set up |
| 3 | + |
| 4 | +MapStruct Spring Extensions is a Java annotation processor based on http://www.jcp.org/en/jsr/detail?id=269[JSR 269] and as such can be used within command line builds (javac, Ant, Maven etc.) as well as from within your IDE. Also, you will need MapStruct itself (at least version `1.4.0.Final`) in your project. |
| 5 | + |
| 6 | +It comprises the following artifacts: |
| 7 | + |
| 8 | +* _org.mapstruct.extensions.spring:mapstruct-spring-annotations_: contains the added annotations such as `@SpringMapperConfig` |
| 9 | +* _org.mapstruct.extensions.spring:mapstruct-spring-extensions_: contains the annotation processor which generates Spring components |
| 10 | + |
| 11 | +=== Apache Maven |
| 12 | + |
| 13 | +For Maven based projects add the following to your POM file in order to use MapStruct Spring Extensions: |
| 14 | + |
| 15 | +.Maven configuration |
| 16 | +==== |
| 17 | +[source, xml, linenums] |
| 18 | +[subs="verbatim,attributes"] |
| 19 | +---- |
| 20 | +... |
| 21 | +<properties> |
| 22 | + <org.mapstruct.extensions.spring.version>{mapstructSpringExtensionsVersion}</org.mapstruct.extensions.spring.version> |
| 23 | +</properties> |
| 24 | +... |
| 25 | +<dependencies> |
| 26 | + <dependency> |
| 27 | + <groupId>org.mapstruct.extensions.spring</groupId> |
| 28 | + <artifactId>mapstruct-spring-annotations</artifactId> |
| 29 | + <version>${org.mapstruct.extensions.spring.version}</version> |
| 30 | + </dependency> |
| 31 | +</dependencies> |
| 32 | +... |
| 33 | +<build> |
| 34 | + <plugins> |
| 35 | + <plugin> |
| 36 | + <groupId>org.apache.maven.plugins</groupId> |
| 37 | + <artifactId>maven-compiler-plugin</artifactId> |
| 38 | + <version>3.8.1</version> |
| 39 | + <configuration> |
| 40 | + <source>1.8</source> |
| 41 | + <target>1.8</target> |
| 42 | + <annotationProcessorPaths> |
| 43 | + <path> |
| 44 | + <groupId>org.mapstruct.extensions.spring</groupId> |
| 45 | + <artifactId>mapstruct-spring-extensions</artifactId> |
| 46 | + <version>${org.mapstruct.extensions.spring.version}</version> |
| 47 | + </path> |
| 48 | + </annotationProcessorPaths> |
| 49 | + </configuration> |
| 50 | + </plugin> |
| 51 | + </plugins> |
| 52 | +</build> |
| 53 | +... |
| 54 | +---- |
| 55 | +==== |
| 56 | + |
| 57 | +[TIP] |
| 58 | +==== |
| 59 | +If you are working with the Eclipse IDE, make sure to have a current version of the http://www.eclipse.org/m2e/[M2E plug-in]. |
| 60 | +When importing a Maven project configured as shown above, it will set up the MapStruct Spring Extensions annotation processor so it runs right in the IDE, whenever you save a mapper type extending a Spring converter. |
| 61 | +Neat, isn't it? |
| 62 | +
|
| 63 | +To double check that everything is working as expected, go to your project's properties and select "Java Compiler" -> "Annotation Processing" -> "Factory Path". |
| 64 | +The MapStruct Spring Extensions JAR should be listed and enabled there. |
| 65 | +Any processor options configured via the compiler plug-in (see below) should be listed under "Java Compiler" -> "Annotation Processing". |
| 66 | +
|
| 67 | +If the processor is not kicking in, check that the configuration of annotation processors through M2E is enabled. |
| 68 | +To do so, go to "Preferences" -> "Maven" -> "Annotation Processing" and select "Automatically configure JDT APT". |
| 69 | +Alternatively, specify the following in the `properties` section of your POM file: `<m2e.apt.activation>jdt_apt</m2e.apt.activation>`. |
| 70 | +
|
| 71 | +Also make sure that your project is using Java 1.8 or later (project properties -> "Java Compiler" -> "Compile Compliance Level"). |
| 72 | +It will not work with older versions. |
| 73 | +==== |
| 74 | + |
| 75 | +=== Gradle |
| 76 | + |
| 77 | +Add the following to your Gradle build file in order to enable MapStruct Spring Extensions: |
| 78 | + |
| 79 | +.Gradle configuration (5.2 and later) |
| 80 | +==== |
| 81 | +[source, groovy, linenums] |
| 82 | +[subs="verbatim,attributes"] |
| 83 | +---- |
| 84 | +... |
| 85 | +
|
| 86 | +dependencies { |
| 87 | + ... |
| 88 | + implementation "org.mapstruct.extensions.spring:mapstruct-spring-annotations:${mapstructSpringExtensionsVersion}" |
| 89 | + annotationProcessor "org.mapstruct.extensions.spring:mapstruct-spring-extensions:${mapstructSpringExtensionsVersion}" |
| 90 | +
|
| 91 | + // If you are using MapStruct Spring Extensions in test code |
| 92 | + testAnnotationProcessor "org.mapstruct.extensions.spring:mapstruct-spring-extensions:${mapstructSpringExtensionsVersion}" |
| 93 | +} |
| 94 | +... |
| 95 | +---- |
| 96 | +==== |
| 97 | +.Gradle configuration (3.4 - 5.1) |
| 98 | +==== |
| 99 | +[source, groovy, linenums] |
| 100 | +[subs="verbatim,attributes"] |
| 101 | +---- |
| 102 | +... |
| 103 | +plugins { |
| 104 | + ... |
| 105 | + id 'net.ltgt.apt' version '0.20' |
| 106 | +} |
| 107 | +
|
| 108 | +// You can integrate with your IDEs. |
| 109 | +// See more details: https://github.com/tbroyer/gradle-apt-plugin#usage-with-ides |
| 110 | +apply plugin: 'net.ltgt.apt-idea' |
| 111 | +apply plugin: 'net.ltgt.apt-eclipse' |
| 112 | +
|
| 113 | +dependencies { |
| 114 | + ... |
| 115 | + implementation "org.mapstruct.extensions.spring:mapstruct-spring-annotations:${mapstructSpringExtensionsVersion}" |
| 116 | + annotationProcessor "org.mapstruct.extensions.spring:mapstruct-spring-extensions:${mapstructSpringExtensionsVersion}" |
| 117 | +
|
| 118 | + // If you are using MapStruct Spring Extensions in test code |
| 119 | + testAnnotationProcessor "org.mapstruct.extensions.spring:mapstruct-spring-extensions:${mapstructSpringExtensionsVersion}" |
| 120 | +} |
| 121 | +... |
| 122 | +---- |
| 123 | +==== |
| 124 | +.Gradle (3.3 and older) |
| 125 | +==== |
| 126 | +[source, groovy, linenums] |
| 127 | +[subs="verbatim,attributes"] |
| 128 | +---- |
| 129 | +... |
| 130 | +plugins { |
| 131 | + ... |
| 132 | + id 'net.ltgt.apt' version '0.20' |
| 133 | +} |
| 134 | +
|
| 135 | +// You can integrate with your IDEs. |
| 136 | +// See more details: https://github.com/tbroyer/gradle-apt-plugin#usage-with-ides |
| 137 | +apply plugin: 'net.ltgt.apt-idea' |
| 138 | +apply plugin: 'net.ltgt.apt-eclipse' |
| 139 | +
|
| 140 | +dependencies { |
| 141 | + ... |
| 142 | + compile "org.mapstruct.extensions.spring:mapstruct-spring-annotations:${mapstructSpringExtensionsVersion}" |
| 143 | + annotationProcessor "org.mapstruct.extensions.spring:mapstruct-spring-extensions:${mapstructSpringExtensionsVersion}" |
| 144 | +
|
| 145 | + // If you are using MapStruct Spring Extensions in test code |
| 146 | + testAnnotationProcessor "org.mapstruct.extensions.spring:mapstruct-spring-extensions:${mapstructSpringExtensionsVersion}" |
| 147 | +} |
| 148 | +... |
| 149 | +---- |
| 150 | +==== |
| 151 | + |
| 152 | + |
| 153 | +=== Apache Ant |
| 154 | + |
| 155 | +Add the `javac` task configured as follows to your _build.xml_ file in order to enable MapStruct Spring Extensions in your Ant-based project. Adjust the paths as required for your project layout. |
| 156 | + |
| 157 | +.Ant configuration |
| 158 | +==== |
| 159 | +[source, xml, linenums] |
| 160 | +[subs="verbatim,attributes"] |
| 161 | +---- |
| 162 | +... |
| 163 | +<javac |
| 164 | + srcdir="src/main/java" |
| 165 | + destdir="target/classes" |
| 166 | + classpath="path/to/mapstruct-spring-annotations{mapstructSpringExtensionsVersion}.jar"> |
| 167 | + <compilerarg line="-processorpath path/to/mapstruct-spring-extensions-{mapstructSpringExtensionsVersion}.jar"/> |
| 168 | + <compilerarg line="-s target/generated-sources"/> |
| 169 | +</javac> |
| 170 | +... |
| 171 | +---- |
| 172 | +==== |
0 commit comments