18
17

More than 5 years have passed since last update.

Antビルドでeclipseコンパイラを用いる方法

Posted at

eclipseのコンパイラを使ったjavacコンパイルについて、以下の2つの方法を試してみました。

1. build.xmlをエクスポート

 ・修正する元となるbuild.xmlは、プロジェクトを右クリックし、「エクスポート」を
  選択する。
 ・「一般」から「Antビルド・ファイル」を選択し、「次へ」ボタンを押下する。
 ・「完了」ボタンを押下する。

2. eclipse上からAntビルドを実行

①build.xmlを以下のように更新する。

build.xml
<property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/>

②Antビルドの設定を行う。
 ・build.xml上で右クリックし、「実行」を選択する。
 ・Antビルドの実行構成ダイアログが開くので、「JRE」タブを開き、
  「ワークスペースと同じJREで実行」を選択する。

③②で作成したAntビルド・ファイルを実行する。

3. WindowsのバッチファイルからAntビルドを実行

eclipseを起動しなくても、Windows環境でコンパイル実行する場合はバッチファイルにして実行するのが簡単です。

①ディレクトリ階層は、以下のような構成を想定とします。

ディレクトリ階層
project-root
   |- build.xml
   |- sample.bat
   |- ant-lib

②eclipseを使用している場合、インストールディレクトリから以下の2つのjarを取得し、任意のディレクトリ(下記では、project-root/ant-lib)に配置します。

  • org.eclipse.jdt.core_xxx.jar (xxxは使用しているeclipseのバージョンに依存)
  • jdtCompilerAdapter.jar

③build.xmlは、以下のようにeclipseのコンパイラを使用するように設定します。

build.xml
<project default="build">
     <typedef name="ecj" classname="org.eclipse.jdt.core.JDTCompilerAdapter">
          <classpath>
               <fileset dir="ant-lib" includes="*.jar" />
          </classpath>
     </typedef>
     <target name="build">
          <delete dir="compiled" />
          <mkdir dir="compiled" />
          <javac srcdir="src" destdir="compiled" compiler="org.eclipse.jdt.core.JDTCompilerAdapter">
               <ecj />
          </javac>
     </target>
</project>

④Windowsバッチファイルから、以下のようにAntを実行します。

sample.bat
ant [-buildfile ファイル名※1] [実行するtarget※2]
例)
ant -buildfile build_up.xml clean build

※1 build.xmlとは別に作成した「build_up.xml」実行したい場合
※2 build_up.xmlのtargetにclean、buildがあり、複数同時に指定する場合

◆参考にさせて頂いたサイト
AntでEclipseのECJ(Eclipse Compiler for Java)を使う

18
17
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
17