APEX Developer Guide

Build APEX from Source

Introduction to building APEX

APEX is written 100% in Java and uses Apache Maven as the build system. The requirements for building APEX are:
  • An installed Java development kit for Java version 8 or higher

  • Maven 3

    • To get Maven 3 running please follow the guidelines for Download and Install, and Run Maven
  • A clone of the APEX source repositories

To get a clone of the APEX source repositories, please see the APEX Installation Guide or the APEX User manual.
One all requirements are in place, APEX can be build. There are several different artifacts one can create building APEX, most of them defined in their own profile. APEX can also be build in a standard way with standard tests (mvn clean install) or without standard tests (mvn clean install -DskipTests).
The examples in this document assume that the APEX source repositories are cloned to:
  • Unix, Cygwin: /usr/local/src/apex
  • Windows: C:\dev\apex
  • Cygwin: /cygdrive/c/dev/apex

Important

A Build requires ONAP Nexus APEX has a dependency to ONAP parent projects. You might need to adjust your Maven M2 settings. The most current settings can be found in the ONAP oparent repo: Settings.

Important

A Build needs Space Building APEX requires approximately 2-3 GB of hard disc space, 1 GB for the actual build with full distribution and 1-2 GB for the downloaded dependencies

Important

A Build requires Internet (for first build to download all dependencies and plugins) During the build, several (a lot) of Maven dependencies will be downloaded and stored in the configured local Maven repository. The first standard build (and any first specific build) requires Internet access to download those dependencies.

Important

Building RPM distributions RPM images are only build if the rpm package is installed (Unix). To install rpm run sudo apt-get install rpm, then build APEX.

Standard Build

Use Maven to for a standard build without any tests.
Unix, Cygwin Windows
>c:
>cd \dev\apex
>mvn clean install -DskipTests
# cd /usr/local/src/apex
# mvn clean install -DskipTests
The build takes about 6 minutes on a standard development laptop. It should run through without errors, but with a lot of messages from the build process.
When Maven is finished with the build, the final screen should look similar to this (omitting some success lines):
 1 [INFO] tools .............................................. SUCCESS [  0.248 s]
 2 [INFO] tools-common ....................................... SUCCESS [  0.784 s]
 3 [INFO] simple-wsclient .................................... SUCCESS [  3.303 s]
 4 [INFO] model-generator .................................... SUCCESS [  0.644 s]
 5 [INFO] packages ........................................... SUCCESS [  0.336 s]
 6 [INFO] apex-pdp-package-full .............................. SUCCESS [01:10 min]
 7 [INFO] Policy APEX PDP - Docker build 2.0.0-SNAPSHOT ...... SUCCESS [ 10.307 s]
 8 [INFO] ------------------------------------------------------------------------
 9 [INFO] BUILD SUCCESS
10 [INFO] ------------------------------------------------------------------------
11 [INFO] Total time: 03:43 min
12 [INFO] Finished at: 2018-09-03T11:56:01+01:00
13 [INFO] ------------------------------------------------------------------------
The build will have created all artifacts required for an APEX installation. The following example show how to change to the target directory and how it should look like.
Unix, Cygwin
 1 # cd packages/apex-pdp-package-full/target
 2 # ls -l
 3 -rwxrwx---+ 1 esvevan Domain Users       772 Sep  3 11:55 apex-pdp-package-full_2.0.0~SNAPSHOT_all.changes*
 4 -rwxrwx---+ 1 esvevan Domain Users 146328082 Sep  3 11:55 apex-pdp-package-full-2.0.0-SNAPSHOT.deb*
 5 -rwxrwx---+ 1 esvevan Domain Users     15633 Sep  3 11:54 apex-pdp-package-full-2.0.0-SNAPSHOT.jar*
 6 -rwxrwx---+ 1 esvevan Domain Users 146296819 Sep  3 11:55 apex-pdp-package-full-2.0.0-SNAPSHOT-tarball.tar.gz*
 7 drwxrwx---+ 1 esvevan Domain Users         0 Sep  3 11:54 archive-tmp/
 8 -rwxrwx---+ 1 esvevan Domain Users        89 Sep  3 11:54 checkstyle-cachefile*
 9 -rwxrwx---+ 1 esvevan Domain Users     10621 Sep  3 11:54 checkstyle-checker.xml*
