Tobi
Tobi

Reputation: 2040

NonExistentClass error when using Mapstruct with Kotlin for generated sources

I want to use Mapstruct to map internal models to models generated by OpenApi3 codegen in a Kotlin project.

When I compile the project, it seems that the Mapstruct is not able to find the sources generated by the OpenApi3 codegen plugin, as the resulting implementation contains a NonExistentClass instead of my OpenApi model.

My plugin configuration is

<plugin>
    <groupId>org.jetbrains.kotlin</groupId>
    <artifactId>kotlin-maven-plugin</artifactId>
    <configuration>
        <args>
            <arg>-Xjsr305=strict</arg>
        </args>
        <compilerPlugins>
            <plugin>spring</plugin>
            <plugin>jpa</plugin>
        </compilerPlugins>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-maven-allopen</artifactId>
            <version>${kotlin.version}</version>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-maven-noarg</artifactId>
            <version>${kotlin.version}</version>
        </dependency>
    </dependencies>

    <executions>
        <execution>
            <id>kapt</id>
            <phase>process-sources</phase>
            <goals>
                <goal>kapt</goal>
            </goals>
            <configuration>
                <sourceDirs>
                    <sourceDir>src/main/kotlin</sourceDir>
                    <sourceDir>src/main/java</sourceDir>
                </sourceDirs>
                <annotationProcessorPaths>
                    <annotationProcessorPath>
                        <groupId>org.mapstruct</groupId>
                        <artifactId>mapstruct-processor</artifactId>
                        <version>${mapstruct.version}</version>
                    </annotationProcessorPath>
                </annotationProcessorPaths>
            </configuration>
        </execution>

        <execution>
            <id>compile</id>
            <phase>process-sources</phase>
            <goals>
                <goal>compile</goal>
            </goals>
        </execution>
        <execution>
            <id>test-compile</id>
            <phase>process-test-sources</phase>
            <goals>
                <goal>test-compile</goal>
            </goals>
        </execution>

    </executions>
</plugin>

It seems that the problem is related to kapt finding the generated Java sources.

Is my configuration broken or am I encountering a limitation of the kotlin annotation processor?

EDIT: A simple example to reproduce this can be found here: https://github.com/tobisinghania/kotlin-openapi3-mapstruct-failure

Upvotes: 7

Views: 1948

Answers (1)

Lautre
Lautre

Reputation: 165

The issue is due to the missing <sourceDir>target/generated-sources/openapi/src/main/java</sourceDir> in the kotlin maven plugin configuration

I've made a Pull Request on your repo,

Upvotes: 5

Related Questions