Skip to content

Commit 723e57e

Browse files
authored
Instructions for the Gradle build tool (#20)
* Add Gradle instructions
1 parent a7155b6 commit 723e57e

File tree

1 file changed

+73
-15
lines changed

1 file changed

+73
-15
lines changed

README.md

Lines changed: 73 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,14 @@ different environments, including:
1313

1414
## Installation
1515

16-
The Functions Framework for Java requires
16+
The Functions Framework for Java uses
1717
[Java](https://java.com/en/download/help/download_options.xml) and
18-
[Maven](http://maven.apache.org/install.html) (the `mvn` command).
18+
[Maven](http://maven.apache.org/install.html) (the `mvn` command),
19+
for building and deploying functions from source.
20+
21+
However, it is also possible to build your functions using
22+
[Gradle](https://gradle.org/), as JAR archives, that you will deploy with the
23+
`gcloud` command-line.
1924

2025
## Quickstart: Hello, World on your local machine
2126

@@ -32,6 +37,16 @@ that supports Maven to create the Maven project. Add this dependency in the
3237
</dependency>
3338
```
3439

40+
If you are using Gradle to build your functions, you can define the Functions
41+
Framework dependency in your `build.gradle` project file as follows:
42+
43+
```groovy
44+
dependencies {
45+
implementation 'com.google.cloud.functions:functions-framework-api:1.0.0-alpha-2-rc3'
46+
}
47+
48+
```
49+
3550
### Writing an HTTP function
3651

3752
Create a file `src/main/java/com/example/HelloWorld.java` with the following
@@ -178,17 +193,55 @@ java -jar java-function-invoker-1.0.0-alpha-2-rc4.jar \
178193
```
179194

180195

196+
## Running a function with Gradle
197+
198+
From Gradle, similarily to running functions with the Functions Framework jar,
199+
we can invoke the `Invoker` class with a `JavaExec` task.
200+
201+
### Configuration in `build.gradle`
202+
203+
```groovy
204+
configurations {
205+
invoker
206+
}
207+
208+
dependencies {
209+
implementation 'com.google.cloud.functions:functions-framework-api:1.0.0-alpha-2-rc3'
210+
invoker 'com.google.cloud.functions.invoker:java-function-invoker:1.0.0-alpha-2-rc4'
211+
}
212+
213+
tasks.register("runFunction", JavaExec) {
214+
main = 'com.google.cloud.functions.invoker.runner.Invoker'
215+
classpath(configurations.invoker)
216+
inputs.files(configurations.runtimeClasspath, sourceSets.main.output)
217+
args(
218+
'--target', project.findProperty('runFunction.target'),
219+
'--port', project.findProperty('runFunction.port') ?: 8080
220+
)
221+
doFirst {
222+
args('--classpath', files(configurations.runtimeClasspath, sourceSets.main.output).asPath)
223+
}
224+
}
225+
```
226+
227+
Then in your terminal or IDE, you will be able to run the function locally with:
228+
229+
```sh
230+
gradle runFunction -PrunFunction.target=com.example.HelloWorld \
231+
-PrunFunction.port=8080
232+
```
233+
234+
Or if you use the Gradle wrapper provided by your Gradle project build:
235+
236+
```sh
237+
./gradlew runFunction -PrunFunction.target=com.example.HelloWorld \
238+
-PrunFunction.port=8080
239+
```
240+
181241
## Functions Framework configuration
182242

183243
There are a number of options that can be used to configure the Functions
184-
Framework, whether run directly or on the command line. This table summarizes
185-
them, and the following sections explain them in detail.
186-
187-
| Command-line | `pom.xml` | Maven system property |
188-
|---------------|--------------------|-----------------------|
189-
| `--target` | `<functionTarget>` | `run.functionTarget` |
190-
| `--port` | `<port>` | `run.port` |
191-
| `--classpath` | - | - |
244+
Framework, whether run directly or on the command line.
192245

193246
### Which function to run
194247

@@ -199,19 +252,24 @@ the Functions Framework:
199252
--target com.example.HelloWorld
200253
<functionTarget>com.example.HelloWorld</functionTarget>
201254
-Drun.functionTarget=com.example.HelloWorld
255+
-Prun.functionTarget=com.example.HelloWorld
202256
```
203257

258+
* Invoker argument: `--target com.example.HelloWorld`
259+
* Maven `pom.xml`: `<functionTarget>com.example.HelloWorld</functionTarget>`
260+
* Maven CLI argument: `-Drun.functionTarget=com.example.HelloWorld`
261+
* Gradle CLI argument: `-Prun.functionTarget=com.example.HelloWorld`
262+
204263
### Which port to listen on
205264

206265
The Functions Framework is an HTTP server that directs incoming HTTP requests to
207266
the function code. By default this server listens on port 8080. Specify an
208267
alternative value like this:
209268

210-
```
211-
--port 12345
212-
<port>12345</port>
213-
-Drun.port=12345
214-
```
269+
* Invoker argument: `--port 12345`
270+
* Maven `pom.xml`: `<port>12345</port>`
271+
* Maven CLI argument: `-Drun.port=12345`
272+
* Gradle CLI argument: `-Prun.port=12345`
215273

216274
### Function classpath
217275

0 commit comments

Comments
 (0)