10 -rwxrwx---+ 1 esvevan Domain Users       584 Sep  3 11:54 checkstyle-header.txt*
11 -rwxrwx---+ 1 esvevan Domain Users        86 Sep  3 11:54 checkstyle-result.xml*
12 drwxrwx---+ 1 esvevan Domain Users         0 Sep  3 11:54 classes/
13 drwxrwx---+ 1 esvevan Domain Users         0 Sep  3 11:54 dependency-maven-plugin-markers/
14 drwxrwx---+ 1 esvevan Domain Users         0 Sep  3 11:54 etc/
15 drwxrwx---+ 1 esvevan Domain Users         0 Sep  3 11:54 examples/
16 drwxrwx---+ 1 esvevan Domain Users         0 Sep  3 11:55 install_hierarchy/
17 drwxrwx---+ 1 esvevan Domain Users         0 Sep  3 11:54 maven-archiver/
Windows
 1 >cd packages\apex-pdp-package-full\target
 2 >dir
 3 
 4 03/09/2018  11:55    <DIR>          .
 5 03/09/2018  11:55    <DIR>          ..
 6 03/09/2018  11:55       146,296,819 apex-pdp-package-full-2.0.0-SNAPSHOT-tarball.tar.gz
 7 03/09/2018  11:55       146,328,082 apex-pdp-package-full-2.0.0-SNAPSHOT.deb
 8 03/09/2018  11:54            15,633 apex-pdp-package-full-2.0.0-SNAPSHOT.jar
 9 03/09/2018  11:55               772 apex-pdp-package-full_2.0.0~SNAPSHOT_all.changes
10 03/09/2018  11:54    <DIR>          archive-tmp
11 03/09/2018  11:54                89 checkstyle-cachefile
12 03/09/2018  11:54            10,621 checkstyle-checker.xml
13 03/09/2018  11:54               584 checkstyle-header.txt
14 03/09/2018  11:54                86 checkstyle-result.xml
15 03/09/2018  11:54    <DIR>          classes
16 03/09/2018  11:54    <DIR>          dependency-maven-plugin-markers
17 03/09/2018  11:54    <DIR>          etc
18 03/09/2018  11:54    <DIR>          examples
19 03/09/2018  11:55    <DIR>          install_hierarchy
20 03/09/2018  11:54    <DIR>          maven-archiver
21                8 File(s)    292,652,686 bytes
22                9 Dir(s)  14,138,720,256 bytes free

Checkstyle with Maven

The codestyle for all APEX java projects can be checked automatically. The checks include empty or non-existing Javadocs. Any checkstyle run should complete without any errors, some warnings are acceptable.
To run checkstyle on an APEX Maven project use:
mvn checkstyle:check
To run checkstyle on all modules use:
mvn checkstyle:checkstyle -DapexAll

Build with standard Tests

Use Maven to for a standard build with standard tests.

Important

Some tests have specific timing Requirements
Some of the tests have very specific timing requirements. If run on a low-powered build machine, or if the build machine is on high load, those tests might fail and the whole build might fail as well. If this happens, reduce the load on your build machine and restart the build.
Unix, Cygwin Windows
1 >c:
2 >cd \dev\apex
3 >mvn clean install
1 # cd /usr/local/src/apex
2 # mvn clean install
The build takes about 10 minutes with tests on a standard development laptop. It should run through without errors, but with a lot of messages from the build process. If build with tests (i.e. without -DskipTests), there will be error messages and stack trace prints from some tests. This is normal, as long as the build finishes successful.

Build with all Tests

Use Maven to for a standard build with all tests.

Important

Some tests have specific timing Requirements.
Some of the tests have very specific timing requirements. If run on a low-powered build machine, or if the build machine is on high load, those tests might fail and the whole build might fail as well. If this happens, reduce the load on your build machine and restart the build.

Important

Might require specific software.
When running all tests, some modules require specific software installed on the build machine. For instance, testing the full capabilities of context (with distribution and persistence) will require Hazelcast and Infinispan installed on the build machine.
Unix, Cygwin Windows
1 >c:
2 >cd \dev\apex
3 >mvn clean install -DallTests
1 # cd /usr/local/src/apex
2 # mvn clean install -DallTests

