【问题标题】:Setup Java 6 annotation processing configuration for eclipse compiler with maven使用 Maven 为 Eclipse 编译器设置 Java 6 注释处理配置
【发布时间】:2010-12-01 15:24:19
【问题描述】:

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

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

  • 在 factorypath 文件中引用处理器 jar
  • org.eclipse.jdt.apt.core.prefs中配置eclipse注解处理器输出目录(org.eclipse.jdt.apt.genSrcDir属性)
  • 将 eclipse 注释处理器输出目录添加为源文件夹

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

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

【问题讨论】:

    标签: java eclipse maven-2 annotations javac


    【解决方案1】:

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

    您可以将目标配置为作为构建的一部分运行,如下所示:

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

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

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

    【讨论】:

    • 已经打不开了,2.2已经实现了。
    【解决方案2】:

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

    • 在编译期间生成源代码(如下配置)
    • “手动”生成源到 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>
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      猜你喜欢
      • 2011-04-08
      • 1970-01-01
      • 2017-09-10
      • 2012-04-30
      • 1970-01-01
      • 2020-10-14
      • 2017-03-01
      • 1970-01-01
      • 2016-08-17
      相关资源
      最近更新 更多