gpt4 book ai didi

java - Maven 使用 AnnotationProcessor 构建,解析 src/main/java 中的文件并生成源代码到 generated-test-sources/test-annotations

转载 作者:行者123 更新时间:2023-11-30 06:22:51 27 4
gpt4 key购买 nike

我正在使用 maven 3.0.3 和 Java7。

我有一个 AnnotationProcessor,它应该在 src/main/java 中解析带注释的 java 文件。 (不是 src/test/java )并为 JUnit 测试生成辅助类。这些辅助类应该存储在 target/generated-test-sources/test-annotations 中因为他们使用仅在测试范围内可用的库。 (注意:只要此依赖项不在测试范围内,一切都可以正常工作,但构建一旦失败就会失败。它绝对只在测试范围内/单元测试和测试类编译期间需要。)

我尝试了几种配置,但都没有成功:

  1. 我配置了 maven-compiler-plugincompile:compile 期间使用 AnnotationProcessor .生成的 HelperClass 将存储在 generated-sources/annotations 中. generated-test-sources/test-annotations如预期的。结果是,不会使用 Test-Scoped 依赖项。由于编译错误“找不到符号”,构建失败。 失败

  2. 我使用了上面的配置并重新定义了generatedSourcesDirectory:

    <generatedSourcesDirectory>
    ${project.build.directory}/generated-test-sources/test-annotations
    </generatedSourcesDirectory>

    生成的类将存储在 generated-test-sources/test-annotations 中正如预期的那样,但构建仍然失败,因为它尝试按上述方式编译该文件并错过了测试范围的依赖项。 失败

  3. 我尝试使用上面的配置并排除了**/generated-test-sources/test-annotations/**/*.java为了防止编译器在这个阶段编译:

    <excludes>
    <exclude>**/generated-test-sources/test-annotations/**/*.java</exclude>
    </excludes>

    运气不好。与上述相同的编译器错误。 失败

  4. 我配置了 maven-compiler-plugintest-compile:testCompile 期间使用 AnnotationProcessor . HelperClass 理论上可能已在 generated-test-sources/test-annotations 中生成,但 AnnotationProcessor 不会偶然发现位于 src/main/java 中的注释类, 不在 src/test/java ,这是 AFAIK 期间的编译范围 test-compile:testCompile .因此将找不到带注释的类,将不会生成 HelperClass,因此无法将其存储在 generated-test-sources 中。 . 失败

  5. 试图在 compile:testCompile 期间运行它和 test-compile:compile ,这在这两种情况下都会导致 src/main/java 的类尚未被编译——因此会出现编译器错误。 失败

我真正想做的是:

  1. 将编译器配置为在 compile:compile 期间使用 AnnotationProcessor将我的 HelperClass 生成为 ${project.build.directory}/generated-test-sources/test-annotations 但是不要让maven编译它
  2. 然后在test-compile:testCompile期间编译HelperClass .

我没有这样做。我不确定我是否在这里缺少重要的 Maven 基础知识(概念),或者排除配置是否存在问题,或者它是什么。

非常感谢任何帮助。提前致谢。

最佳答案

我用 includes 进行了几次测试, excludes , testExcludes等等(我发现记录得很糟糕)。似乎 maven 只是忽略了这些配置设置。不能真的相信它,但尽管它表明包含和排除已正确配置并且使用了这些配置(mvn -X clean install),它仍然编译排除文件或不编译包含文件。 (顺便说一句:我也从 CLI 对其进行了测试,因为我发现某些 IDE 仍然会忽略这些设置。)在那里找到的任何解决方案,例如添加资源目录,包括、排除、定义 generatedTestSourcesDirectorytest-compile:testCompile期间匹配 generatedSourcesDirectorycompile:compile根本行不通。随便。

我找到了解决问题的方法如下:

  1. 让第一个编译步骤(compile:compile)只使用注释处理器,但不编译生成的类:<proc>only</proc> .定义 generatedSourcesDirectory应生成测试源的位置:${project.build.directory}/generated-test-sources/test-annotations .
  2. 使用build-helper-maven-plugin在正确的阶段和目标中添加测试源目录。隐式 test-compile:testCompile然后将在生成的类源路径添加到正常源路径的情况下执行。

下面是我的配置。请注意,我需要在实际问题发生之前生成其他内容,并且必须生成到 generated-sources/annotations 中,因此比解决方案所需的多了一个编译步骤。

所以,就是这样:

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
<executions>
<execution>
<!-- normal compile and generation of other classes to standard location (implicit, you shouldn't need that) -->
<id>default-compile</id>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<!-- Generates Test-Helper-Class into ${project.build.directory}/generated-test-sources/test-annotations WITHOUT compiling it -->
<id>compile-TestHelperClass</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<annotationProcessors>
<annotationProcessor>org.my.UnitTestGenerationProcessor</annotationProcessor>
</annotationProcessors>
<generatedSourcesDirectory>${project.build.directory}/generated-test-sources/test-annotations</generatedSourcesDirectory>
<!-- generated class depends on test-scope libs, so don't compile now: proc:only DISABLES compilation of generated classes-->
<proc>only</proc>
</configuration>
</execution>
<!-- implicit test-compile:testCompile -->
</executions>
</plugin>
<plugin>
<!-- adds source-dir during generate-test-sources:add-test-source
so that the path to our generated class is now known to the
compiler during test-compile:testCompile -->
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>add-test-source</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/generated-test-sources/test-annotations</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

关于java - Maven 使用 AnnotationProcessor 构建,解析 src/main/java 中的文件并生成源代码到 generated-test-sources/test-annotations,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18853585/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com