在Maven模块化项目中使用mapstruct生成DTO

时间:2018-07-03 12:59:19

标签: java spring maven mapstruct

我有一个Java Web应用程序,我正在使用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>

我做错什么了吗?

1 个答案:

答案 0 :(得分:0)

我刚刚删除了此插件:

<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>

它工作正常,希望能对其他人有所帮助!