1

我正在尝试在新的 spring-boot 项目(个人)上使用 mapstruct 生成的类,似乎我的构建脚本需要其他东西。

这些类正在正确生成,因为我可以看到它们(构建文件夹中的 java 和类文件),并且当从 jar 文件执行应用程序时,它实际上可以工作。

问题是,当它从 Eclipse STS 执行时,它说 spring 找不到生成的类,是的,我确定它们是使用 @Component 创建的,并且位于 ComponentScanPath 中。

构建.gradle

buildscript {
    ext {
        springBootVersion = '1.1.6.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot' 

ext {
    javaLanguageLevel = '1.8'
    generatedMapperSourcesDir = "${buildDir}/generated-src/mapstruct/main"
}
configurations {
    mapstruct
}
sourceSets.main {
    ext.originalJavaSrcDirs = java.srcDirs
    java.srcDir "${generatedMapperSourcesDir}"
}
jar {
    baseName = 'MtgGrimoire'
    version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-aop")
    compile("org.springframework.boot:spring-boot-starter-web")
    compile("org.springframework.boot:spring-boot-starter-websocket")
    compile("org.springframework.boot:spring-boot-starter-data-jpa")
    compile("org.springframework.boot:spring-boot-starter-thymeleaf")
    compile("org.springframework.boot:spring-boot-starter-ws")
    compile('org.postgresql:postgresql:9.3-1102-jdbc41')
    compile( 'org.mapstruct:mapstruct:1.0.0.Beta1' )
    compile fileTree(dir: 'libs', include: ['*.jar'])
    mapstruct( 'org.mapstruct:mapstruct-processor:1.0.0.Beta1' )
    testCompile("org.springframework.boot:spring-boot-starter-test")
}

eclipse {
    classpath {
         containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
         containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8'
    }
}

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

task generateMainMapperClasses(type: JavaCompile) {
    ext.aptDumpDir = file( "${buildDir}/tmp/apt/mapstruct" )
    destinationDir = aptDumpDir

    classpath = compileJava.classpath + configurations.mapstruct
    source = sourceSets.main.originalJavaSrcDirs
    ext.sourceDestDir = file ( "$generatedMapperSourcesDir" )

    options.define(
        compilerArgs: [
            "-nowarn",
            "-proc:only",
            "-encoding", "UTF-8",
            "-processor", "org.mapstruct.ap.MappingProcessor",
            "-s", sourceDestDir.absolutePath,
            "-source", rootProject.javaLanguageLevel,
            "-target", rootProject.javaLanguageLevel,
        ]
    );

    inputs.dir source
    outputs.dir generatedMapperSourcesDir;
    doFirst {
         sourceDestDir.mkdirs()
    }
    doLast {
        aptDumpDir.delete()
    }
}

compileJava.dependsOn generateMainMapperClasses

此外,它似乎不是在项目 bin 文件夹中生成的

4

0 回答 0