With Java 17 and Spring Boot 3.2.3 @Mapper is not able create Beans

158 views Asked by At

I was using Mapstruct since last 3 year with Java 11 and Spring boot 2.6.6. Recently updated project dependencies to Java 17 and Spring Boot 3.2.3. Once I did that having issue in creating the beans of Mapper

Description:

   Field myMapper in com.abc.portal.service.UserService required a bean of type 
  'com.abc.portal.mapper.UserMapper' that could not be found.


Action:

Consider defining a bean of type 'com.abc.portal.mapper.UserMapper' in your configuration.

On the other hand I have all required plugin in pom.xml

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
                <annotationProcessorPaths>
                    <path>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                        <version>${lombok.version}</version>
                    </path>
                    <path>
                        <groupId>org.mapstruct</groupId>
                        <artifactId>mapstruct-processor</artifactId>
                        <version>${org.mapstruct.version}</version>
                    </path>
                    <path>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok-mapstruct-binding</artifactId>
                        <version>0.2.0</version>
                    </path>
                    <path>
                        <groupId>com.querydsl</groupId>
                        <artifactId>querydsl-apt</artifactId>
                        <version>5.0.0</version>
                        <classifier>jakarta</classifier>
                    </path>
                    <path>
                        <groupId>jakarta.persistence</groupId>
                        <artifactId>jakarta.persistence-api</artifactId>
                        <version>3.1.0</version>
                    </path>
                </annotationProcessorPaths>
                <compilerArgs>
                    <compilerArg>
                        -Amapstruct.defaultComponentModel=spring
                    </compilerArg>
                </compilerArgs>
            </configuration>
        </plugin>
        

Just for sake of double check I have tried with

@Mapper(componentModel = "spring") and mvn clean install

as well. But still no luck for me. Any suggestion would be appreciated.

Update Auto generated files are generating enter image description here

1

There are 1 answers

0
Dilshad On

Add plugin in pom.xml

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>3.5.0</version>
    <executions>
      <execution>
        <id>add-source</id>
        <phase>generate-sources</phase>
        <goals>
          <goal>add-source</goal>
        </goals>
        <configuration>
          <sources>
            <source>target/generated-sources/annotations</source>
          </sources>
        </configuration>
      </execution>
    </executions>
  </plugin>

Then execute

mvn clean install

But note that - Run mvn command from terminal not from your IDE. Do not do clean of your project once mvn clean install get successful.