Setup Java 6 annotation processing configuration for eclipse compiler with maven

谁说我不能喝 提交于 2019-12-20 08:45:30

问题


What's the best way to setup the eclipse project compiler configuration for Java 6 annotation processors?

My solution is to setup the org.eclipse.jdt.apt.core.prefs and factorypath files manually. This is a bit cumbersome:

  • Reference the processor jar in the factorypath file
  • Configure the eclipse annotation processor output directory (org.eclipse.jdt.apt.genSrcDir property in org.eclipse.jdt.apt.core.prefs)
  • Add the eclipse annotation processor output directory as source folder

One problem is that eclipse generated sources will be compiled with maven. Only maven clean compile is reliable as it removes the eclipse generated source files. (Eclipse and javac generated source files could be out of sync.)

Is there are better solution to configure maven without eclipse generated source files at the maven source path?

<project>
  <properties>
    <eclipse.generated.src>${project.build.directory}/eclipse</eclipse.generated.src>
  </properties>
  <build>
      <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>1.4</version>
            <executions>
                <execution>
                  <id>add-source</id>
                  <phase>generate-sources</phase>
                  <goals> <goal>add-source</goal> </goals>
                  <configuration>
                      <sources>
                        <source>${eclipse.generated.src}</source>
                      </sources>
                    </configuration>
              </execution>
            </executions>
          </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-eclipse-plugin</artifactId>
        <configuration>
          <additionalConfig>
            <file> <name>.factorypath</name>
        <content><![CDATA[<factorypath>
  <factorypathentry kind="VARJAR" id="M2_REPO/processor/processor.jar" enabled="true" runInBatchMode="false"/>
  </factorypath>
  ]]>      </content>
            </file>
            <file>
              <name>.settings/org.eclipse.jdt.apt.core.prefs</name>
        <content><![CDATA[
  eclipse.preferences.version=1
  org.eclipse.jdt.apt.aptEnabled=true
  org.eclipse.jdt.apt.genSrcDir=${eclipse.generated.src}
  org.eclipse.jdt.apt.reconcileEnabled=true
   ]]>     </content>
            </file>
          </additionalConfig>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

回答1:


Update: You could try using the apt-maven-plugin. It currently provides three goals:

  • apt-process Executes apt on project sources.
  • apt:test-process Executes apt on project test sources.
  • apt:eclipse Generates Eclipse files for apt integration.

You can configure the goals to run as part of your build as follows:

<build>
  ...
  <plugins>
    ...
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>apt-maven-plugin</artifactId>
      <version>1.0-alpha-2</version>
      <executions>
        <execution>
          <goals>
            <goal>process</goal>
            <goal>test-process</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
    ...
  </plugins>
  ...
</build>

By default the output directory is set to ${project.build.directory}/generated-sources/apt,

There is an open Jira against the compiler plugin to add APT support for Java 6, you can go and vote for it if this is something you want to to see in future versions.




回答2:


I am using http://code.google.com/p/maven-annotation-plugin/ wich is simpler to configure. You can use it in two ways:

  • generate sources during compilation (configuration below)
  • generate sources "by hand" to src/main/generated and keep them on SCM
       <plugin>
            <groupId>org.bsc.maven</groupId>
            <artifactId>maven-processor-plugin</artifactId>
            <executions>
                <execution>
                    <id>process</id>
                    <goals>
                        <goal>process</goal>
                    </goals>
                    <phase>generate-sources</phase>
                    <configuration>
                        <compilerArguments>-encoding ${project.build.sourceEncoding}</compilerArguments>
                    </configuration>
                </execution>
            </executions>
        </plugin>



       <plugin>
            <groupId>org.bsc.maven</groupId>
            <artifactId>maven-processor-plugin</artifactId>
            <executions>
                <execution>
                    <id>process-test</id>
                    <goals>
                        <goal>process-test</goal>
                    </goals>
                    <phase>generate-test-sources</phase>
                    <configuration>
                        <compilerArguments>-encoding ${project.build.sourceEncoding}</compilerArguments>
                    </configuration>
                </execution>
            </executions>
        </plugin>


      <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.1</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
                <encoding>${project.build.sourceEncoding}</encoding>
                <!-- Disable annotation processors during normal compilation -->
                <compilerArgument>-proc:none</compilerArgument>
            </configuration>
        </plugin>



回答3:


There is a simpler solution in Eclipse Juno (I'm not sure about previous versions). In Preferences / Maven / Annotation Processing there are three modes for annotation processing. It is disabled by default, but I've tested both other options and worked like a charm for me. This way, you don't need to configure APT processing for every project or modify its pom.xml.



来源:https://stackoverflow.com/questions/1480912/setup-java-6-annotation-processing-configuration-for-eclipse-compiler-with-maven

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!