代码之家  ›  专栏  ›  技术社区  ›  Thomas Jung

用Maven设置Eclipse编译器的Java 6注释处理配置

  •  16
  • Thomas Jung  · 技术社区  · 15 年前

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

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

    • 引用FactoryPath文件中的处理器JAR
    • 配置Eclipse注释处理器输出目录 (org.eclipse.jdt.apt.genSrcDir 属性 org.eclipse.jdt.apt.core.prefs )
    • 添加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>
    
    3 回复  |  直到 11 年前
        1
  •  6
  •   Betlista    12 年前

    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

        2
  •  3
  •   Betlista    12 年前

    http://code.google.com/p/maven-annotation-plugin/

           <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
  •  2
  •   sinuhepop    11 年前