首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用maven为eclipse编译器设置Java 6批注处理配置

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

Stack Overflow用户
提问于 2009-09-26 10:07:56
回答 3查看 11.6K关注 0票数 16

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

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

org.eclipse.jdt.apt.core.prefs)

  • Add中引用处理器jar在路径文件中配置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>
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2009-09-26 12:18:36

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

生成Eclipse文件

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

<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来增加对Java6的APT支持,如果你想在未来的版本中看到这一点,你可以去投票支持它。

票数 6
EN

Stack Overflow用户

发布于 2010-09-22 14:52:30

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

主源在编译期间生成(手动配置below)

  • generate源到src//
  • 并将其保存在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
EN

Stack Overflow用户

发布于 2013-01-10 23:13:00

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1480912

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档