Java 在 Maven 中为注解处理器设置生成的源目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1588038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 16:31:12  来源:igfitidea点击:

Setting the generated source directory for annotation processors in Maven

javamavenmaven-2annotation-processing

提问by Robert Munteanu

I'm trying to move a build which generates sources using an annotation processor to Maven. I've tried configuring the maven-compiler-plugin as follows:

我正在尝试将使用注释处理器生成源的构建移动到 Maven。我试过如下配置 maven-compiler-plugin:

    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <fork>true</fork>
                <compilerArgument>-s ${project.build.directory}/target/generated-sources/annotation-processing</compilerArgument>
            </configuration>
        </plugin>
    </plugins>

But javac fails with

但是 javac 失败了

[INFO] Compilation failure  
Failure executing javac,  but could not parse the error:
javac: invalid flag: -s /home/robert/workspaces/betbrain/sportsengine.common/sportsengine.bean.test/target/target/generated-sources/annotation-processing  
Usage: javac <options> <source files>  
use -help for a list of possible options

As far as I can tell, -s should be passed before the source files to javac, but maven passes it after.

据我所知, -s 应该在源文件之前传递给 javac,但是 maven 在之后传递它。

How can I pass the -sflag properly to the maven-compiler-plugin?

如何将-s标志正确传递给 maven-compiler-plugin?



Update: the maven-annotation-plugindoes not seem to work.

更新maven-annotation-plugin似乎不起作用。

When configured as

当配置为

        <plugin>
            <groupId>org.bsc.maven</groupId>
            <artifactId>maven-processor-plugin</artifactId>
            <version>1.0-SNAPSHOT</version>
            <executions>
                <execution>
                    <id>process</id>
                    <goals>
                        <goal>process</goal>
                    </goals>
                    <phase>generate-sources</phase>
                    <configuration>
                        <outputDirectory>${generated.sources.directory}</outputDirectory>
                        <processors>
                            <processor>xxx.annotation.EnforceJavaBeansConventionsProcessor</processor>
                        </processors>
                    </configuration>
                </execution>
            </executions>
        </plugin>

Execution fails with

执行失败

[INFO] [processor:process {execution: process}]
error: Annotation processor 'xxx.annotation.EnforceJavaBeansConventionsProcessor' not found
1 error

采纳答案by Robert Munteanu

The plugin was using the harcoded Windows classpath separator to build the classpath, so it was failing on my Linux machine.

该插件使用硬编码的 Windows 类路径分隔符来构建类路径,所以它在我的 Linux 机器上失败了。

Submitted patches:

提交的补丁:

回答by Pascal Thivent

I may be missing something but shouldn't you:

我可能会遗漏一些东西,但你不应该:

  1. Generate sources in target/generated-sources/annotation-processingduring the generate-sourcesphase? The apt-maven-pluginor the maven-annotation-plugincould help.

  2. Include generated sources when compiling sources into target/classesusing <includes>in the maven-compiler-pluginor the maven-build-helper-plugin?

  1. target/generated-sources/annotation-processinggenerate-sources阶段期间生成源?该公寓- Maven的插件Maven的注解插件可以帮助。

  2. 在将源编译为在maven-compiler-pluginmaven-build-helper-plugin 中target/classes使用时包括生成的源?<includes>

EDIT:Where is xxx.annotation.EnforceJavaBeansConventionsProcessorlocated? Don't you need to add dependenciesto the configuration of the maven-annotation-plugin as documented on the Usagepage?

编辑:xxx.annotation.EnforceJavaBeansConventionsProcessor位于何处?您不需要添加dependencies使用页面上记录的 maven-annotation-plugin 的配置中吗?

<plugin>
  <groupId>org.bsc.maven</groupId>
  <artifactId>maven-processor-plugin</artifactId>
  <version>1.0-SNAPSHOT</version>
  <executions>
    <execution>
      <id>process</id>
      <goals>
        <goal>process</goal>
      </goals>
      <phase>generate-sources</phase>
      <configuration>
        <outputDirectory>src/main/generated</outputDirectory><!-- PROCESSOR OUT DIR --> 
        <processors><!-- LIST OF PROCESSOR CLASS(S) -->
          <processor>org.bsc.apt.BeanInfoAnnotationProcessor</processor>
        </processors>
      </configuration> 
    </execution>
  </executions>
  <dependencies/><!-- ADD DEPENDENCIES HERE IF REQUIRED -->
</plugin>

PS: I wouldn't use src/main/generatedas output directory but rather a subdirectory of target/generated-sources.

PS:我不会src/main/generated用作输出目录,而是用作target/generated-sources.