Build with all Components

A standard APEX build will not build all components. Some parts are for specific deployments, only. Use Maven to for a standard build with all components.

Important

Might require specific software.
When building all components, some modules require specific software installed on the build machine.
Unix, Cygwin Windows
1 >c:
2 >cd \dev\apex
3 >mvn clean install -DapexAll
1 # cd /usr/local/src/apex
2 # mvn clean install -DapexAll

Build the APEX Documentation

The APEX Maven build also includes stand-alone documentations, such as the HowTo documents, the Installation Guide, and the User Manual. Use Maven to build the APEX Documentation. The Maven options -N prevents Maven to go through all APEX modules, which is not necessary for the documentation. The final documents will be in target/generated-docs (Windows: target\generated-docs). The HTML documents are in the html/ folder, the PDF documents are in the pdf/ folder. Once the documentation is build, copy the HTML and PDF documents to a folder of choice
Unix, Cygwin Windows
1 >c:
2 >cd \dev\apex
3 >mvn clean generate-resources -N -DapexDocs
1 # cd /usr/local/src/apex
2 # mvn clean generate-resources -N -DapexDocs

Build APEX Site

The APEX Maven build comes with full support to build a web site using Maven Site. Use Maven to build the APEX Site. Stage the APEX web site. The target folder for the staged site is
  • Unix: /usr/local/src/apex/target/ad-site
  • Windows: C:\dev\apex\target\ad-site
  • Cygwin: /cygdrive/c/dev/apex/target/ad-site
Once the web site is staged, copy the full site to a folder of choice or into a web server.

Important

Building a Site takes Time.
Building and staging the APEX web site can take very long. The stand-alone documentation will take about 2 minutes. The sites for all modules and projects and the main APEX site can take between 10-30 minutes depending on your build machine (~10 minutes without generating source and test-source reports, closer to 30 minutes with all reports).
Start the build deleting the staging directory that might have been created by a previous site build. Then go to the APEX packaging directory.
Unix Windows Cygwin
1 cd /usr/local/src/apex
2 rm -fr target/ad-site
1 c:
2 cd \dev\apex
3 rmdir /s/q target\ad-site
1 cd /cygdrive/c/dev/apex
2 rm -fr target/ad-site
the workflow for building a complete site then is as follows:
mvn clean -DapexAll (1)
mvn install -DskipTests (2)
mvn generate-resources -N -DapexDocs (3)
mvn initialize site:attach-descriptor site site:stage -DapexSite (4)
  1. First clean all modules to remove any site artifacts, use the apexXtext profile to make sure these modules are processed as well
  2. Next run a simple install without tests
  3. Now generate the APEX stand -alone documentation, they are in the local package only so we can use the -N switch
  4. Last build the actual sites and stage (copy to the staging directory) with the profile apexSite (do not forget the initialize goal, otherwise the staging directory will not be correctly set and sites are staged in every model in a directory called docs).
If you want to build the site for a particular project for testing, the Maven command is simpler. Since only the main project has APEX documentation (stand-alone), you can use Maven as follow.
mvn clean site -DapexSite
If you want to stage the tested site, then use
mvn clean initialize site:attach-descriptor site site:stage -DapexSite

APEX Codestyle

Introduction: APEX Codestyle

This page describes how to apply a code style to the APEX Java projects. The provided code templates are guidelines and are provided for references and as examples. We will not engage in “holy war” on style for coding. As long as the style of a particular block of code is understandable, consistent, and readable, please feel free to adapt or modify these guides or use other guides as you see fit.
The JAutoDoc and Checkstyle Eclipse Plugins and tools are useful and remove a lot of the tedium from code documentation. Use them to check your code and please fix any issues they identify with your code.
Since APEX is part of ONAP, the general ONAP rules and guideliness for development do apply. Please see ONAP Wiki for details.

