Skip to content

Latest commit

 

History

History
129 lines (84 loc) · 3.39 KB

README.md

File metadata and controls

129 lines (84 loc) · 3.39 KB

VMF Tutorial 1

HOME NEXT ->

Defining your First Model

What you will learn

In this tutorial you will learn how to

  • setup a Gradle project for VMF
  • create a basic model
  • use the generated implementation

Setting up a Gradle Project

Since VMF comes with a convenient Gradle plugin it's easy to setup. We just have to add the VMF plugin id, e.g. via

plugins {
  id "eu.mihosoft.vmf" version "0.2" // use desired plugin version
}

The plugin adds a source set directory ( e.g., src/main/vmf) to our Gradle project intended for the model definition. In our first example we want to generate code for a very basic model. It just consists of one interface Parent with a single String property name. Here's how we can define the model as Java interface:

package eu.mihosoft.vmf.tutorial01.vmfmodel;

interface Parent {
    String getName();
}

The source directories of our tutorial project looks like this:

src
├── main/java
│         ├── /eu/mihosoft/vmf/tutorial01/Main.java
│         └── ...  
│
└── main/vmf
          ├── /eu/mihosoft/vmf/tutorial01/vmfmodel/Parent.java
          └── ...

Running the Code Generator

After we created our first model definition we are ready to run the code generator via the vmfGenModelSourcetask, e.g. via

./gradlew vmfGenModelSources

VMF should show the following output:

> Task :vmfGenModelSources
 -> generating code for vmf model in package: eu/mihosoft/vmf/tutorial01/vmfmodel

Using the Code

To use the code just use the generated code from your regular Java code, e.g, in src/main/java:

package eu.mihosoft.vmf.tutorial01;

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        // create a new parent instance
        Parent parent = Parent.newInstance();
        
        // set parent's name
        parent.setName("My Name");
        
        // check that name is set
        if("My Name".equals(parent.getName())) {
          System.out.println("name is correctly set");
        } else {
          System.out.println("something went wrong :(");
        }
        
    }
}

Running the Tutorial

To run this code, just call the run task of your gradle project:

Requirements

  • Java >= 1.8
  • Internet connection (dependencies are downloaded automatically)
  • IDE: Gradle Plugin (not necessary for command line usage)

IDE

Open the VMF-Tutorial-01 Gradle project in your favourite IDE (tested with NetBeans 8.2 and IntelliJ 2018) and run it by calling the run task.

Command Line

Navigate to the Gradle project (i.e., path/to/VMF-Tutorial-01) and enter the following command

Bash (Linux/macOS/Cygwin/other Unix shell)

bash gradlew run

Windows (CMD)

gradlew run

Conclusion

Congrats, you have successfully created your first VMF model. If you are lazy you can get the full project here.

HOME NEXT ->