Skip to content

Latest commit

 

History

History
260 lines (217 loc) · 10.6 KB

File metadata and controls

260 lines (217 loc) · 10.6 KB

Maven Plugin for Azure Functions

Maven Central

Table of Content

Prerequisites

Tool Required Version
JDK 1.8
Maven 3.0 and above
.Net Core SDK Latest version
Azure Functions Core Tools 2.0 and above

Note: See how to install Azure Functions Core Tools - 2.x

Goals

azure-functions:package

  • Scan the output directory (default is ${project.basedir}/target/classes) and generating function.json for each function (method annotated with FunctionName) in the staging directory.
  • Copy JAR files from the build directory (default is ${project.basedir}/target/) to the staging directory.

The default lifecycle phase for azure-functions:package is package. You can simply run mvn package to generate all the outputs you need to run/deploy your project.

Default staging directory is ${project.basedir}/target/azure-functions/${function-app-name}/

azure-functions:add

  • Create a new Java function and add to the current project.
  • You will be prompted to choose a template and enter parameters. Templates for below triggers are supported as of now:
    • HTTP Trigger
    • Azure Storage Blob Trigger
    • Azure Storage Queue Trigger
    • Timer Trigger
    • Event Grid Trigger
    • Event Hub Trigger
    • Cosmos DB Trigger
    • Service Bus Queue Trigger
    • Service Bus Topic Trigger

azure-functions:run

  • Invoke Azure Functions Local Emulator to run all functions. Default working directory is the staging directory.

azure-functions:deploy

  • Deploy the staging directory to target Azure Functions.
  • If target Azure Functions does not exist already, it will be created.

Usage

To use the Maven Plugin for Azure Functions in your Maven Java app, add the following snippet to your pom.xml file:

<project>
  ...
  <build>
    ...
    <plugins>
      ...
      <plugin>
        <groupId>com.microsoft.azure</groupId>
          <artifactId>azure-functions-maven-plugin</artifactId>
          <version>1.3.2</version>
          <configuration>
            ...
          </configuration>
      </plugin>
    </plugins>
  </build>
</project>

Read How-To section to learn detailed usages.

Common Configuration

This Maven plugin supports common configurations of all Maven Plugins for Azure. Detailed documentation of common configurations is at here.

Configurations

This Maven Plugin supports the following configuration properties:

Property Required Description
<resourceGroup> true Specifies the Azure Resource Group for your Azure Functions.
<appName> true Specifies the name of your Azure Functions.
<region>* false Specifies the region where your Azure Functions will be hosted; default value is westus. All valid regions are at Supported Regions section.
<pricingTier>* false Specifies the pricing tier for your Azure Functions; default value is Consumption. All valid pricing tiers are at Supported Pricing Tiers section.
<appServicePlanResourceGroup> false Specifies the resource group of the existing App Service Plan when you do not want to create a new one. If this setting is not specified, the plugin will use the value defined in <resourceGroup>.
<appServicePlanName> false Specifies the name of the existing App Service Plan when you do not want to create a new one.
<appSettings> false Specifies the application settings for your Azure Functions, which are defined in name-value pairs like following example:
<property>
    <name>xxxx</name>
    <value>xxxx</value>
</property>
<deploymentType> false Specifies the deployment approach you want to use.
The default value is zip. When <deploymentType> is zip and WEBSITE_RUN_FROM_PACKAGE in appSettings values 1, the function will be deployed and run from package, you may visit here for more information.
<httpProxyHost> false Specifies an optional HTTP proxy to connect to Azure through.
<httpProxyPort> false Specifies an optional HTTP proxy port to connect to Azure through
The default value is 80.
<localDebugConfig> false The config string of debug options, you may visit here for more information. The default value is transport=dt_socket,server=y,suspend=n,address=5005;

*: This setting will be used to create a new Azure Functions if specified Azure Functions does not exist; if target Azure Functions already exists, this setting will be ignored.

Supported Regions

All valid regions are listed as below. Read more at Azure Region Availability.

  • westus
  • westus2
  • eastus
  • eastus2
  • northcentralus
  • southcentralus
  • westcentralus
  • canadacentral
  • canadaeast
  • brazilsouth
  • northeurope
  • westeurope
  • uksouth
  • eastasia
  • southeastasia
  • japaneast
  • japanwest
  • australiaeast
  • australiasoutheast
  • centralindia
  • southindia

Supported Pricing Tiers

Consumption plan is the default if you don't specify anything for your Azure Functions. You can also run Functions within your App Service Plan. All valid App Service plan pricing tiers are listed as below. Read more at Azure App Service Plan Pricing.

  • F1
  • D1
  • B1
  • B2
  • B3
  • S1
  • S2
  • S3
  • P1V2
  • P2V2
  • P3V2

How-To

Generate the function archetype

Run below command to generate the function archetype:

mvn archetype:generate -DarchetypeGroupId=com.microsoft.azure -DarchetypeArtifactId=azure-functions-archetype

Add new function to current project

Run below command to create a new function:

  • In package com.your.package
  • Named NewFunction
  • Bound to HttpTrigger
mvn azure-functions:add -Dfunctions.package=com.your.package -Dfunctions.name=NewFunction -Dfunctions.template=HttpTrigger

You don't have to provide all the properties on the command line. Missing properties will be prompted for input during the execution of the goal.

Generate function.json from current project

Follow below instructions, you don't need to handwrite function.json anymore.

  1. Use annotations from package com.microsoft.azure:azure-functions-java-library to decorate your functions.
  2. Run mvn clean package; then function.json files will be automatically generated for all functions in your project.

Run Azure Functions locally

With the help of goal azure-functions:run, you can run your Azure Functions in current project locally with below command:

mvn azure-functions:run

If you want to start the function host in debug mode, please add -DenableDebug as the argument. The function host use TCP-Socket Transport and listen on 5005 port by default, you may change the config string in configuration properties.

mvn azure-functions:run -DenableDebug

Note: Before you can run Azure Functions locally, install .Net Core SDK and Azure Functions Core Tools first.

Deploy Azure Functions to Azure

Directly deploy to target Azure Functions by running mvn azure-functions:deploy.

Supported deployment methods are listed as below. The default value is ZIP.

  • ZIP
  • 
    

Add Proxies Configuration to Your Azure Function

Azure Functions proxies can be used for things like URL rewriting. To include a proxies.json file in your deployment you need to add it to the maven-resources-plugin configuration in pom.xml alongside host.json and local.settings.json.

For example, if you had a function bound to the path api/index which you wanted to also expose at the root URL of your Functions app you can do that with a proxies.json like this:

{
  "$schema": "http://json.schemastore.org/proxies",
  "proxies": {
    "IndexProxy": {
      "matchCondition": {
        "route": "/"
      },
      "backendUri": "https://%WEBSITE_HOSTNAME%/api/index"
    }
  }
}

Common Questions

Q: Can I upload other static content, e.g. HTML files, as part of the function deployment?

A: You can achieve this by adding configurations for the maven-resources-plugin. For example:

<plugin>
  <artifactId>maven-resources-plugin</artifactId>
  <executions>
    <execution>
      <id>copy-resources</id>
      <phase>package</phase>
      <goals>
        <goal>copy-resources</goal>
      </goals>
      <configuration>
        <overwrite>true</overwrite>
        <outputDirectory>${stagingDirectory}</outputDirectory>
        <resources>         
          ...         
          <!-- Your static resources -->
          <resource>
            <directory>${project.basedir}/src/main/resources</directory>
            <includes>
              <include>www/**</include>
            </includes>
          </resource>
        </resources>
      </configuration>
    </execution>
    ...
  </executions>
</plugin>