Java coding Rules

  • APEX is (in large parts) a platform (or middleware), so Software Design Patterns are a good thing

  • The Solid Principles apply

  • Avoid class fields scoped as protected

    • They break a lot of good design rules, e.g. most SOLID rules
    • For a discussion see this Stackoverflow Question
  • If you absolutely need protected class fields they should be final

  • Avoid default scope for class fields and methods

    • For fields: use public or private (see also above)
    • For methods: use public for general use, protected for specialization using inheritance (ideally final), private for everything else
  • Method parameters that are not changed in the method should be marked final

  • Every package must have a package-info.java file with an appropriate description, minimum a descriptive one liner

  • Every class must have

    • The common header (copyright, file, date)
    • Javadoc header for the class with description of the class and author
    • Javadoc for all public_ fields
    • If possible, Javadoc for private fields, at least some documentation for private fields
    • Javadoc for all methods
  • All project must build with all tests on Unix, Windows, and Cygwin

    • Support all line endings in files, e.g. \n and \r\n
    • Be aware of potential differences in exception messages, if testing against a message
    • Support all types of paths: Unix with /, Windows with an optinal drive C:\ and \, Cygwin with mixed paths

Eclipse Plugin: JAutodoc

This plugin is a helper plugin for writing Javadoc. It will automatically create standard headers on files, create package-info.java files and will put in remarkably good stub Javadoc comments in your code, using class names and method names as hints.
Available from the Eclipse Marketplace. In Eclipse Help→Eclipse Marketplace…​ and type JAutodoc. Select JAutodoc when the search returns and install it.
You must configure JAutoDoc in order to get the most out of it. Ideally JAutoDoc should be configured with templates that cooperate with the inbuilt Eclipse Code Formatter for best results.

Eclipse Plugin: Checkstyle

This plugin integrates Checkstyle into Eclipse. It will check your code and flag any checkstyle issues as warnings in the code.
Available from the Eclipse Marketplace. In Eclipse Help→Eclipse Marketplace…​ and type “Checkstyle”. Select “Checkstyle Plug-in” when the search returns and install it. Note that “Checkstyle Plug-in” may not be the first result in the list of items returned.
For APEX, the ONAP checkstyle rules do apply. The configuration is part of the ONAP parent. See ONAP Git for details and updates. All settings for checkstyle are already part of the code (POM files).

Configure Eclipse

  • Set the template for Eclipse code clean up

    1. Eclipse  Window  Preferences  Java  Code Style Clean Up → Import…​
    2. Select your template file (ApexCleanUpTemplate.xml) and apply it
  • Set the Eclipse code templates

    1. Eclipse  Window  Preferences  Java  Code Style Code Templates → Import…​

    2. Select your templates file (ApexCodeTemplates.xml) and apply it

      • Make sure to set your email address in generated comments by selecting “Comments→Types” in the “Configure generated code and comments:” pane, then change the email address on the @author tag to be your email address
  • Set the Eclipse Formatter profile

    1. Eclipse  Window  Preferences  Java  Code Style Formatter → Import…​
    2. Select your formatter profile file (ApexFormatterProfile.xml) and apply it
The templates mentioned above can be found in apex-model/apex-model.build-tools/src/main/resources/eclipse

Configure JAutodoc (Eclipse)

Import the settings for JAutodoc:
  1. Eclipse  Window  Preferences  Java  JAutodoc → Import All…​ (at bottom of the JAutodoc preferences window)

  2. Leave all the preferences ticked to import all preferences, browse to the JAutodoc setting file (ApexJautodocSettings.xml) and press OK

  3. Set your email address in the package Javadoc template

    • Press Edit Template…​ in the Package Javadoc area of the JAutodoc preferences window, and change the email address on the @author tag to be your email address
  4. Now, apply the JAutodoc settings

The templates mentioned above can be found in apex-model/apex-model.build-tools/src/main/resources/eclipse

Configure Checkstyle (Maven)

When using a custom style configuration with Checkstyle, the definition of that style must of course be available to Checkstyle. In order not to have to distribute style files for checkstyle into all Maven modules, it is recommended that a special Maven module be built that contains the checkstyle style definition. That module is then used as a dependency in the POM for all other modules that wish to use that checkstyle style. For a full explanation see the explanation of Checkstyle multi-module configuration.
For APEX, the ONAP checkstyle rules do apply. The configuration is part of the ONAP parent. See ONAP Git for details and updates.

Run Checkstyle (Maven)

Run Checkstyle using Maven on the command line with the command:
mvn checkstyle:check
On the main APEX project, run a full checkstyle check as:
mvn checkstyle:checkstyle -DapexAll

