Maven 使用 AnnotationProcessor 构建,它解析 src/main/java 中的文件并生成源到 generate-test-sources/test-annotations [英] Maven build with AnnotationProcessor that parses files in src/main/java and generates sources to generated-test-sources/test-annotations

查看:71
本文介绍了Maven 使用 AnnotationProcessor 构建,它解析 src/main/java 中的文件并生成源到 generate-test-sources/test-annotations的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 maven 3.0.3 和 Java7.

I'm using maven 3.0.3 and Java7.

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

I've got an AnnotationProcessor that is supposed to parse annotated java-files in src/main/java (not src/test/java) and generate Helper-Classes for JUnit-Tests. These Helper-Classes are supposed to be stored in target/generated-test-sources/test-annotations because they use libraries that are only available in test-scope. (Note: everything works fine as long as this dependency isn't in test-scope, but the build fails as soon as it is. It's definitely only needed in test-scope / during unit-tests and compilation of test-classes.)

我尝试了几种配置都没有运气:

I tried several configurations without any luck:

  1. 我将 maven-compiler-plugin 配置为在 compile:compile 期间使用 AnnotationProcessor.生成的 HelperClass 将存储在 generated-sources/annotations 中.不在 generated-test-sources/test-annotations 中根据需要.结果是,不会使用测试范围的依赖项.由于编译错误找不到符号",构建失败.失败

  1. I configured the maven-compiler-plugin to use the AnnotationProcessor during compile:compile. The generated HelperClass would be stored in generated-sources/annotations. Not in generated-test-sources/test-annotations as desired. The result was, that Test-Scoped dependencies would not have been used. Build failed due to Compilation Error "cannot find symbol". Fail

我使用了上面的配置,重新定义了generatedSourcesDirectory:

I used the above configuration and redefined the generatedSourcesDirectory:

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

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

The generated class would be stored in generated-test-sources/test-annotations as expected, but the build still failed because it tried to compile that file as above and missed the test-scoped dependencies. Fail

我尝试使用上述配置并排除 **/generated-test-sources/test-annotations/**/*.java 以防止编译器编译这个阶段:

I tried to use the above configuration and excluded **/generated-test-sources/test-annotations/**/*.java in order to prevent the compiler from compiling in this phase:

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

没有运气.与上述相同的编译器错误.失败

no luck. Same Compiler-Error as above. Fail

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

I configured the maven-compiler-plugin to use the AnnotationProcessor during test-compile:testCompile. The HelperClass might theoretically have been generated in generated-test-sources/test-annotations, but the AnnotationProcessor wouldn't stumble upon the annotated class that is located in src/main/java, not in src/test/java, which is AFAIK the compilation-scope during test-compile:testCompile. So the Annotated class would not be found, the HelperClass would not be generated and could therefore not be stored in generated-test-sources. Fail

尝试在 compile:testCompiletest-compile:compile 期间运行它,这在这两种情况下都导致 src/main/java 的类尚未正在编译 - 因此编译器错误.失败

Tried to run that during compile:testCompile and test-compile:compile, which lead in both cases to classes of src/main/java not yet being compiled - thus compiler error. Fail

我真正想做的是:

  1. compile:compile期间配置编译器使用AnnotationProcessor来生成我的HelperClass到${project.build.directory}/generated-test-sources/test-annotations 但不要让 maven 编译它
  2. 然后在test-compile:testCompile期间编译HelperClass.
  1. Configure the compiler to use the AnnotationProcessor during compile:compile to generate my HelperClass to ${project.build.directory}/generated-test-sources/test-annotations but don't let maven compile it
  2. Then compile the HelperClass during test-compile:testCompile.

我没有这样做.我不确定我是否在这里遗漏了重要的 maven 基础知识(概念),或者排除配置是否存在问题,或者其他任何问题.

I'm failing to do so. I'm not sure if I am missing important maven basics (concept) here, or if there's a problem with the exclusion configuration, or whatever it is.

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

Any help is very appreciated. Thanks in advance.

推荐答案

我用 includesexcludestestExcludes 等进行了几次测试上(我发现记录相当糟糕).似乎 maven 只是忽略了这些配置设置.不能真正相信它,但是尽管它表明包含和排除已正确配置并且使用了这些配置(mvn -X clean install),但它仍然编译了排除文件或没有编译包含文件.(顺便说一句:我也从 CLI 测试了它,因为我发现一些 IDE 仍然忽略这些设置.)在那里找到的任何解决方案,例如添加资源目录,包括,排除,在 test-compile:testCompile 期间定义 generatedTestSourcesDirectory 以匹配 generatedSourcesDirectorygeneratedSourcesDirectorycode>compile:compile 根本不起作用.随便.

I performed several tests with includes, excludes, testExcludes and so on (which I find rather badly documented). It seems that maven simply ignores these configuration settings. Can't really believe it, but though it showed that includes and excludes have been configured correctly and these configurations where used (mvn -X clean install), it still compiled excluded files or didn't compile included files. (BTW: I tested it from CLI as well, as I found out that some IDEs still ignore those settings.) Any solutions found out there, like adding resource directories, including, excluding, defining generatedTestSourcesDirectory during test-compile:testCompile to match the generatedSourcesDirectory of compile:compile simply didn't work. Whatever.

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

  1. 让第一个编译步骤 (compile:compile) 只使用 Annotation-Processors,但不编译生成的类:<proc>only</proc>.将 generatedSourcesDirectory 定义为应生成测试源的位置:${project.build.directory}/generated-test-sources/test-annotations.
  2. 使用 build-helper-maven-plugin 在正确的阶段和目标中添加 test-source-directory.隐含的 test-compile:testCompile 然后将在生成的类源路径添加到正常源路径的情况下执行.
  1. let the first compile-step (compile:compile) only use Annotation-Processors, but don't compile generated classes: <proc>only</proc>. Define the generatedSourcesDirectory to be where test-sources should be generated to: ${project.build.directory}/generated-test-sources/test-annotations.
  2. use build-helper-maven-plugin to add test-source-directory in the correct phase and goal. The implicit test-compile:testCompile would then be executed with the generated classes sourcepath added to the normal sourcepath.

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

Below is my configuration. Note that I need to generate other stuff before my actual problem occurs and that has to be generated into generated-sources/annotations, so there's one compile-step more than needed for the solution.

所以,就是这样:

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

这篇关于Maven 使用 AnnotationProcessor 构建,它解析 src/main/java 中的文件并生成源到 generate-test-sources/test-annotations的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