In this tutorial you will learn how to
- setup a Gradle project for VMF
- create a basic model
- use the generated implementation
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
└── ...
After we created our first model definition we are ready to run the code generator via the vmfGenModelSource
task, 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
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 :(");
}
}
}
To run this code, just call the run
task of your gradle project:
- Java >= 1.8
- Internet connection (dependencies are downloaded automatically)
- IDE: Gradle Plugin (not necessary for command line usage)
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.
Navigate to the Gradle project (i.e., path/to/VMF-Tutorial-01
) and enter the following command
bash gradlew run
gradlew run
Congrats, you have successfully created your first VMF model. If you are lazy you can get the full project here.