Back to articles list
- 3 minutes read

How I Use the Vertabelo API With Gradle

What is Gradle?

Gradle is an open source build automation system. It can automate the building, testing, publishing, deployment and more of software packages or other types of projects such as generated static websites, generated documentation or indeed anything else.

Learn more about what makes Gradle a compelling choice for build automation.

One of the exciting moments in a programmer’s life is to leave legacy code behind and have fun with some new, fresh and cool tools. Today, I have just such an opportunity – I’m creating the structure of a new project in Java.

First of all, let choose the build system. There’re plenty of them: Ant, Maven, Make. Well, let’s try something better: Gradle. That build automation tool with a clean and simple format of configuration written in Groovy DSL became a real competitor to Maven.

Installing Gradle for Ubuntu users:

sudo apt-get install gradle

It’s recommend to install the newest version:

  • download and unzip the archive from the official page: Gradle
  • set GRADLE_HOME environment which points to the unpacked directory
  • add GRADLE_HOME/bin to your PATH environment variable

Test your installation:

gradle -v

Getting started using Gradle is quite simple – just create build.gradle in a project directory and add only one line:

apply plugin: 'java'

Add HelloVertabelo.java to src/main/java directory:

class HelloVertabelo {
   public static void main(String[] args) {
               System.out.println("Hello Vertabelo!");
}}

and run:

gradle build

Your application has just been built.

Now let’s think about the application database. Creating it by hand? Not a good idea. Vertabelo? Yes, that’s better. First impressions with Vertabelo are quite promising. I want Gradle to download the model during the build (for example to create the database physical structure). How do I do that? Well, I’ve got a solution already. You need to write the task in Gradle. It’s not complicated. The syntax of Gradle files is Groovy DSL which will be familiar to Java programmers. Additionally, you can invoke ready-to-use ant tasks which simplify the code. Just add the text below to build.gradle file:

task updateVertabelo << {
   String gid= <'enter you gid'>
   String accessToken = System.getenv()['VERTABELO_API_TOKEN']
   if (accessToken == null) {
       ant.fail("Vertabelo api token not set")
   }

   String apiUrl='https://my.vertabelo.com/api'
   String targetDir='./'
   ant.get(src: apiUrl + '/xml/' + gid,
           dest: targetDir + project.name + '.xml',
           username: accessToken
   )
   ant.get(src: apiUrl + '/sql/' + gid,
           dest: targetDir + project.name + '.sql',
           username: accessToken
   )
}
build.dependsOn updateVertabelo

Remember that there’re two parameters required to run this task properly:

  • the variable ‘git’ in the task is a identifier of your model in Vertabelo, which you can find in the Model details panel:


    Vertabelo Model details

  • accessToken parameter set as the environment variable taken from Vertabelo => My Account


    Vertabelo API

The token should be set in that (Unix) way:

export VERTABELO_API_TOKEN=

Finally, you can execute: gradle build

$ gradle build
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:jar UP-TO-DATE
:assemble UP-TO-DATE
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test UP-TO-DATE
:check UP-TO-DATE
:updateVertabelo
:build

BUILD SUCCESSFUL

Done! The database model with the SQL file will be downloaded every time you build the application.

For more information about Gradle please visit the official page: Gradle.

For information about Vertabelo: Vertabelo.

go to top