Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Running a function with Gradle & Kotlin (build.gradle.kts) #35

Open
jasperroel opened this issue May 27, 2020 · 3 comments
Open

Running a function with Gradle & Kotlin (build.gradle.kts) #35

jasperroel opened this issue May 27, 2020 · 3 comments
Labels
enhancement New feature or request

Comments

@jasperroel
Copy link

Thanks for putting all this together! I was trying to get the local invoker working on my local machine, which is setup using kotlin (1.3.72) and gradle (6.4.1) via the build.gradle.kts file.

The Gradle example in the documentation uses a format that doesn't seem compatible with the build.gradle.kts file, so I'm hoping sharing that particular configuration helps people set up their project.

build.gradle.kts

val invoker by configurations.creating

dependencies {
    implementation("com.google.cloud.functions:functions-framework-api:1.0.1")
    invoker("com.google.cloud.functions.invoker:java-function-invoker:1.0.0-alpha-2-rc5")
}

task<JavaExec>("runFunction") {
    main = "com.google.cloud.functions.invoker.runner.Invoker"
    classpath(invoker)
    inputs.files(configurations.runtimeClasspath, sourceSets["main"].output)
    args(
        "--target", project.findProperty("runFunction.target") ?: "",
        "--port", project.findProperty("runFunction.port") ?: 8080
    )
    doFirst {
        args("--classpath", files(configurations.runtimeClasspath, sourceSets["main"].output).asPath)
    }
}

Apart from the particular formatting for the .kts version, I did have to add one thing:

  • I had to add the elvis operator ?: "" for ``--target, otherwise you cannot build the project due to Illegal null value provided in this collection: [--target, null, --port, 8080]`
@xetra11
Copy link

xetra11 commented Mar 19, 2021

@jasperroel
Hey there,
Where is val invoker by configurations.creating coming from? I can't resolve the example of yours because I have no idea what invoker is.

@jasperroel
Copy link
Author

@xetra11
Good question, honestly don't know anymore.

My example is "just" the .kts translation of this regular build.gradle script here: https://github.com/GoogleCloudPlatform/functions-framework-java#configuration-in-buildgradle

Did you add the implementation line that imports the dependencies?
implementation("com.google.cloud.functions:functions-framework-api:1.0.1") (or even 1.0.4, I see there have been some updates since then).

@i-walker
Copy link

This worked fine for me, too without specifying a new configuration. One only needs a configuration with resolve=false:

dependencies {
  implementation(Libs.gfun)
  compileOnly(Libs.gfunInvoker) // for running the function locally
}

tasks.register<JavaExec>("runFunction") {
  main = "com.google.cloud.functions.invoker.runner.Invoker"
  classpath(configurations.compileOnly)
  inputs.files(configurations.runtimeClasspath.get(), sourceSets.main.get().output)
  args(
    "--target", "<My Target class>",
    "--port", 8080 // hard-coded port
  )
  doFirst {
    args("--classpath", files(configurations.runtimeClasspath.get(), sourceSets.main.get().output).asPath)
  }
}

where Libs.gfun is "com.google.cloud.functions:functions-framework-api:1.0.4" or the latest version
and Libs.gfunInvoker is "com.google.cloud.functions.invoker:java-function-invoker:1.0.2" or the latest version

@josephlewis42 josephlewis42 added the enhancement New feature or request label Apr 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants