【问题标题】:Mapstruct together with other annotation-processors in maven-pomMapstruct 与 maven-pom 中的其他注释处理器一起
【发布时间】:2020-01-15 07:12:41
【问题描述】:

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0

<artifactId>yyy-data</artifactId>
<name>VS yyy (data)</name>

<parent>
    <groupId>de.xxx</groupId>
    <artifactId>yyy-server</artifactId>
    <version>8.1.0-SNAPSHOT</version>
</parent>
<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <distribution.outputDirectory>${project.build.directory}/dist</distribution.outputDirectory>
</properties>

<dependencies>

    <dependency>
        <groupId>com.mysema.querydsl</groupId>
        <artifactId>querydsl-apt</artifactId>
    </dependency>
    <dependency>
        <groupId>com.mysema.querydsl</groupId>
        <artifactId>querydsl-jpa</artifactId>
    </dependency>

    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.2.8</version>
    </dependency>
    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-core</artifactId>
        <version>2.2.8-b01</version>
    </dependency>
    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-impl</artifactId>
        <version>2.2.8-b01</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.mapstruct</groupId>
        <artifactId>mapstruct</artifactId>
        <version>1.3.0.Final</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <annotationProcessorPaths>
                    <path>
                        <groupId>org.mapstruct</groupId>
                        <artifactId>mapstruct-processor</artifactId>
                        <version>1.3.0.Final</version>
                    </path>
                </annotationProcessorPaths>
                <annotationProcessors>
                    <annotationProcessor>de.zzz.annotation.processor.GenerateHibernateTypeForEnumProcessor</annotationProcessor>
                    <annotationProcessor>de.zzz.annotation.processor.GenerateHibernateTypeForWertelistenProcessor</annotationProcessor>
                    <annotationProcessor>de.zzz.annotation.processor.BusinessObjectAnnotationProcessor</annotationProcessor>
                    <annotationProcessor>com.mysema.query.apt.jpa.JPAAnnotationProcessor</annotationProcessor>
                </annotationProcessors>
            </configuration>
            <executions>
                <execution>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

<profiles>
    <profile>
        <id>m2e</id>
        <activation>
            <property>
                <name>m2e.version</name>
            </property>
        </activation>
        <build>
            <plugins>
                <plugin> 
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <executions>
                        <execution>
                            <goals>
                                <goal>compile</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

亲爱的,

在我的project 中实现mapstruct 时需要一些帮助。我已经有一些注释处理器需要在 maven 的pom.xml 中命名。 Mapstruct 建议在 pom.xml 中添加 annotationProcessorPaths。似乎不可能在同一个 pom.xml 中同时使用两个注解(annotationProcessorPathsannotationProcessor)。如果我这样做,我会收到错误

未能执行目标 org.apache.maven.plugins:maven-compiler-plugin:3.8.0:compile (default) on project yyy-data: 编译失败 [错误] 未找到注释处理器“de.zzz.annotation.processor.GenerateHibernateTypeForEnumProcessor”

我确实缩短了 pom。因此,可能存在比实际需要更多的依赖项。

提前致谢 奥利弗

【问题讨论】:

    标签: java maven pom.xml mapstruct annotation-processor


    【解决方案1】:

    您仍然可以使用annotationProcessor。您需要在依赖项中添加mapstruct-processor,然后在annotationProcessor 中添加org.mapstruct.ap.MappingProcessor

    您看到错误的原因是,当您定义 annotationProcessorPaths 时,maven-compiler-plugin 只会使用那些用于查找注释处理器而不是您的其他依赖项。

    或者,如果您的依赖项,您也可以添加您在annotationProcessoPaths 中拥有的所有注释处理器。

    【讨论】:

    • 非常感谢。你的第一个建议解决了这个问题。我有第二个问题。大多数其他注释处理器是自行编写的并且在项目内部。我无法找到如何为此类类设置 annotationProcessorPath。你也可以在这里给我一个提示吗?它应该类似于“将项目类路径添加到处理器路径”。
    • 通常你在不同的模块中编写注释处理器并在 annotationProcessorPaths 中使用该模块。我不知道你是否可以从同一个模块中实现你正在做的事情
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-22
    • 1970-01-01
    • 2020-10-14
    • 2011-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多