eclipse 使用maven为eclipse编译器设置Java 6注解处理配置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1480912/
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-09-19 14:02:42  来源:igfitidea点击:

Setup Java 6 annotation processing configuration for eclipse compiler with maven

javaeclipsemaven-2annotationsjavac

提问by Thomas Jung

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

为 Java 6 注释处理器设置 eclipse 项目编译器配置的最佳方法是什么?

My solution is to setup the org.eclipse.jdt.apt.core.prefsand factorypathfiles manually. This is a bit cumbersome:

我的解决方案是手动设置org.eclipse.jdt.apt.core.prefsfactorypath文件。这有点麻烦:

  • Reference the processor jar in the factorypath file
  • Configure the eclipse annotation processor output directory (org.eclipse.jdt.apt.genSrcDirproperty in org.eclipse.jdt.apt.core.prefs)
  • Add the eclipse annotation processor output directory as source folder
  • 在 factorypath 文件中引用处理器 jar
  • (org.eclipse.jdt.apt.genSrcDirorg.eclipse.jdt.apt.core.prefs) 中配置 eclipse 注释处理器输出目录属性
  • 将eclipse注解处理器输出目录添加为源文件夹

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

一个问题是 eclipse 生成的源代码会用 maven 编译。Onlymaven clean compile是可靠的,因为它删除了 eclipse 生成的源文件。(Eclipse 和 javac 生成的源文件可能不同步。)

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

是否有更好的解决方案来配置 maven 而无需在 maven 源路径中生成 eclipse 生成的源文件?

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

采纳答案by Rich Seller

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

更新:您可以尝试使用apt-maven-plugin。它目前提供三个目标:

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,

默认情况下,输出目录设置为${project.build.directory}/generated-sources/apt

There is an open Jiraagainst 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.

有一个针对编译器插件的开放 Jira来添加对 Java 6 的 APT 支持,如果您想在未来的版本中看到这一点,您可以去投票支持它。

回答by Piotr Gwiazda

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

我正在使用http://code.google.com/p/maven-annotation-plugin/配置更简单。您可以通过两种方式使用它:

  • generate sources during compilation (configuration below)
  • generate sources "by hand" to src/main/generated and keep them on SCM
  • 在编译期间生成源代码(配置如下)
  • 将源“手动”生成到 src/main/generated 并将它们保存在 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>

回答by sinuhepop

There is a simpler solution in Eclipse Juno (I'm not sure about previous versions). In Preferences / Maven / Annotation Processingthere 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.

Eclipse Juno 中有一个更简单的解决方案(我不确定以前的版本)。在Preferences / Maven / Annotation Processing 中,有三种注释处理模式。默认情况下它是禁用的,但我已经测试了其他两个选项并且对我来说就像一个魅力。这样,您就不需要为每个项目配置 APT 处理或修改其 pom.xml。