回答by Jesse Glick

Not exactly an answer to your question, but of interest:

不完全是您问题的答案,但很有趣:

http://jira.codehaus.org/browse/MCOMPILER-75

http://jira.codehaus.org/browse/MCOMPILER-75

I'm afraid there are a number of issues using JSR 269 in Maven, at least with the default compiler plugin.

恐怕在 Maven 中使用 JSR 269 有很多问题,至少在默认编译器插件中是这样。

回答by user611480

One reason for this could be that $JAVA_HOME is pointing to jdk 1.5 version instead of 1.6. (on windows check %JAVA_HOME% of course)

原因之一可能是 $JAVA_HOME 指向 jdk 1.5 版本而不是 1.6。(当然在 Windows 上检查 %JAVA_HOME%)

回答by user1374968

I got the same problem ...

我遇到了同样的问题...



[ERROR] COMPILATION ERROR :

[INFO] -------------------------------------------------------------

[ERROR] Failure executing javac, but could not parse the error:

javac: invalid flag: -s
...


Are you using java5?

你在使用 java5吗?

$ mvn -version

Apache Maven 2.2.1 (rdebian-8)

Java version: 1.5.0_22

Java home: /usr/lib/jvm/java-1.5.0-sun-1.5.0.22/jre

Default locale: en_US, platform encoding: UTF-8

OS name: "linux" version: "3.2.0-24-generic" arch: "amd64" Family: "unix"

$ which java
/usr/bin/java

$ ll /usr/bin/java
/usr/bin/java -> /etc/alternatives/java*

$ ll /etc/alternatives/java
/etc/alternatives/java -> /usr/lib/jvm/java-1.5.0-sun/jre/bin/java*


$ sudo update-alternatives --config java



$ mvn -version

Apache Maven 2.2.1 (rdebian-8)

Java version: 1.6.0_24

Java home: /usr/lib/jvm/java-6-openjdk-amd64/jre

Default locale: en_US, platform encoding: UTF-8

OS name: "linux" version: "3.2.0-24-generic" arch: "amd64" Family: "unix"


now, mvn installshould work :-)

现在,mvn install应该可以工作了:-)

回答by HaveAGuess

I had this issue with GWTP Source Generate APT Processing annotations

我在 GWTP Source Generate APT Processing annotations 遇到了这个问题

It was because I didn't set a version for the compiler plugin, my final setup:

那是因为我没有为编译器插件设置版本,我的最终设置:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.0</version>
    <configuration>
        <source>1.6</source>
        <target>1.6</target>
        <!-- <compilerArgument>-proc:none</compilerArgument> -->
        <generatedSourcesDirectory>${generated.sources}</generatedSourcesDirectory>
        <annotationProcessors>
            <annotationProcessor>com.gwtplatform.dispatch.annotation.processor.GenEventProcessor</annotationProcessor>
            <annotationProcessor>com.gwtplatform.dispatch.annotation.processor.GenDtoProcessor</annotationProcessor>
            <annotationProcessor>com.gwtplatform.dispatch.annotation.processor.GenDispatchProcessor</annotationProcessor>
        </annotationProcessors>
    </configuration>
</plugin>

回答by John Q Citizen

I've been using the maven-processor-pluginas follows:

我一直在使用maven-processor-plugin如下:

<plugin>
    <groupId>org.bsc.maven</groupId>
    <artifactId>maven-processor-plugin</artifactId>
    <version>2.2.4</version>
    <executions>
      <execution>
        <id>process-my-annotations</id>
        <goals>
          <goal>process</goal>
        </goals>
        <phase>generate-test-sources</phase>
        <configuration>
          <processors>
            <processor>x.y.z.MyAnnotationProcessor</processor>
          </processors>
          <outputDirectory>${project.build.directory}/generated-test-sources/test-annotations</outputDirectory>
          <outputClassDirectory>${project.build.directory}/test-classes</outputClassDirectory>
        </configuration>
      </execution>
    </executions>
  </plugin>

The generated code goes into target/generated-test-sources/test-annotations, and generated resources into target/test-classes(the default for the processgoal is target/generated-sourcesand target/classes, respectively).

生成的代码进入target/generated-test-sources/test-annotations,生成的资源进入target/test-classes流程目标的默认值分别是target/generated-sourcestarget/classes)。

I also need to use maven-build-helper-pluginto add target/generated-test-sources/test-annotationsto the test source path so that test-compilephase will compile the generated code.

我还需要使用maven-build-helper-plugintarget/generated-test-sources/test-annotations 添加到测试源路径,以便test-compile阶段将编译生成的代码。