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.
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. Azure Functions 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. 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 project, which has specific Azure support.
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. 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.
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:
gradle azureFunctionsRun
This will run Azure Functions locally, so this is the best way to develop and debug your Function.
gradle azureFunctionsPackage
This will package your application, so it is ready to be deployed to Azure Functions.
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:
curl https://<YOUR_SPRING_FUNCTION_NAME>.azurewebsites.net/api/hello -d "{\"name\":\"Azure\"}"
Top comments (1)
What if I run this in private network where vm and func app is secured with public access disabled? If I run gradlew azureFunctionsDeploy now, it trys to deploy func app from some kind of external runners and fails, how to avoid that?