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

时间:2009-10-19 10:54:31

标签: java maven maven-2 annotation-processing

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

7 个答案:

答案 0 :(得分:5)

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

提交补丁:

答案 1 :(得分:5)

不完全是您的问题的答案,但感兴趣的是:

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

我担心在Maven中使用JSR 269存在许多问题,至少使用默认的编译器插件。

答案 2 :(得分:4)

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

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

  2. 使用maven-compiler-pluginmaven-build-helper-pluginUsage中的target/classes将来源汇编到<includes>时包含生成的来源?

  3. 编辑: xxx.annotation.EnforceJavaBeansConventionsProcessor位于何处?您是否需要在{{3}}页面上记录的maven-annotation-plugin配置中添加dependencies

    <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的子目录。

答案 3 :(得分:1)

这样做的一个原因可能是$ JAVA_HOME指向jdk 1.5版而不是1.6版。 (在Windows上检查%JAVA_HOME%当然)

答案 4 :(得分:0)

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


[ERROR] COMPILATION ERROR :

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

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

javac: invalid flag: -s
...

您使用的是 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 应该可以工作: - )

答案 5 :(得分:0)

我在GWTP源代码生成APT处理注释时出现此问题

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

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

答案 6 :(得分: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 的默认值>进程目标分别是目标/生成源目标/类。)

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