Maven 用のネイティブ単体テスト プロジェクトの構成

Pre-requisite: 必要なリソースを Maven リポジトリにインストールするには、通常版の Maven がインストールされて PATH にある必要があります。
COBOL JVM プロジェクトとは異なり、ネイティブ COBOL プロジェクトおよびネイティブ単体テスト プロジェクトは、Maven ベースのライフサイクルに組み込むために Maven ベースのプロジェクトに変換する必要はありません。代わりに、pom.xml ファイルを手動で追加し、Ant ベースのビルド プロセスのラッパーとして機能する maven-antrun-plugin 用に構成します。
  1. Enterprise Developer コマンド プロンプトから、いくつかの追加の依存関係を Maven リポジトリにインストールします。
    注: これらのサブステップは、一度だけ実行する必要があります。Enterprise Developer の一部として提供される必要な .jar ファイルを Maven リポジトリに含めることで、それらを後続のネイティブ COBOL プロジェクトおよびネイティブ単体テスト プロジェクトで利用できます。
    1. 次の環境変数を設定します。
      set COBDIR=C:\Program Files (x86)\Micro Focus\Enterprise Developer
      set COBOL_VERSION=8.0.0
      注: 製品をデフォルト以外のディレクトリにインストールした場合は、それに応じてパスを調整してください。
    2. mfant.jar を Maven リポジトリにインストールします。
      mvn install:install-file -Dfile="%COBDIR%\bin\mfant.jar" -DgroupId=com.microfocus.cobol.build -DartifactId=mfant -Dversion=%COBOL_VERSION% -Dpackaging=jar
    3. mfunit.jar を Maven リポジトリにインストールします。
      mvn install:install-file -Dfile="%COBDIR%\bin\mfunit.jar" -DgroupId=com.microfocus.cobol.rts -DartifactId=mfunit -Dversion=%COBOL_VERSION% -Dpackaging=jar
      両方のビルドが成功すると、2 つの .jar ファイルが Maven リポジトリ (デフォルトでは %USERPROFILE%\.m2) にインストールされます。
  2. POM ファイルをネイティブ COBOL 単体テスト プロジェクトに追加します。
    1. COBOL エクスプローラーで、必要なプロジェクトを右クリックし、[New] をポイントして [File] を選択します。

      [Create New File] ダイアログ ボックスが表示されます。

    2. [File name] フィールドに pom.xml と入力し、[Finish] をクリックします。

      ファイルがエディターで開きます。

  3. プロジェクトで使用できるように pom.xml を編集します。
    1. ファイルを POM として識別するベース タグを追加し、例の成果物の詳細を必要に応じて修正します。
      <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
       <modelVersion>4.0.0</modelVersion>
       <groupId>com.mfcobolbook.cobol</groupId>
       <artifactId>MyMavenUnitTestProject</artifactId>
       <version>1.0.0</version>
       <build>
       </build>
      </project>
    2. <build> セクションの前に <properties> セクションを挿入します。
      <properties>
       <mfurun.executable>mfurun</mfurun.executable>
       <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
       <test.project.output.dir>${project.basedir}/New_Configuration.bin</test.project.output.dir>
       <test.project.output.artifact>MyMavenUnitTestProject.dll</test.project.output.artifact>
       <project.under.test.output.dir>${project.basedir}/../MyMavenProject/New_Configuration.bin</project.under.test.output.dir>
       <test.results.dir>test-results</test.results.dir>
       <test.results.junit.package>native.MyTestResults.testcases</test.results.junit.package>
      </properties>
    3. 次のプロパティを更新し、上記の例で指定されている値を構成に応じた値に置き換えます。
      プロパティ 説明
      <test.project.output.dir> 単体テスト プロジェクトの出力ディレクトリ。
      <test.project.output.artifact> 出力ファイルの名前。
      <project.under.test.output.dir> テスト対象のプロジェクトの出力ディレクトリ。
      <test.results.dir> テスト結果を出力するディレクトリ。これは、プロジェクト ディレクトリを基準とする相対パスです。
      <test.results.junit.package> テスト結果のパッケージの名前。
    4. <build> タグの間にフェーズ/ゴールおよび依存関係の詳細を追加します。
      <plugins>
       <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-antrun-plugin</artifactId>
          <version>3.0.0</version>
          <executions>
           <execution>
            <id>build</id>
              <phase>compile</phase>
               <configuration>
                <target>
                  <ant antfile="${project.basedir}/.cobolBuild" target="cobolbuild" />
                </target>
               </configuration>
               <goals>
                <goal>run</goal>
               </goals>
            </execution>
           <execution>
            <id>test</id>
            <phase>test</phase>
              <configuration>
               <target>
                <!-- Generate an mfu file -->
                <exec dir="${test.project.output.dir}" executable="${mfurun.executable}">
                  <arg value="-generate-mfu ${test.project.output.artifact}"/>
                </exec>
                <!-- Run the tests -->
                <taskdef name="mfurun" classname="com.microfocus.mfunit.ant.MFURunMFUTask"/>
                 <mfurun printsummary="true" junitresults="true" junitreportpackage="${test.results.junit.package}" outputdir="${test.results.dir}">
                  <fileset dir="${test.project.output.dir}" includes="*.mfu" />
                  <env key="COBPATH" value="${project.under.test.output.dir}" />
                 </mfurun>
               </target>
              </configuration>
              <goals>
               <goal>run</goal>
              </goals>
           </execution>
           <execution>
            <id>clean</id>
            <phase>clean</phase>
             <configuration>
              <target>
               <ant antfile="${project.basedir}/.cobolBuild" target="clean" />
              </target>
             </configuration>
             <goals>
              <goal>run</goal>
             </goals>
            </execution>
           </executions>
           <dependencies>
            <dependency>
             <groupId>ant-contrib</groupId>
             <artifactId>ant-contrib</artifactId>
             <version>1.0b3</version>
             <exclusions>
              <exclusion>
               <groupId>ant</groupId>
               <artifactId>ant</artifactId>
              </exclusion>
             </exclusions>
            </dependency>
            <dependency>
              <groupId>com.microfocus.cobol.build</groupId>
              <artifactId>mfant</artifactId>
              <version>8.0.0</version>
             </dependency>
             <dependency>
              <groupId>com.microfocus.cobol.rts</groupId>
              <artifactId>mfunit</artifactId>
              <version>8.0.0</version>
             </dependency>
            </dependencies>
         </plugin>
         <plugin>
           <groupId>org.apache.maven.plugins</groupId>
           <artifactId>maven-install-plugin</artifactId>
           <version>3.0.0-M1</version>
           <configuration>
            <skip>true</skip>
           </configuration>
         </plugin>
        </plugins>
    5. Ctrl + S を押してファイルを保存します。
    プラグインが正常にインストールされ、pom.xml で依存関係として正しく参照されると、プロジェクトをビルドし、想定されるビルド成果物を生成できるようになります。
  4. エディターで POM ファイルを右クリックし、[Run As > Maven install] または [Run As > Maven test] を選択します。 ビルドが呼び出され、ビルドの結果が <project.output.dir> に表示されます。

    [Maven test] を実行する場合は、テスト対象のプロジェクトがすでに Maven を使用してビルドされていることを確認してください。テストの実行が完了すると、<test.results.dir> で指定された場所にテストの結果が格納されます。

ここで作成したものと同様のサンプル POM ファイルが「POM ファイルの例」に用意されています。