16

Java 6アノテーションプロセッサ用のeclipseプロジェクトコンパイラ設定をセットアップするための最良の方法は何ですか?

私の解決策は、org.eclipse.jdt.apt.core.prefsfactorypathファイルを手動で設定することです。これは少し面倒です:

  • factorypathファイルでプロセッサjarを参照します
  • でEclipseアノテーションプロセッサの出力ディレクトリ(org.eclipse.jdt.apt.genSrcDirプロパティを設定しますorg.eclipse.jdt.apt.core.prefs
  • Eclipseアノテーションプロセッサの出力ディレクトリをソースフォルダとして追加します

1つの問題は、Eclipseで生成されたソースがMavenでコンパイルされることです。maven clean compileEclipseで生成されたソースファイルを削除するため、信頼できるのは1つだけです。(Eclipseとjavacで生成されたソースファイルが同期していない可能性があります。)

MavenソースパスでEclipseによって生成されたソースファイルなしでMavenを構成するためのより良い解決策はありますか?

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

3 に答える 3

6

更新: apt-maven-pluginを使用してみてください。現在、次の 3 つの目標を提供しています。

  • apt-processプロジェクト ソースに対して apt を実行します。
  • apt:test-processプロジェクト テスト ソースで apt を実行します。
  • apt:eclipse apt 統合用の 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

Java 6 の APT サポートを追加するために、コンパイラ プラグインに対してオープンな Jiraがあります。これが将来のバージョンで見たいものである場合は、投票してください。

于 2009-09-26T12:18:36.127 に答える
3

私はhttp://code.google.com/p/maven-annotation-plugin/を使用しています。これは構成が簡単です。次の 2 つの方法で使用できます。

  • コンパイル中にソースを生成します (以下の構成)
  • 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>
于 2010-09-22T06:52:30.230 に答える
2

Eclipse Juno には、より単純なソリューションがあります (以前のバージョンについてはわかりません)。Preferences / Maven / Annotation Processingには、アノテーション処理用の 3 つのモードがあります。デフォルトでは無効になっていますが、他のオプションを両方ともテストしたところ、魅力的に機能しました。この方法では、プロジェクトごとに APT 処理を構成したり、その pom.xml を変更したりする必要はありません。

于 2013-01-10T15:13:00.440 に答える