Maven を使用した Eclipse コンパイラーの Java 6 アノテーション処理構成のセットアップ
-
18-09-2019 - |
質問
Java 6 アノテーションプロセッサ用に Eclipse プロジェクトコンパイラ設定をセットアップする最良の方法は何ですか?
私の解決策は、 org.eclipse.jdt.apt.core.prefs
そして factorypath
ファイルを手動で作成します。これは少し面倒です:
- ファクトリーパスファイル内のプロセッサjarを参照します。
- Eclipse アノテーション プロセッサの出力ディレクトリを構成する
(org.eclipse.jdt.apt.genSrcDir
の財産org.eclipse.jdt.apt.core.prefs
) - Eclipse アノテーション プロセッサの出力ディレクトリをソース フォルダーとして追加します。
1 つの問題は、Eclipse で生成されたソースが Maven でコンパイルされることです。のみ maven clean compile
Eclipse で生成されたソース ファイルを削除するため、信頼性が高くなります。(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>
解決
アップデート:を使ってみてはいかがでしょうか apt-maven-プラグイン. 。現在、次の 3 つの目標が提供されています。
- 適切なプロセス プロジェクトソースに対して apt を実行します。
- apt:テストプロセス プロジェクトのテストソースに対して apt を実行します。
- 適切:日食 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
,
があります Jira を開く Java 6 の APT サポートを追加するコンパイラ プラグインに反対して、これが将来のバージョンで期待されるものである場合は、投票してください。
他のヒント
使っています 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>
Eclipseのジュノ(私は、以前のバージョンについてはよく分からない)での簡単な解決策があります。 の設定/ Mavenの/注釈処理のでは注釈処理のための3つのモードがあります。これは、デフォルトでは無効になっていますが、私は両方の他のオプションをテストし、私にとっては魅力のように働いてきました。このように、あなたはすべてのプロジェクトのためのAPT処理を設定するか、そののpom.xmlを変更する必要はありません。