Using the new Gradle plugin for Azure Functions to deploy Spring Boot serverless applications
Tags: spring, azure, java, serverless
Published: 2020-03-31 14:36:21

---
title: Using the new Gradle plugin for Azure Functions to deploy Spring Boot serverless applications
published: true
description: This post shows how to use the new Gradle plugin for Azure Functions with Spring Cloud Functions
tags: spring, azure, java, serverless
---

## Introducing the new Gradle plugins for Azure

For building and deploying Java projects, Azure always had great Maven support, but it was lacking Gradle support. As many people love Gradle, in particular when building complex projects, I was thrilled to learn that a new set of Gradle plugins were being developed! They will all be Open Source, and their code will be available on GitHub at [https://github.com/microsoft/azure-gradle-plugins](https://github.com/microsoft/azure-gradle-plugins).

The first plugin to be released is the one for Azure Functions, at [https://github.com/microsoft/azure-gradle-plugins/tree/master/azure-functions-gradle-plugin](https://github.com/microsoft/azure-gradle-plugins/tree/master/azure-functions-gradle-plugin). [Azure Functions](https://docs.microsoft.com/azure/azure-functions/?WT.mc_id=devto-blog-judubois) is the Azure serverless offer, and you can use it to run your Java workloads in a managed, event-driven, fully scalable and inexpensive environment.

If you want to run this Gradle plugin for a simple Java serverless application, the full documentation is available [on the Microsoft Docs website](https://docs.microsoft.com/azure/azure-functions/functions-create-first-java-maven?pivots=java-build-tools-gradle/?WT.mc_id=devto-blog-judubois). This is good for a simple Java application, which you would use for a very simple Function, but what about more complex applications, the ones that typically would benefit more from using Gradle?

## Running Spring Boot in a serverless environnement

Thanks to Microsoft's work with Pivotal/VMWare, you can run Spring Boot on Azure Functions using the [Spring Cloud Function](https://spring.io/projects/spring-cloud-function) project, which has specific [Azure support](https://github.com/spring-cloud/spring-cloud-function/tree/master/spring-cloud-function-samples/function-sample-azure).

If you want to test Spring Cloud Function and Azure Functions, I maintain the official sample application at [https://github.com/Azure-Samples/hello-spring-function-azure](https://github.com/Azure-Samples/hello-spring-function-azure). This is using Maven by default, and in this blog post we are going see how to use it with the new Gradle plugin.

## Using the new Gradle plugin to deploy Spring Boot to Azure Functions

I have created a specific `gradle` branch in the official sample project, which you can check at [https://github.com/Azure-Samples/hello-spring-function-azure/tree/gradle](https://github.com/Azure-Samples/hello-spring-function-azure/tree/gradle).

With that code, you will be able to use the new Azure Gradle plugin to deploy a Spring Cloud Function application to Azure Functions.

It uses one specific trick to have Azure Functions run the "Main" Spring Boot class, and this is why we added this block in the `build.gradle` file:

```
jar {
    manifest {
        attributes 'Main-Class' : 'com.example.HelloFunction'
    }
}
```

The block of code above will create a specific Java manifest file, which points to the main Spring Boot class, so that Azure Functions will execute it.

If you look at the Maven version of this application, we are copying a specific `local.settings.json` file to achieve the same goal (helping Azure Functions find the main class). This solution looks more elegant with Gradle.

Then, the rest of the configuration uses the Gradle plugin normally:

```
azurefunctions {
    resourceGroup = 'my-spring-function-resource-group'
    appName = 'my-spring-function'
    appSettings {
        WEBSITE_RUN_FROM_PACKAGE = '1'
        FUNCTIONS_EXTENSION_VERSION = '~2'
        FUNCTIONS_WORKER_RUNTIME = 'java'
        MAIN_CLASS = 'com.example.HelloFunction'
    }
    authentication {
        type = 'azure_cli'
    }
    // enable local debug
    // localDebug = "transport=dt_socket,server=y,suspend=n,address=5005"
    deployment {
        type = 'run_from_blob'
    }
}
```

- You will need to configure a specific Azure Resource Group in which your Function will run.
- You will need to give a unique `appName` to your Function: this name should be unique across all of Azure, so usually people prefix it with their username.
- We are using the latest `FUNCTIONS_EXTENSION_VERSION` available at the time of this writing, as it has much better cold start performance when running Java.

Now that the plugin is installed, you can use the following Gradle commands:

```bash
gradle azureFunctionsRun
```

This will run Azure Functions locally, so this is the best way to develop and debug your Function.

```bash
gradle azureFunctionsPackage
```

This will package your application, so it is ready to be deployed to Azure Functions.

```bash
gradle azureFunctionsDeploy
```

This will deploy your Function to Azure Functions, so it is available in the cloud.

The application then works exactly the same as the one built with Maven, of course! So you can test it by doing a cURL request as follows:

```bash
curl https://<YOUR_SPRING_FUNCTION_NAME>.azurewebsites.net/api/hello -d "{\"name\":\"Azure\"}"
```