Skip to content

Instantly share code, notes, and snippets.

@yokawasa
Last active January 29, 2021 14:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save yokawasa/a26d47bd8a544dfe65f5a9096105bd98 to your computer and use it in GitHub Desktop.
Save yokawasa/a26d47bd8a544dfe65f5a9096105bd98 to your computer and use it in GitHub Desktop.
Quickstart V2 Java Functions

Quickstart V2 Java Functions

Prerequisites

  • JDK version 1.8.
# Mac 
brew tap caskroom/versions
brew cask install java8
# JAVA_HOME
export JAVA_HOME=/usr/libexec/java_home
#  or specifying version
export JAVA_HOME=/usr/libexec/java_home -v 1.8  

$ java -version
java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)
  • Apache Maven, version 3.0 or above
# Install on Mac
$ brew install maven
 
$ mvn --version
Apache Maven 3.6.0 (97c98ec64a1fdfee7767ce5ffb20918da4f719f3; 2018-10-25T03:41:47+09:00)
Maven home: /usr/local/Cellar/maven/3.6.0/libexec
Java version: 1.8.0_131, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.14.3", arch: "x86_64", family: "mac"

Create Java functions

Create new project

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

Define value for property 'groupId' (should match expression '[A-Za-z0-9_\-\.]+'): com.funcs.java
Define value for property 'artifactId' (should match expression '[A-Za-z0-9_\-\.]+'): funcs-java
Define value for property 'version' 1.0-SNAPSHOT: :
Define value for property 'package' com.funcs.java: :
Define value for property 'appName' funcs-java-20190408143812180: :
Define value for property 'appRegion' westus: : japaneast
Define value for property 'resourceGroup' java-functions-group: : RG-azfuncv2

New folder with a name of artifactId funcs-java will be created.

$ cd funcs-java
$ tree
.
├── host.json
├── local.settings.json
├── pom.xml
└── src
    ├── main
    │   └── java
    │       └── com
    │           └── funcs
    │               └── java
    │                   └── Function.java
    └── test
        └── java
            └── com
                └── funcs
                    └── java
                        ├── FunctionTest.java
                        └── HttpResponseMessageMock.java

Run the function locally

$ cd funcs-java

$ mvn clean package 

[INFO] Data/Telemetry
[INFO] ---------
[INFO] This project collects usage data and sends it to Microsoft to help improve our products and services.
[INFO] Read Microsoft's privacy statement to learn more: https://privacy.microsoft.com/en-us/privacystatement.
[INFO]
[INFO] You can change your telemetry configuration through 'allowTelemetry' property.
[INFO] For more information, please go to https://aka.ms/azure-maven-config.
[INFO]
[INFO] Step 1 of 7: Searching for Azure Functions entry points
[INFO] 1 Azure Functions entry point(s) found.
[INFO]
[INFO] Step 2 of 7: Generating Azure Functions configurations
[INFO] Generation done.
[INFO]
[INFO] Step 3 of 7: Validating generated configurations
[INFO] Validation done.
[INFO]
[INFO] Step 4 of 7: Saving empty host.json
[INFO] Successfully saved to /Users/yoichika/dev/tests/azure/functions/java/funcs-java/target/azure-functions/funcs-java-20190408143812180/host.json
[INFO]
[INFO] Step 5 of 7: Saving configurations to function.json
[INFO] Starting processing function: HttpTrigger-Java
[INFO] Successfully saved to /Users/yoichika/dev/tests/azure/functions/java/funcs-java/target/azure-functions/funcs-java-20190408143812180/HttpTrigger-Java/function.json
[INFO]
[INFO] Step 6 of 7: Copying JARs to staging directory/Users/yoichika/dev/tests/azure/functions/java/funcs-java/target/azure-functions/funcs-java-20190408143812180
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource to /Users/yoichika/dev/tests/azure/functions/java/funcs-java/target/azure-functions/funcs-java-20190408143812180
[INFO] Copied successfully.
[INFO] Skip install Function extension for HTTP Trigger Functions
[INFO] Successfully built Azure Functions.
...

$ mvn azure-functions:run

Hosting environment: Production
Content root path: /Users/yoichika/dev/tests/azure/functions/java/funcs-java/target/azure-functions/funcs-java-20190408143812180
Now listening on: http://0.0.0.0:7071
Application started. Press Ctrl+C to shut down.

Http Functions:

        HttpTrigger-Java: [GET,POST] http://localhost:7071/api/HttpTrigger-Java

Test Client request

$ curl http://localhost:7071/api/HttpTrigger-Java\?name\=hoge

Hello, hoge

Deployment

$ mvn azure-functions:deploy

LINKS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment