How can I generate Mapstruct mapper implementations for test scope mapper interface in Gradle build?

saw303 :

I'm having a simple Java single module Gradle project in which I use Mapstruct for the Java mapping. My build.gradle looks like this:

plugins {
    id 'java-library'
    id 'groovy'
    id 'net.ltgt.apt' version '0.20'
}

repositories {
    jcenter()
}

dependencies {
    implementation 'org.mapstruct:mapstruct-jdk8:1.2.0.Final'
    annotationProcessor 'org.mapstruct:mapstruct-processor:1.2.0.Final'

    testImplementation 'org.codehaus.groovy:groovy-all:2.5.5'

    // Use the awesome Spock testing and specification framework even with Java
    testImplementation 'org.spockframework:spock-core:1.2-groovy-2.5'
    testImplementation 'junit:junit:4.12'
}

sourceSets {
    main {
        java {
            srcDirs "${project.buildDir}/generated/sources/annotationProcessor/java/main"
        }
    }
    test {
        java {
            srcDirs "${project.buildDir}/generated/sources/annotationProcessor/java/test"
        }
    }
}

My source folder contains the following Java source code:

src
├── main
│   ├── java
│   │   └── ch
│   │       └── silviowangler
│   │           ├── Person.java
│   │           ├── SomeMapper.java
│   │           └── User.java
│   └── resources
└── test
    ├── groovy
    │   └── ch
    │       └── silviowangler
    ├── java
    │   └── ch
    │       └── silviowangler
    │           └── YoloMapper.java
    └── resources

SomeMapper is a simple mapper interface that looks like this

@Mapper
public interface SomeMapper {

    @Mappings({
            @Mapping(target = "firstName", source = "nickname"),
            @Mapping(target = "surname", ignore = true),
            @Mapping(target = "dateOfBirth", ignore = true)
    })
    Person fromString(User user);
}

And the YoloMapper that resides in the test scope looks like that

@Mapper
public interface YoloMapper {

    String fromLocalDate(LocalDate localDate);
}

When I run ./gradlew clean cTJ the build completes successfully and the annotation processor generates a mapper implementation for SimpleMapper but it does not generate anything for the YoloMapper. The build folder after the build looks like

build
├── classes
│   └── java
│       ├── main
│       │   └── ch
│       │       └── silviowangler
│       │           ├── Person.class
│       │           ├── SomeMapper.class
│       │           ├── SomeMapperImpl.class
│       │           └── User.class
│       └── test
│           └── ch
│               └── silviowangler
│                   └── YoloMapper.class
├── generated
│   └── sources
│       └── annotationProcessor
│           └── java
│               ├── main
│               │   └── ch
│               │       └── silviowangler
│               │           └── SomeMapperImpl.java
│               └── test
└── tmp
    ├── compileJava
    └── compileTestJava

How can I make Gradle to tell the annotation processor to generate Mapstruct mapper implementation in the test scope?

M.Ricciuti :

You need to configure the testAnnocationProcess configuration, as follows:

dependencies{

    // for Main sources set
    implementation 'org.mapstruct:mapstruct-jdk8:1.2.0.Final'
    annotationProcessor 'org.mapstruct:mapstruct-processor:1.2.0.Final'

    // for Test sources set  
    testAnnotationProcessor "org.mapstruct:mapstruct-processor:1.2.0.Final"

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=69313&siteId=1