mapstruct / mapstruct-examples

Examples for using MapStruct
Other
1.28k stars 512 forks source link

Exception on mapstruct-lombok & mapstruct-on-gradle with gradle plugin net.ltgt.apt #31

Closed duquewu closed 7 years ago

duquewu commented 7 years ago

Exception on mapstruct-lombok & mapstruct-on-gradle with gradle plugin net.ltgt.apt

java.lang.ExceptionInInitializerError
    at com.mycompany.mapper.SourceTargetMapperTest.testMapping(SourceTargetMapperTest.java:35)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.RuntimeException: java.lang.ClassNotFoundException: Cannot find implementation for com.mycompany.mapper.SourceTargetMapper
    at org.mapstruct.factory.Mappers.getMapper(Mappers.java:79)
    at com.mycompany.mapper.SourceTargetMapper.<clinit>(SourceTargetMapper.java:31)
    ... 23 more
Caused by: java.lang.ClassNotFoundException: Cannot find implementation for com.mycompany.mapper.SourceTargetMapper
    at org.mapstruct.factory.Mappers.getMapper(Mappers.java:93)
    at org.mapstruct.factory.Mappers.getMapper(Mappers.java:76)
    ... 24 more

Process finished with exit code 255
duquewu commented 7 years ago

It seems gradle's apt plugin not work well.After I remove the apt plugin and replace apt with compileOnly everything works well.

Before

plugins {
    id 'java'
    id 'net.ltgt.apt' version '0.9'
}

...

dependencies {
    compile "org.mapstruct:mapstruct-jdk8:${mapstructVersion}", "org.projectlombok:lombok:${lombokVersion}"
    testCompile 'junit:junit:4.12'
    apt "org.mapstruct:mapstruct-processor:${mapstructVersion}", "org.projectlombok:lombok:${lombokVersion}"
}

After

plugins {
    id 'java'
}

...

dependencies {
    compile "org.mapstruct:mapstruct-jdk8:${mapstructVersion}", "org.projectlombok:lombok:${lombokVersion}"
    testCompile 'junit:junit:4.12'
    compileOnly "org.mapstruct:mapstruct-processor:${mapstructVersion}", "org.projectlombok:lombok:${lombokVersion}"
}
gunnarmorling commented 7 years ago

Hi, what are the steps to reproduce this issue? Are you using the Gradle wrapper checked into the project? Using this one (Gradle 3.4), everything works when running ./gradlew clean build. Are you using another Gradle version perhaps?

filiphr commented 7 years ago

To sum up for others that stumble to this issue. The problem was with the setup of IntelliJ. When using Gradle and IntelliJ make sure that you use Delegating IDE build/run actions to Gradle. That way everything will go via Gradle and will work correctly.

The reason why we recommend using the apt plugin is because by using that you won't expose the processor JAR on the compilation classpath. That way no code from the processor could accidentally be used within the compiled project.