6

我正在尝试将使用注释处理器生成源的构建移动到 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>

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

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

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


更新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>${generated.sources.directory}</outputDirectory>
                        <processors>
                            <processor>xxx.annotation.EnforceJavaBeansConventionsProcessor</processor>
                        </processors>
                    </configuration>
                </execution>
            </executions>
        </plugin>

执行失败

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

7 回答 7

5

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

提交的补丁:

于 2009-10-19T14:13:28.770 回答
5

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

https://issues.apache.org/jira/browse/MCOMPILER-75

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

于 2010-01-23T18:58:32.467 回答
4

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

  1. target/generated-sources/annotation-processing在阶段生成源generate-sourcesapt-maven-pluginmaven-annotation-plugin可以提供帮助。

  2. maven-compiler-pluginmaven-build-helper-plugin中编译源时包含生成target/classes的源?<includes>

编辑:位于哪里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:我不会src/main/generated用作输出目录,而是用作target/generated-sources.

于 2009-10-19T11:36:28.400 回答
1

造成这种情况的一个原因可能是 $JAVA_HOME 指向 jdk 1.5 版本而不是 1.6。(当然在 Windows 上检查 %JAVA_HOME%)

于 2011-08-19T14:25:55.007 回答
1

我在 GWTP Source Generate APT Processing 注释中遇到了这个问题

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

<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>
于 2012-12-16T03:20:49.570 回答
0

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


[ERROR] COMPILATION ERROR :

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

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

javac: invalid flag: -s
...

你用的是java 5吗?

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

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

于 2012-05-04T12:59:34.780 回答
0

我一直在使用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>

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

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

于 2014-09-24T07:58:40.807 回答