在测试编译阶段而不是编译阶段进行Maven注释处理?

问题描述 投票:3回答:2

我已经编写了一个简单的Java注释处理器,该处理器可以生成Java文件和资源文件。

我已经将注释处理器打包到一个jar文件中,并且我的maven项目可以在maven生命周期的compile阶段加载它并处理注释。生成的代码按预期显示在target / generated-sources / annotations中,生成的资源文件显示在target / classes中。

但是我只想在测试过程中使用生成的java文件和资源文件,所以我希望生成的文件出现在target / generated-test-sources / test-annotationstarget / test -classes。简而言之,我需要批注处理发生在test-compile阶段而不是compile阶段。

更重要的是:注释处理器运行在生产代码上,但是生成的文件随后包含在target \ foo-test.jar中,而不是target \ foo.jar中。

我不确定如何使用Maven进行此操作,除了可能在process-test-resources阶段移动生成的文件之外,这似乎很丑陋。

有什么建议吗?

编辑:我尝试将<scope>test</scope>添加到注释处理器的依赖项中,我可以看到该处理器是在testCompile阶段创建的,但是未调用该处理器的process()方法,并且什么也没有发生。

Edit#1:注释处理器包含以下代码,用于在。[[META-INF / services:

下写入.java文件和资源文件。@Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { List<String> declarations = generateDeclarationsForAnnotation(roundEnv); generateServiceImplFile(declarations); generateResourceFile(); return true; } private void generateResourceFile() throws Exception { JavaFileManager.Location location = StandardLocation.CLASS_OUTPUT; FileObject file= processingEnv.getFiler().createResource(location, "", "META-INF/services/x.y.z.FooService"); BufferedWriter writer = new BufferedWriter(file.openWriter()); ... write resource file ... } private void generateServiceImplFile(List<String> declarations) throws Exception { JavaFileObject file = processingEnv.getFiler().createSourceFile("x.y.z.FooServiceImpl"); BufferedWriter writer = new BufferedWriter(file.openWriter()); ... write source code ... }
我目前在pom.xml文件中声明依赖项,如下所示:

<!-- Dependency for the @Foo annotation --> <dependency> <groupId>x.y.z</groupId> <artifactId>foo-annotations</artifactId> <scope>compile</scope> </dependency> <!-- Dependency for the @Foo annotation processor --> <dependency> <groupId>x.y.z</groupId> <artifactId>foo-annotations-processor</artifactId> <scope>compile</scope> </dependency>

我正在使用JDK 1.7,maven-compiler-plugin 3.1版。注释处理器通过其存在于类路径上的方式被调用。我没有添加任何处理器插件等,也没有调整编译器插件等以使其正常工作。

Edit#2:我对maven-compiler-plugin进行了以下更改:

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <executions> <!-- Disable annotation processing for default compile execution --> <execution> <id>default-compile</id> <goals> <goal>compile</goal> </goals> <configuration> <proc>none</proc> </configuration> </execution> <!-- Only annotation process in this execution, with generated code going to generated-test-sources directory. --> <execution> <id>compile-with-annotations-processor</id> <goals> <goal>compile</goal> </goals> <configuration> <proc>only</proc> <generatedSourcesDirectory> ${project.build.directory}/generated-test-sources/test-annotations </generatedSourcesDirectory> </configuration> </execution> </executions> </plugin>
这将禁用对default-compile执行的注释处理,并且compile-with-annotations-processing执行仅进行注释处理,将生成的源放入

target \ generate-test-sources \ test-annotations

目录。运行此命令时,尽管资源文件仍出现在

target / classes

目录中,但在target / generate-test-sources / test-annotations目录中看到了生成的Java文件。我猜想可以通过将generateResourceFile()方法更改为使用StandardLocation.SOURCE_OUTPUT而不是StandardLocation.CLASS_OUTPUT来解决此问题。而且,此修复程序不能很好地处理多个注释处理器。我真的只希望在default-compile执行期间禁用此特定注释处理器,而仅在compile-with-annotations-processing执行中启用该处理器(具有不同的源目录)。否则,如果我添加另一个(不相关的)注释处理器,则其生成的代码将不会出现在预期的位置。

Edit#3:也许一个更简单的解决方案是允许编译器正常运行注释处理器,然后将生成的文件从target \ generation-sources

移至target \ generation -test-sources,例如使用maven-antrun-plugin

Edit#4:我想我已经使用上述maven-antrun-plugin解决了这个问题:

    配置maven-antrun-plugin,将文件从
  • 目标/生成的源/注释
移至目标/生成的源/测试注释,并从目标/类/ META-INF /在[[compile阶段,将服务更改为目标/测试类/ META-INF /服务。配置build-helper-maven-plugin以将目标/生成测试源添加到
  • 生成测试源阶段。
  • 我的[[foo.jarfoo-test.jar
    似乎正确构建了:-)

    Edit#5:我发现了一个名为maven-processor-plugin的插件,并将其配置为在生产代码的generate-test-sources阶段运行,并将输出目录设置为< [目标/生成测试源/注释

    目标/测试类

    。奇迹般有效。因此,现在完全不需要maven_antrun_plugin

    maven annotations
    2个回答
    1
    投票
    目标/生成源/注释目录。同一模块中的测试代码可以访问此位置,并且测试运行正常。然后,我将maven-jar-plugin配置为从

    artifactId .jar中排除生成的代码,并将生成的代码包含在名为artifactId -test-libs.jar的新工件中。然后其他模块在artifactId -test-libs上添加测试依赖项。

    © www.soinside.com 2019 - 2024. All rights reserved.