title | description | ms.date | ms.topic | ms.custom | adobe-target | adobe-target-activity | adobe-target-experience | adobe-target-content | |||
---|---|---|---|---|---|---|---|---|---|---|---|
Create a Java function from the command line - Azure Functions |
Learn how to create a Java function from the command line, then publish the local project to serverless hosting in Azure Functions. |
11/03/2020 |
quickstart |
|
true |
DocsExp–386541–A/B–Enhanced-Readability-Quickstarts–2.19.2021 |
Experience B |
./create-first-function-cli-java-uiex |
[!INCLUDE functions-language-selector-quickstart-cli]
In this article, you use command-line tools to create a Java function that responds to HTTP requests. After testing the code locally, you deploy it to the serverless environment of Azure Functions.
If Maven isn't your preferred development tool, check out our similar tutorials for Java developers:
Completing this quickstart incurs a small cost of a few USD cents or less in your Azure account.
Before you begin, you must have the following:
-
An Azure account with an active subscription. Create an account for free.
-
The Azure Functions Core Tools version 3.x..
-
The Azure CLI version 2.4 or later.
-
The Java Developer Kit, version 8 or 11. The
JAVA_HOME
environment variable must be set to the install location of the correct version of the JDK. -
Apache Maven, version 3.0 or above.
-
In a terminal or command window, run
func --version
to check that the Azure Functions Core Tools are version 3.x. -
Run
az --version
to check that the Azure CLI version is 2.4 or later. -
Run
az login
to sign in to Azure and verify an active subscription.
In Azure Functions, a function project is a container for one or more individual functions that each responds to a specific trigger. All functions in a project share the same local and hosting configurations. In this section, you create a function project that contains a single function.
-
In an empty folder, run the following command to generate the Functions project from a Maven archetype.
mvn archetype:generate -DarchetypeGroupId=com.microsoft.azure -DarchetypeArtifactId=azure-functions-archetype -DjavaVersion=8
mvn archetype:generate "-DarchetypeGroupId=com.microsoft.azure" "-DarchetypeArtifactId=azure-functions-archetype" "-DjavaVersion=8"
mvn archetype:generate "-DarchetypeGroupId=com.microsoft.azure" "-DarchetypeArtifactId=azure-functions-archetype" "-DjavaVersion=8"
[!IMPORTANT]
- Use
-DjavaVersion=11
if you want your functions to run on Java 11. To learn more, see Java versions. - The
JAVA_HOME
environment variable must be set to the install location of the correct version of the JDK to complete this article.
- Use
-
Maven asks you for values needed to finish generating the project on deployment.
Provide the following values when prompted:Prompt Value Description groupId com.fabrikam
A value that uniquely identifies your project across all projects, following the package naming rules for Java. artifactId fabrikam-functions
A value that is the name of the jar, without a version number. version 1.0-SNAPSHOT
Choose the default value. package com.fabrikam
A value that is the Java package for the generated function code. Use the default. -
Type
Y
or press Enter to confirm.Maven creates the project files in a new folder with a name of artifactId, which in this example is
fabrikam-functions
. -
Navigate into the project folder:
cd fabrikam-functions
This folder contains various files for the project, including configurations files named local.settings.json and host.json. Because local.settings.json can contain secrets downloaded from Azure, the file is excluded from source control by default in the .gitignore file.
If desired, you can skip to Run the function locally and examine the file contents later.
Function.java contains a run
method that receives request data in the request
variable is an HttpRequestMessage that's decorated with the HttpTrigger annotation, which defines the trigger behavior.
:::code language="java" source="~/azure-functions-samples-java/src/main/java/com/functions/Function.java":::
The response message is generated by the HttpResponseMessage.Builder API.
Settings for the Azure resources created to host your app are defined in the configuration element of the plugin with a groupId of com.microsoft.azure
in the generated pom.xml file. For example, the configuration element below instructs a Maven-based deployment to create a function app in the java-functions-group
resource group in the westus
region. The function app itself runs on Windows hosted in the java-functions-app-service-plan
plan, which by default is a serverless Consumption plan.
:::code language="java" source="~/azure-functions-samples-java/pom.xml" range="62-107":::
You can change these settings to control how resources are created in Azure, such as by changing runtime.os
from windows
to linux
before initial deployment. For a complete list of settings supported by the Maven plug-in, see the configuration details.
The archetype also generates a unit test for your function. When you change your function to add bindings or add new functions to the project, you'll also need to modify the tests in the FunctionTest.java file.
-
Run your function by starting the local Azure Functions runtime host from the LocalFunctionProj folder:
mvn clean package mvn azure-functions:run
Toward the end of the output, the following lines should appear:
... Now listening on: http://0.0.0.0:7071 Application started. Press Ctrl+C to shut down. Http Functions: HttpExample: [GET,POST] http://localhost:7071/api/HttpExample ...
[!NOTE]
If HttpExample doesn't appear as shown above, you likely started the host from outside the root folder of the project. In that case, use Ctrl+C to stop the host, navigate to the project's root folder, and run the previous command again. -
Copy the URL of your
HttpExample
function from this output to a browser and append the query string?name=<YOUR_NAME>
, making the full URL likehttp://localhost:7071/api/HttpExample?name=Functions
. The browser should display a message that echoes back your query string value. The terminal in which you started your project also shows log output as you make requests. -
When you're done, use Ctrl+C and choose
y
to stop the functions host.
A function app and related resources are created in Azure when you first deploy your functions project. Settings for the Azure resources created to host your app are defined in the pom.xml file. In this article, you'll accept the defaults.
Tip
To create a function app running on Linux instead of Windows, change the runtime.os
element in the pom.xml file from windows
to linux
. Running Linux in a consumption plan is supported in these regions. You can't have apps that run on Linux and apps that run on Windows in the same resource group.
-
Before you can deploy, sign in to your Azure subscription using either Azure CLI or Azure PowerShell.
az login
The az login command signs you into your Azure account.
Connect-AzAccount
The Connect-AzAccount cmdlet signs you into your Azure account.
-
Use the following command to deploy your project to a new function app.
mvn azure-functions:deploy
This creates the following resources in Azure:
- Resource group. Named as java-functions-group.
- Storage account. Required by Functions. The name is generated randomly based on Storage account name requirements.
- Hosting plan. Serverless hosting for your function app in the westus region. The name is java-functions-app-service-plan.
- Function app. A function app is the deployment and execution unit for your functions. The name is randomly generated based on your artifactId, appended with a randomly generated number.
The deployment packages the project files and deploys them to the new function app using zip deployment. The code runs from the deployment package in Azure.
[!INCLUDE functions-run-remote-azure-cli]
[!INCLUDE functions-streaming-logs-cli-qs]
If you continue to the next step and add an Azure Storage queue output binding, keep all your resources in place as you'll build on what you've already done.
Otherwise, use the following command to delete the resource group and all its contained resources to avoid incurring further costs.
az group delete --name java-functions-group
Remove-AzResourceGroup -Name java-functions-group
[!div class="nextstepaction"] Connect to an Azure Storage queue