gpt4 book ai didi

java - 带有Gradle和Eclipse的MapStruct无法正常工作

转载 作者:行者123 更新时间:2023-12-03 05:41:59 28 4
gpt4 key购买 nike

使用SpringBoot和MapStruct创建了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(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文件夹下创建的,但是在运行代码时出现错误:

Exception in thread "main" java.lang.ExceptionInInitializerError at org.mapstruct.example.app.main(app.java:7) Caused by: java.lang.RuntimeException: java.lang.ClassNotFoundException: Cannot find implementation for org.mapstruct.example.SourceTargetMapper at org.mapstruct.factory.Mappers.getMapper(Mappers.java:68) at org.mapstruct.example.SourceTargetMapper.(SourceTargetMapper.java:29) ... 1 more Caused by: java.lang.ClassNotFoundException: Cannot find implementation for org.mapstruct.example.SourceTargetMapper at org.mapstruct.factory.Mappers.getMapper(Mappers.java:85) at org.mapstruct.factory.Mappers.getMapper(Mappers.java:65)



如果我从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']
}

测试{useTestNG()}

但仍然无法正常工作。请检查缺少的内容。

最佳答案

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

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

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

关于java - 带有Gradle和Eclipse的MapStruct无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52392705/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com