Configure Checkstyle (Eclipse, globally)

  1. Set up a module with the Checkstyle style files (see above)

  2. In Eclipse  Window  Preferences go to Checkstyle

  3. Import the settings for Checkstyle

    • Press New…​ to create a new Global Check Configurations entry
    • Give the configuration a name such as Apex Checkstyle Configuration and select the External Configuration File form in the Type drop down menu
    • Browse to the Checckstyle setting file (ApexCheckstyleSettings.xml) and press OK
  4. Press OK

    • You may now get an Unresolved Properties found dialogue
    • This is because there is a second Checkstyle configuration file required to check file headers
  5. Press Edit Properties…​ and press Find unresolved properties on the next dialogue window

  6. The plugin will find the ${checkstyle.header.file} property is unresolved and will ask should it be added to the properties, click yes

  7. Now, select the row on the dialogue for the checkstyle.header.file property and click Edit…​

  8. Set the value of the checkstyle.header.file property to <your-apex-git-location>/apex-model/apex-model.build-tools/src/main/resources/checkstyle/apex_header.txt

    • Of course replacing the tag <your-apex-git-location> with the location of your Apex GIT repository
  9. Press OK, OK, OK to back out to the main Checkstyle properties window

  10. Select the Apex Checkstyle Configuration as your default configuration by selecting its line in the Global Check Configuraitons list and clicking Set as Default

  11. Press Apply and Close to finish Checkstyle global configuration

The templates mentioned above can be found in apex-model/apex-model.build-tools/src/main/resources/eclipse

2.10. Configure Checkstyle Blueprint

As well as being configured globally, Checkstyle must be configured and activated for each project in Eclipse. In order to make this process less tedious, set up the first project you apply Checkstye to as a blueprint project and then use this blueprint for all other projects.
  1. Select the project you want to use as a blueprint

    • For example, apex-model.basic-model in apex and enter the project properties by right clicking and selecting Properties
  2. Click Checkstyle on the properties to get the Checkstyle project configuration window

  3. Click the box Checkstyle active for this project and in the Exclude from checking…​ list check the boxes:

    • files outside source directories
    • derived (generated) files
    • files from packages:
  4. Now, in order to turn off checking on resource directories and on JUnit tests

    • Select the line files from packages: in the Exclude from checking…​ list and click Change…​
  5. On the Filter packages dialogue

    • Check all the boxes except the top box, which is the box for src/main/java

    • Ensure that the recursively exclude sub-packages check box is ticked

      • recursively exclude sub-packages
    • Press OK

  6. Press Apply and Close to apply the changes

Use Eclipse Source Operations

Eclipse Source Operations can be carried out on individual files or on all the files in a package but do not recurse into sub-packages. They are available as a menu in Eclipse by selecting a file or package and right clicking on Source. Note that running Clean Up…​ with the Apex clean up profile will run Format and Organize Imports. So if you run a clean up on a file or package, you need not run Format or Organize Imports.
We recommend you use the following Eclipse Source Operations:
  1. Format applies the current format definition to the file or all files in a package

  2. Organize Imports sorts the imports on each file in standard order

  3. Clean Up runs a number of cleaning operations on each file. The Apex clean up template

    • Remove this qualifier for non static field accesses
    • Change non static accesses to static members using declaring type
    • Change indirect accesses to static members to direct accesses (accesses through subtypes)
    • Convert control statement bodies to block
    • Convert for loops to enhanced for loops
    • Add final modifier to private fields
    • Add final modifier to local variables
    • Remove unused imports
    • Remove unused private methods
    • Remove unused private constructors
    • Remove unused private types
    • Remove unused private fields
    • Remove unused local variables
    • Add missing @Override annotations
    • Add missing @Override annotations to implementations of interface methods
    • Add missing @Deprecated annotations
    • Add missing serial version ID (generated)
    • Remove unnecessary casts
    • Remove unnecessary $NON-NLS$ tags
    • Organize imports
    • Format source code
    • Remove trailing white spaces on all lines
    • Correct indentation
    • Remove redundant type arguments
    • Add file header (JAutodoc)

Using JAutodoc

