如何将测试生成的源保留在生成的源目标文件夹之外

How to keep test generated sources out of the generated sources target folder

我正在使用 MapStruct 生成一些实体到 DTO 映射器。我定义了一个抽象映射器,并使用测试实体、测试 dto 和测试映射器为其创建了一些测试。这些测试文件位于 src/test/java 文件夹中,但是当 MapStruct 为测试映射器生成实现时,它将生成的源放在 target/generated-sources 文件夹中而不是 target/generated-test-sources 文件夹中。这导致 class 被编译成我不想要的实际 jar 文件。

[更新] 我在这里举了一个重现问题的例子:

https://github.com/niltz/so-51090868-example

当我 运行 在命令行上使用 maven 构建时似乎工作正常,但是当我将 pom 导入 Spring Tool Suite 时,我遇到了问题。

我在 src/test/java/ 中使用 class 进行了快速测试,结果为 class

 @Mapper
 public abstract class AbstactClass {

 }

我的 maven pom 中有以下依赖项和插件:

      <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.5.1</version>
          <configuration>
            <source>1.8</source>
            <target>1.8</target>
            <annotationProcessorPaths>
              <path>
                <groupId>org.mapstruct</groupId>
                <artifactId>mapstruct-processor</artifactId>
                <version>1.1.0.Final</version>
              </path>
            </annotationProcessorPaths>
          </configuration>
        </plugin>

    <dependency>
      <groupId>org.mapstruct</groupId>
      <artifactId>mapstruct-jdk8</artifactId>
      <version>1.1.0.Final</version>
    </dependency>

和 运行 mvn clean install。生成的 class 在 target/generated-test-sources 中。 如果您提供有关 class 和设置的更多信息,我可以提供更多帮助。

似乎是 m2e eclipse 插件在使用 JDT APT、maven-compiler-plugin 和旧版本的 eclipse 时出现问题。

https://marketplace.eclipse.org/content/m2e-apt

显然我需要确保使用 Eclipse Photon 或更高版本,或者我可以改用 maven-processor-plugin。我选择升级 Eclipse,它现在似乎可以工作了。

https://bsorrentino.github.io/maven-annotation-plugin/