在maven模块项目中用mapstruct生成DTO
Generate DTOs with mapstruct in a maven modular projet
我有一个 java 网络应用程序,我正在使用 Spring 4.3.12.RELEASE
,我想集成 mapstruct
以自动生成 DTOs
。
首先,我用一些 facke 类 创建了一个新的独立项目,它工作正常,所以我可以找到我新生成的 类 *impl.java.
在那之后,我在我的项目中使用相同的 类 和相同的 pom 文件创建了一个新的 maven 模块,但是我的插件不起作用并且没有生成 impls!
这是我所拥有的示例:
public class SimpleSource {
private int id;
private String name;
//constructors and getters and setters
}
public class SimpleDestination {
private int id;
private String name;
//constructors and getters and setters
}
@Mapper(componentModel = "spring")
public interface SimpleSourceDestinationMapper {
SimpleDestination sourceToDestination(SimpleSource source);
SimpleSource destinationToSource(SimpleDestination destination);
}
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>fr.project</groupId>
<artifactId>project-parent</artifactId>
<version>${version.proj}</version>
</parent>
<groupId>fr.project</groupId>
<artifactId>service-dtos</artifactId>
<name>service-dto</name>
<dependencies>
<dependency>
<groupId>fr.pasteur</groupId>
<artifactId>service</artifactId>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-jdk8</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
</dependencies>
<build>
<finalName>service-dto</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</project>
我是不是做错了什么?
我刚刚删除了这个插件:
<plugin>
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>
<executions>
<execution>
<id>process</id>
<goals>
<goal>process</goal>
</goals>
<phase>generate-sources</phase>
<configuration>
<outputDirectory>target/metamodel</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
并在 maven-compiler-plugin 插件中添加:
<!-- For JPA static metamodel generation -->
<path>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>${hibernate.version}</version>
</path>
所以我的 pom 看起来像:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${java.version}</source>
<source>${java.version}</source>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${mapstruct.version}</version>
</path>
<!-- For JPA static metamodel generation -->
<path>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>${hibernate.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
........
</plugins>
它工作正常,我希望可以帮助其他人!
我有一个 java 网络应用程序,我正在使用 Spring 4.3.12.RELEASE
,我想集成 mapstruct
以自动生成 DTOs
。
首先,我用一些 facke 类 创建了一个新的独立项目,它工作正常,所以我可以找到我新生成的 类 *impl.java.
在那之后,我在我的项目中使用相同的 类 和相同的 pom 文件创建了一个新的 maven 模块,但是我的插件不起作用并且没有生成 impls!
这是我所拥有的示例:
public class SimpleSource {
private int id;
private String name;
//constructors and getters and setters
}
public class SimpleDestination {
private int id;
private String name;
//constructors and getters and setters
}
@Mapper(componentModel = "spring")
public interface SimpleSourceDestinationMapper {
SimpleDestination sourceToDestination(SimpleSource source);
SimpleSource destinationToSource(SimpleDestination destination);
}
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>fr.project</groupId>
<artifactId>project-parent</artifactId>
<version>${version.proj}</version>
</parent>
<groupId>fr.project</groupId>
<artifactId>service-dtos</artifactId>
<name>service-dto</name>
<dependencies>
<dependency>
<groupId>fr.pasteur</groupId>
<artifactId>service</artifactId>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-jdk8</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
</dependencies>
<build>
<finalName>service-dto</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</project>
我是不是做错了什么?
我刚刚删除了这个插件:
<plugin>
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>
<executions>
<execution>
<id>process</id>
<goals>
<goal>process</goal>
</goals>
<phase>generate-sources</phase>
<configuration>
<outputDirectory>target/metamodel</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
并在 maven-compiler-plugin 插件中添加:
<!-- For JPA static metamodel generation -->
<path>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>${hibernate.version}</version>
</path>
所以我的 pom 看起来像:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${java.version}</source>
<source>${java.version}</source>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${mapstruct.version}</version>
</path>
<!-- For JPA static metamodel generation -->
<path>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>${hibernate.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
........
</plugins>
它工作正常,我希望可以帮助其他人!