[英]Maven mapstruct issue when running mvn install/mvn test
So, I am using mapstruct api in my maven project.所以,我在我的 maven 项目中使用 mapstruct api。
Here is my application pom.xml configuration:这是我的应用程序pom.xml配置:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.mapstruct.examples.lombok</groupId>
<artifactId>mapstruct-examples-lombok</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<org.mapstruct.version>1.4.1.Final</org.mapstruct.version>
<org.projectlombok.version>1.18.16</org.projectlombok.version>
<lombok-mapstruct-binding.version>0.1.0</lombok-mapstruct-binding.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
<!-- lombok dependencies should not end up on classpath -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${org.projectlombok.version}</version>
<scope>provided</scope>
</dependency>
<!-- IntelliJ pre 2018.1.1 requires the mapstruct processor to be present as provided dependency -->
<!-- <dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
<scope>provided</scope>
</dependency>-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
<version>4.13.1</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<!-- See https://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html -->
<!-- Classpath elements to supply as annotation processor path. If specified, the compiler -->
<!-- will detect annotation processors only in those classpath elements. If omitted, the -->
<!-- default classpath is used to detect annotation processors. The detection itself depends -->
<!-- on the configuration of annotationProcessors. -->
<!-- -->
<!-- According to this documentation, the provided dependency processor is not considered! -->
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${org.projectlombok.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>${lombok-mapstruct-binding.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>target/generate-sources</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
My Pojo classes:我的 Pojo 课程:
SimpleSource.java SimpleSource.java
public class SimpleSource {
private String name;
private String description;
// getters and setters
}
SimpleDestination.java SimpleDestination.java
public class SimpleDestination {
private String name;
private String description;
// getters and setters
}
Mapper interface:映射器接口:
SimpleSourceDestinationMapper.java SimpleSourceDestinationMapper.java
@Mapper
public interface SimpleSourceDestinationMapper {
SimpleDestination sourceToDestination(SimpleSource source);
SimpleSource destinationToSource(SimpleDestination destination);
}
It generates proper implementation class:它生成正确的实现 class:
SimpleSourceDestinationMapperImpl.java ( generated ) SimpleSourceDestinationMapperImpl.java (生成)
public class SimpleSourceDestinationMapperImpl implements SimpleSourceDestinationMapper {
@Override
public SimpleDestination sourceToDestination(SimpleSource source) {
if ( source == null ) {
return null;
}
SimpleDestination simpleDestination = new SimpleDestination();
simpleDestination.setName( source.getName() );
simpleDestination.setDescription( source.getDescription() );
return simpleDestination;
}
@Override
public SimpleSource destinationToSource(SimpleDestination destination){
if ( destination == null ) {
return null;
}
SimpleSource simpleSource = new SimpleSource();
simpleSource.setName( destination.getName() );
simpleSource.setDescription( destination.getDescription() );
return simpleSource;
}
}
Everything is working fine when I run command mvn clean install
/ mvn clean test
.当我运行命令
mvn clean install
/ mvn clean test
时一切正常。
But when I run mvn test
/ mvn install
it is failing with below error.(Note: if we run mvn install/mvn test
for the first time, it works because target folder is not present for the first time before build).但是当我运行
mvn test
/ mvn install
它失败并出现以下错误。(注意:如果我们第一次运行mvn install/mvn test
,它可以工作,因为目标文件夹在构建之前第一次不存在)。
So if I run the mvn test
/ mvn install
again when target folder is present, I get the below error.因此,如果我在目标文件夹存在时再次运行
mvn test
/ mvn install
,则会收到以下错误。
mapstructapt\src\main\java\SimpleSourceDestinationMapper.java:9: error: Internal error in the mapping processor: java.lang.RuntimeException: javax.annotation.processing.FilerException: Attempt to recreate a file for type SimpleSourceDestinationMapperImpl at org.mapstruct.ap.internal.processor.MapperRenderingProcessor.createSourceFile(MapperRenderingProcessor.java:67) at org.mapstruct.ap.internal.processor.MapperRenderingProcessor.writeToSourceFile(MapperRenderingProcessor.java:52) at org.mapstruct.ap.internal.processor.MapperRenderingProcessor.process(MapperRenderingProcessor.java:42) at org.mapstruct.ap.internal.processor.MapperRenderingProcessor.process(MapperRenderingProcessor.java:37) at org.mapstruct.ap.MappingProcessor.process(MappingProcessor.java:223) at org.mapstruct.ap.
mapstructapt\src\main\java\SimpleSourceDestinationMapper.java:9:错误:映射处理器中的内部错误:java.lang.RuntimeException:javax.annotation.processing.FilerException:尝试为 SimpleMapDestination 重新创建一个简单映射结构的文件ap.internal.processor.MapperRenderingProcessor.createSourceFile(MapperRenderingProcessor.java:67) at org.mapstruct.ap.internal.processor.MapperRenderingProcessor.writeToSourceFile(MapperRenderingProcessor.java:52) at org.mapstruct.ap.internal.processor.MapperRenderingProcessor. process(MapperRenderingProcessor.java:42) at org.mapstruct.ap.internal.processor.MapperRenderingProcessor.process(MapperRenderingProcessor.java:37) at org.mapstruct.ap.MappingProcessor.process(MappingProcessor.java:223) at org.mapstruct .ap。 MappingProcessor.processMapperTypeElement(MappingProcessor.java:203)
MappingProcessor.processMapperTypeElement(MappingProcessor.java:203)
Not sure what is happening with mapstruct/maven.不确定 mapstruct/maven 发生了什么。 So need some help to solve this issue.
所以需要一些帮助来解决这个问题。
EDIT:编辑:
My pom.xml configuration is similar to the accepted answer suggested in below post.我的 pom.xml 配置类似于下面帖子中建议的公认答案。 MapStruct and Lombok not working together
MapStruct 和 Lombok 不能一起工作
I donot have any issues with lombok and mapstruct integration and my only issue is when running maven command like mvn test
or mvn install
which won't clean the target folder and then mapstruct trying to recreate the implementation files.我对 lombok 和 mapstruct 集成没有任何问题,我唯一的问题是运行 maven 命令时,例如
mvn test
或mvn install
不会清理目标文件夹,然后 mapstruct 尝试重新创建实现文件。
I donot have any issues with lombok and mapstruct integration and my only issue is when running maven command like mvn test or mvn install which won't clean the target folder and then mapstruct trying to recreate the implementation files.
我对 lombok 和 mapstruct 集成没有任何问题,我唯一的问题是运行 maven 命令时,例如 mvn test 或 mvn install 不会清理目标文件夹,然后 mapstruct 尝试重新创建实现文件。
So, use maven's option clean
.所以,使用 Maven 的选项
clean
。 It will clear your target folder.它将清除您的目标文件夹。
-> mvn clean install
for example. ->
mvn clean install
例如。
It seems you don't have issue finally...看来你终于没有问题了...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.