Similar to Eclipse Source Operations, JAutodoc operations can be carried out on individual files or on all the files in a package but do not recurse into sub-packages. The JAutodoc operations are available by selecting a file or package and right clicking on JAutodoc:
  1. To add a package-info.java file to a package, select the package and right-click Jautodoc  Add Package Javadoc
  2. To add headers to files select on a file (or on the package to do all files) and right click JAutodoc  Add Header
  3. To add JAutodoc stubs to a files, select on a file (or on the package to do all files) and right click JAutodoc Add Javadoc

Using Checkstyle

In order to use Checkstyle, you must configure it per project and then activate it per project. The easiest way to do this is to set up one project as a blueprint and use that blueprint for other projects (see above). Once you have a blueprint project, you can use Checkstyle on other projects as follows
  1. Set up Checkstyle on projects by selecting one or more projects

    • Right clicking and selecting Checkstyle  Configure project(s) from blueprint…​ and then selecting your blueprint project
    • (for example apex-model.basic-model) from the list of projects and pressing OK
  2. Activate Checkstyle on projects by selecting one or more projects

    • Right clicking and selecting Checkstyle  Activate Checkstyle
    • Now Checkstyle warnings will appear on the selected projects if they have warnings
  3. You can disable Checkstyle checking on a file or a package (recursively) by selecting a file or package

    • Right clicking and selecting Checkstyle  Clear Checkstyle violations
  4. You can enable Checkstyle checking on a file or a package (recursively) by selecting a file or package

    • Right clicking and selecting Checkstyle  Check Code with Checkstyle
  5. On individual files, you can apply fixes that clear some Checkstyle warnings

    • Select the file, right click and select Apply Checkstyle fixes

Disable Eclipse Formatting (partially)

Sometimes, the Eclipse code formatting results in correct but untidy indentation, for example when Java Persistence annotations or long sequences of lined-up assignments are formatted. You can disable formatting for sections of code.
  1. Ensure that Off/On Tags are enabled in Eclipse
  2. In Eclipse  Window  Preferences  Java  Code Style Formatter window press Edit…​
  3. Click on the Off/On Tags tab
  4. Ensure that the Enable Off/On Tags checkbox is checked
  5. Surround the section of code that you do not want the formatter to act on with comments containing the Off/On tags
1 // @formatter:off
2 // Plugin Parameters
3 private DistributorParameters distributorParameters = new DistributorParameters();
4 private SchemaParameters      schemaParameters      = new SchemaParameters();
5 private LockManagerParameters lockManagerParameters = new LockManagerParameters();
6 private PersistorParameters   persistorParameters   = new PersistorParameters();
7 // @formatter:on

Supress Checkstyle (partially)

Sometimes Checkstyle checks identify code that does not comply with Checkstyle rules. In limited cases Checkstyle rules can be suppressed, for example where it is impossible to design the code in a way that complies with Checkstyle or where the Checkstyle rule is impossible to apply. Checkstyle rules are suppressed as is explained in this Stackoverflow post.
The example below illustrates how to suppress a Checkstyle rule that specifies all methods must have seven parameters or less.
1 // CHECKSTYLE:OFF: checkstyle:ParameterNumber
2 public myMethod(final int par1, final int par2, final int par3, final int par4,
3   final int par5, final int par6, final int par7, final int par8) {
4 }
5 // CHECKSTYLE:ON: checkstyle:ParameterNumber

apex-apps.utilities

CLI Example

Using the APEX CLI utilities can be done as follows. First, add the dependency of the utility project to your POM file.
<dependency>
  <groupId>org.onap.policy.apex-pdp.tools</groupId>
  <artifactId>tools-common</artifactId>
  <version>2.0.0-SNAPSHOT</version>
