简体   繁体   中英

How to instruct Mapstruct to use lombok builder?

MapStruct is unable to create an implementation when I'm trying to map object with a private default constructor, but with a (lombok generated) builder.

SomeMapperImpl.java:[20,27] SomeDto() is not public in com.example.mapstructdemo.dto.SomeDto; cannot be accessed from outside package

Dto:

@Value
@Builder
public class SomeDto {
}

Model:

@Value
@Builder
public class SomeModel {
}

Mapper interface:

@Mapper
public interface SomeMapper {
    SomeDto map(SomeModel someModel);
    SomeModel map(SomeDto someDto);
}

fragment from Pom.xml:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>${compiler-plugin.version}</version>
        <configuration>
            <annotationProcessorPaths>
                <path>
                    <groupId>org.projectlombok</groupId>
                    <artifactId>lombok</artifactId>
                    <version>1.18.16</version>
                </path>
                <path>
                    <groupId>org.projectlombok</groupId>
                    <artifactId>lombok-mapstruct-binding</artifactId>
                    <version>0.2.0</version>
                </path>
                <path>
                    <groupId>org.mapstruct</groupId>
                    <artifactId>mapstruct-processor</artifactId>
                    <version>1.4.1.Final</version>
                </path>
            </annotationProcessorPaths>
        </configuration>
    </plugin>

The generated implementation:

@Generated(
    value = "org.mapstruct.ap.MappingProcessor",
    date = "2021-01-29T13:47:46+0100",
    comments = "version: 1.4.1.Final, compiler: javac, environment: Java 11.0.9.1 (Ubuntu)"
)
public class SomeMapperImpl implements SomeMapper {

    @Override
    public SomeDto map(SomeModel someModel) {
        if ( someModel == null ) {
            return null;
        }

        SomeDto someDto = new SomeDto();

        return someDto;
    }

    @Override
    public SomeModel map(SomeDto someDto) {
        if ( someDto == null ) {
            return null;
        }

        SomeModel someModel = new SomeModel();

        return someModel;
    }
}

What can I do to help mapstruct find the builder?

To reproduce the problem, clone this repo https://github.com/rmvanderspek/mapstruct-demo and mvn verify.

MapStruct is unable to create an implementation when I'm trying to map object with a private default constructor, but with a (lombok generated) builder.

SomeMapperImpl.java:[20,27] SomeDto() is not public in com.example.mapstructdemo.dto.SomeDto; cannot be accessed from outside package

Dto:

@Value
@Builder
public class SomeDto {
}

Model:

@Value
@Builder
public class SomeModel {
}

Mapper interface:

@Mapper
public interface SomeMapper {
    SomeDto map(SomeModel someModel);
    SomeModel map(SomeDto someDto);
}

fragment from Pom.xml:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>${compiler-plugin.version}</version>
        <configuration>
            <annotationProcessorPaths>
                <path>
                    <groupId>org.projectlombok</groupId>
                    <artifactId>lombok</artifactId>
                    <version>1.18.16</version>
                </path>
                <path>
                    <groupId>org.projectlombok</groupId>
                    <artifactId>lombok-mapstruct-binding</artifactId>
                    <version>0.2.0</version>
                </path>
                <path>
                    <groupId>org.mapstruct</groupId>
                    <artifactId>mapstruct-processor</artifactId>
                    <version>1.4.1.Final</version>
                </path>
            </annotationProcessorPaths>
        </configuration>
    </plugin>

The generated implementation:

@Generated(
    value = "org.mapstruct.ap.MappingProcessor",
    date = "2021-01-29T13:47:46+0100",
    comments = "version: 1.4.1.Final, compiler: javac, environment: Java 11.0.9.1 (Ubuntu)"
)
public class SomeMapperImpl implements SomeMapper {

    @Override
    public SomeDto map(SomeModel someModel) {
        if ( someModel == null ) {
            return null;
        }

        SomeDto someDto = new SomeDto();

        return someDto;
    }

    @Override
    public SomeModel map(SomeDto someDto) {
        if ( someDto == null ) {
            return null;
        }

        SomeModel someModel = new SomeModel();

        return someModel;
    }
}

What can I do to help mapstruct find the builder?

To reproduce the problem, clone this repo https://github.com/rmvanderspek/mapstruct-demo and mvn verify.

MapStruct is unable to create an implementation when I'm trying to map object with a private default constructor, but with a (lombok generated) builder.

SomeMapperImpl.java:[20,27] SomeDto() is not public in com.example.mapstructdemo.dto.SomeDto; cannot be accessed from outside package

Dto:

@Value
@Builder
public class SomeDto {
}

Model:

@Value
@Builder
public class SomeModel {
}

Mapper interface:

@Mapper
public interface SomeMapper {
    SomeDto map(SomeModel someModel);
    SomeModel map(SomeDto someDto);
}

fragment from Pom.xml:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>${compiler-plugin.version}</version>
        <configuration>
            <annotationProcessorPaths>
                <path>
                    <groupId>org.projectlombok</groupId>
                    <artifactId>lombok</artifactId>
                    <version>1.18.16</version>
                </path>
                <path>
                    <groupId>org.projectlombok</groupId>
                    <artifactId>lombok-mapstruct-binding</artifactId>
                    <version>0.2.0</version>
                </path>
                <path>
                    <groupId>org.mapstruct</groupId>
                    <artifactId>mapstruct-processor</artifactId>
                    <version>1.4.1.Final</version>
                </path>
            </annotationProcessorPaths>
        </configuration>
    </plugin>

The generated implementation:

@Generated(
    value = "org.mapstruct.ap.MappingProcessor",
    date = "2021-01-29T13:47:46+0100",
    comments = "version: 1.4.1.Final, compiler: javac, environment: Java 11.0.9.1 (Ubuntu)"
)
public class SomeMapperImpl implements SomeMapper {

    @Override
    public SomeDto map(SomeModel someModel) {
        if ( someModel == null ) {
            return null;
        }

        SomeDto someDto = new SomeDto();

        return someDto;
    }

    @Override
    public SomeModel map(SomeDto someDto) {
        if ( someDto == null ) {
            return null;
        }

        SomeModel someModel = new SomeModel();

        return someModel;
    }
}

What can I do to help mapstruct find the builder?

To reproduce the problem, clone this repo https://github.com/rmvanderspek/mapstruct-demo and mvn verify.

I've spent a couple of hours figuring this out:

Just in case you are not using Lombok @Builder to set up the builder pattern for your object.

Make sure your builder method is public and static :

 public static SectionBuilder builder() {
        return new SectionBuilder();
 } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM