Setting the generated source directory for annotation processors in Maven

前端 未结 7 1371
南方客
南方客 2021-02-09 20:01

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:

    <         


        
相关标签:
7条回答
  • 2021-02-09 20:36

    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)

    0 讨论(0)
  • 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 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"
    

    now, mvn install should work :-)

    0 讨论(0)
  • 2021-02-09 20:37

    I may be missing something but shouldn't you:

    1. Generate sources in target/generated-sources/annotation-processing during the generate-sources phase? The apt-maven-plugin or the maven-annotation-plugin could help.

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

    EDIT: Where is xxx.annotation.EnforceJavaBeansConventionsProcessor located? Don't you need to add dependencies to the configuration of the maven-annotation-plugin as documented on the Usage page?

    <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/generated as output directory but rather a subdirectory of target/generated-sources.

    0 讨论(0)
  • 2021-02-09 20:38

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

    Submitted patches:

    • system dependent path separator character
    • Allow the default processor discovery mechanism to run
    0 讨论(0)
  • 2021-02-09 20:38

    I've been using the maven-processor-plugin as follows:

    <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 process goal is target/generated-sources and target/classes, respectively).

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

    0 讨论(0)
  • 2021-02-09 20:41

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

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

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

    0 讨论(0)
提交回复
热议问题