</dependency>
Now, create a new application project, for instance MyApp. In this project, create a new main application class as Application.java. In this class, create a new main method as public static void main(String[] args).
No use the provided CliOptions and CliParser. Manually importing means to add the following lines to the start of your application (in Eclipse this import will be done automatically):
1 import org.onap.policy.apex.tools.common.CliOptions;
2 import org.onap.policy.apex.tools.common.CliParser;
Now, inside your main() method, start setting some general application properties. Important are the application name and some description of your application. For instance:
1 String appName = "test-app";
2 final String appDescription = "a test app for documenting how to use the CLI utilities";
Next, create a new CLI Parser and add a few CLI options from the standard CliOptions. The following example adds options for help, version, and a model file:
1 final CliParser cli = new CliParser();
2 cli.addOption(CliOptions.HELP);
3 cli.addOption(CliOptions.VERSION);
4 cli.addOption(CliOptions.MODELFILE);
Next, parse the given CLI arguments:
1 final CommandLine cmd = cli.parseCli(args);
Once the command line is parsed, we can look into the individual options, check if they are set, and then act accordingly. We start with the option for help. If the option is present, we print a help screen and return:
1 // help is an exit option, print usage and exit
2 if (cmd.hasOption('h') || cmd.hasOption("help")) {
3     final HelpFormatter formatter = new HelpFormatter();
4     LOGGER.info(appName + " v" + cli.getAppVersion() + " - " + appDescription);
5     formatter.printHelp(appName, cli.getOptions());
6     return;
7 }
Next, we process the option for version. Here, we want to print a version for our application and return. The CLI Parser already provides a method to obtain the correct version for an APEX build, so we use that:
1 // version is an exit option, print version and exit
2 if (cmd.hasOption('v') || cmd.hasOption("version")) {
3     LOGGER.info(appName + " " + cli.getAppVersion());
4     return;
5 }
Once help and version arguments are processed, we can proceed to look at all other options. We have added an option for a model file, so check this option and test if we can actually load a model file with the given argument. If we can load a model, everything is ok. If we cannot load a model, we print an error and return.
1 String modelFile = cmd.getOptionValue('m');
2 if (modelFile == null) {
3     modelFile = cmd.getOptionValue("model");
4 }
5 if (modelFile == null) {
6     LOGGER.error(appName + ": no model file given, cannot proceed (try -h for help)");
7     return;
8 }
With a model file being loadable, we finish parsing command line arguments. We also print some status messages to note that the application now is ready to start:
1 LOGGER.info(appName + ": starting");
2 LOGGER.info(" --> model file: " + modelFile);
The last action now is to run the actual application. The example below is taken from a version of the Model2Cli application, which creates a new object and runs it in a try block, since exceptions might be thrown by the object:
 1 // your code for the application here
 2 // e.g.
 3 // try {
 4 // Model2Cli app = new Model2Cli(modelFile, !cmd.hasOption("sv"), appName);
 5 // app.runApp();
 6 // }
 7 // catch(ApexException aex) {
 8 // LOGGER.error(appName + ": caught APEX exception with message: " + aex.getMessage());
 9 // }
If this new application is now called with the command line -h or --help it will print the following help screen:
test-app v2.0.0-SNAPSHOT - a test app for documenting how to use the CLI utilities
usage: test-app
 -h,--help                 prints this help and usage screen
 -m,--model <MODEL-FILE>   set the input policy model file
 -v,--version              prints the application version
If this new application is called with the option -v or --version it will print its version information as:
test-app 2.0.0-SNAPSHOT

Autoversioning an Application

The APEX utilities project provides means to versioning an application automatically towards the APEX version it is written for. This is realized by generating a file called app-version.txt that includes the Maven project version. This file is then automatically deployed in the folder etc of a full APEX distribution. The CLI Parser here provides a mthod to access this version for an application.
First, create a new CLI Parser object, add some options (in the example an option for version, but any options will do), then parse the command line:
1 final CliParser cli = new CliParser();
2 cli.addOption(CliOptions.VERSION);
3 final CommandLine cmd = cli.parseCli(args);
Next, we check if the version option was used in the command line and print application name and version if it was used:
1 // version is an exit option, print version and exit
2 if (cmd.hasOption('v') || cmd.hasOption("version")) {
3     LOGGER.info("myApp" + " " + cli.getAppVersion());
4     return;
5 }
The output will be:
myApp 2.0.0-SNAPSHOT
The auto-version information comes from the method call cli.getAppVersion() in line 2 in the example above. The method is defined in the CliParser class as:
1 public String getAppVersion() {
2     return new Scanner(CliParser.class.getResourceAsStream("/app-version.txt"), "UTF-8").useDelimiter("\\A").next();
3 }
The file app-version.txt is automatically added to an APEX full distribution, as described above (for details on this see the POM files in the APEX application packaging projects).