Configuring native unit testing projects for Maven

Pre-requisite: To install the required resources into your Maven repository, you will need a full version of Maven installed and on the PATH.
Unlike COBOL JVM projects, native COBOL and native unit testing projects are not required to be converted to Maven-based projects to be incorporated into your Maven-based lifecycles; instead, you manually add a pom.xml file and configure it for the maven-antrun-plugin, which acts as a wrapper around your Ant-based build processes.
  1. From an Enterprise Developer command prompt, install some additional dependencies into your Maven repository:
    Note: These substeps are only required to be run once. When the required .jar files, which are supplied as part of Enterprise Developer, are part of your Maven repository, you can utilize them with subsequent native COBOL and native unit testing projects.
    1. Set the following environment variables:
      export COBDIR=/opt/microfocus/VisualCOBOL/bin/cobsetenv
      export COBOL_VERSION=<product-version>

      where <product-version> is the version of your current product; for example: 8.0.0, 9.0.0, etc...

      Note: If you have installed your product into a non-default directory, adjust the path accordingly.
    2. Install mfant.jar into your Maven repository:
      mvn install:install-file -Dfile="$COBDIR/lib/mfant.jar" -DgroupId=com.microfocus.cobol.build -DartifactId=mfant -Dversion=$COBOL_VERSION -Dpackaging=jar
    3. Install mfunit.jar into your Maven repository:
      mvn install:install-file -Dfile="$COBDIR/lib/mfunit.jar" -DgroupId=com.microfocus.cobol.rts -DartifactId=mfunit -Dversion=$COBOL_VERSION -Dpackaging=jar
      After two successful builds, both .jar files are installed in your Maven repository ( /home/user-name/.m2, by default).
  2. Add a POM file to your native COBOL unit testing project:
    1. From COBOL Explorer, right-click the required project, point to New and select File.

      The Create New File dialog box is displayed.

    2. In the File name field, type pom.xml, and then click Finish.

      The file is opened in the editor.

  3. Edit the pom.xml to work with your project:
    1. Add the base tags to identify the file as a POM, and then amend the example artifact details, as appropriate:
      <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. Before the <build> section, insert the <properties> section:
      <properties>
       <mfurun.executable>cobmfurun64</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.so</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. If required, update the following properties, substituting the example values given above for ones for ones that match your configuration:
      Property Description
      <test.project.output.dir> The output directory for your unit testing project.
      <test.project.output.artifact> The name of the output file.
      <project.under.test.output.dir> The output directory of the project under test.
      <test.results.dir> The directory in which to output the test results. This is relative to the project directory.
      <test.results.junit.package> The package name for the test results.
    4. Within the <build> tags, add the phases/goals, and dependency details:
      <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. Press Ctrl + S to save the file.
    When the plugins are successfully installed and correctly referenced as dependencies in the pom.xml, you should be able to build the project and produce the expected build artifacts.
  4. Right-click the POM file in the editor, and select Run As > Maven install, or Run As > Maven test.

    A build is invoked and the results of the build are shown in <project.output.dir>.

    Note: These options must be selected from the editor context menu; they do not work from the views in the left-hand pane.

    If you are running Maven test, ensure that the project under test has already been built using Maven. On completion of the test run, the location specified in <test.results.dir> contains the result of the tests.

A sample POM file, similar to the one just created, is available from Example POM file.