使用Gradle和eclipse的MapStruct不起作用

问题描述 投票:0回答:1

使用带有MapStruct的Spring Boot创建gradle应用程序。

Build.gradle文件有以下条目。

dependencies {
    compile "org.mapstruct:mapstruct-jdk8:${mapstructVersion}"
    testCompile 'org.testng:testng:6.10', 'org.easytesting:fest-assert:1.4'
    compile "org.mapstruct:mapstruct-processor:${mapstructVersion}"
}

plugins {
    id 'java'
    id 'net.ltgt.apt-eclipse' version '0.18'
}

Mapper类

@Mapper(componentModel="spring")
public interface SourceTargetMapper {

    SourceTargetMapper INSTANCE = Mappers.getMapper(SourceTargetMapper.class);

    @Mapping(source = "qax", target = "baz")
    @Mapping(source = "baz", target = "qax")
    Target sourceToTarget(Source source);

    @InheritInverseConfiguration
    Source targetToSource(Target target);
}

Impl文件是在build文件夹下创建的,但在运行代码时遇到错误:

org.mapstruct.example.app.main(app.java:7)中线程“main”java.lang.ExceptionInInitializerError中的异常引起:java.lang.RuntimeException:java.lang.ClassNotFoundException:无法找到org.mapstruct的实现位于org.mapstruct.example.SourceTargetMapper的org.mapstruct.factory.Mappers.getMapper(Mappers.java:68)的.example.SourceTargetMapper。(SourceTargetMapper.java:29)... 1更多引起:java.lang.ClassNotFoundException :无法在org.mapstruct.factory.Mappers.getMapper(Mappers.java:65)的org.mapstruct.factory.Mappers.getMapper(Mappers.java:85)中找到org.mapstruct.example.SourceTargetMapper的实现

如果我从Build文件夹bin文件夹中移动Impl文件,它可以正常工作。这意味着MapStruct在build文件夹下创建实现文件并尝试从bin文件夹访问它。

如何将构建文件的位置更改为bin文件夹?

使用M2e插件可以正常使用Maven项目,但不能使用gradle。

建议更改后的Build.gradle文件。

    plugins {
    id 'java'
    id 'net.ltgt.apt-eclipse' version '0.18'
}

repositories {
    mavenCentral()
    mavenLocal()
}

task wrapper(type: Wrapper) { gradleVersion = '3.4' }

ext {
    mapstructVersion = "1.2.0.Final"
    javaLanguageLevel = '1.8'
    generatedMapperSourcesDir = "${buildDir}/generated-src/mapstruct/main"
}

sourceCompatibility = rootProject.javaLanguageLevel

sourceSets.main {
    ext.originalJavaSrcDirs = java.srcDirs
    java.srcDir "${generatedMapperSourcesDir}"
}

dependencies {
    compile "org.mapstruct:mapstruct-jdk8:${mapstructVersion}"
    testCompile 'org.testng:testng:6.10', 'org.easytesting:fest-assert:1.4'
    annotationProcessor "org.mapstruct:mapstruct-processor:${mapstructVersion}"
}

tasks.withType(JavaCompile) {
    options.compilerArgs = ['-Amapstruct.suppressGeneratorTimestamp=true']
}

test {useTestNG()}

但仍然没有工作。请检查遗漏的内容。

java eclipse spring-boot gradle mapstruct
1个回答
0
投票

看起来生成的源不会自动包含在应用程序的类路径中。

the sample on github射击,它工作正常。

使用相同的gradle config as the sample为您的应用程序:

© www.soinside.com 2019 - 2024. All rights reserved.