diff --git a/docs/src/main/asciidoc/gcp-functions-http.adoc b/docs/src/main/asciidoc/gcp-functions-http.adoc index 7774d38f594c9..c2f444ca993ca 100644 --- a/docs/src/main/asciidoc/gcp-functions-http.adoc +++ b/docs/src/main/asciidoc/gcp-functions-http.adoc @@ -142,25 +142,10 @@ public class GreetingRoutes { == Build and Deploy to Google Cloud -To build your application, you first need to define a packaging of type `uber-jar` via your `application.properties`. - -[source] ----- -quarkus.package.uber-jar=true ----- +NOTE: Quarkus forces a packaging of type `uber-jar` for your function as Google Cloud Function deployment requires a single JAR. Package your application using the standard `mvn clean package` command. -The result of the previous command is a single JAR file inside the `target` repository that contains classes and dependencies of the project. - -To deploy your JAR to Google Cloud, you need to pass a directory with only this JAR inside it, to the `gcloud` utility. - -So first, create a `deployment` directory and copy the generated artifact inside it. - -[source] ----- -mkdir deployment -cp target/google-cloud-functions-http-1.0-SNAPSHOT-runner.jar deployment/ ----- +The result of the previous command is a single JAR file inside the `target/deployment` directory that contains the classes and the dependencies of the project. Then you will be able to use `gcloud` to deploy your function to Google Cloud. @@ -168,7 +153,7 @@ Then you will be able to use `gcloud` to deploy your function to Google Cloud. ---- gcloud beta functions deploy quarkus-example-http \ --entry-point=io.quarkus.gcp.functions.http.QuarkusHttpFunction \ - --runtime=java11 --trigger-http --source=deployment + --runtime=java11 --trigger-http --source=target/deployment ---- [IMPORTANT] @@ -213,7 +198,7 @@ Then you can use it to launch your function locally. [source] ---- java -jar java-function-invoker-1.0.0-beta1.jar \ - --classpath target/google-cloud-functions-http-1.0-SNAPSHOT-runner.jar \ + --classpath target/deployment/google-cloud-functions-http-1.0-SNAPSHOT-runner.jar \ --target io.quarkus.gcp.functions.http.QuarkusHttpFunction ---- diff --git a/extensions/google-cloud-functions-http/deployment/src/main/java/io/quarkus/gcp/functions/http/deployment/CloudFunctionsDeploymentBuildStep.java b/extensions/google-cloud-functions-http/deployment/src/main/java/io/quarkus/gcp/functions/http/deployment/CloudFunctionsDeploymentBuildStep.java new file mode 100644 index 0000000000000..125ae2c29e7ea --- /dev/null +++ b/extensions/google-cloud-functions-http/deployment/src/main/java/io/quarkus/gcp/functions/http/deployment/CloudFunctionsDeploymentBuildStep.java @@ -0,0 +1,49 @@ +package io.quarkus.gcp.functions.http.deployment; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Collections; + +import io.quarkus.builder.BuildException; +import io.quarkus.deployment.IsNormal; +import io.quarkus.deployment.annotations.BuildStep; +import io.quarkus.deployment.pkg.builditem.ArtifactResultBuildItem; +import io.quarkus.deployment.pkg.builditem.JarBuildItem; +import io.quarkus.deployment.pkg.builditem.OutputTargetBuildItem; +import io.quarkus.deployment.pkg.builditem.UberJarRequiredBuildItem; +import io.quarkus.deployment.pkg.steps.NativeBuild; + +public class CloudFunctionsDeploymentBuildStep { + @BuildStep + public UberJarRequiredBuildItem forceUberJar() { + // Google Cloud Function needs a single JAR inside a dedicated directory + return new UberJarRequiredBuildItem(); + } + + /** + * Creates a target/deployment dir and copy the uber jar in it. + * This facilitates the usage of the 'glcoud' command. + */ + @BuildStep(onlyIf = IsNormal.class, onlyIfNot = NativeBuild.class) + public ArtifactResultBuildItem functionDeployment(OutputTargetBuildItem target, JarBuildItem jar) + throws BuildException, IOException { + if (!jar.isUberJar()) { + throw new BuildException("Google Cloud Function deployment need to use a uberjar, " + + "please set 'quarkus.package.uber-jar=true' inside your application.properties", + Collections.EMPTY_LIST); + } + + Path deployment = target.getOutputDirectory().resolve("deployment"); + if (Files.notExists(deployment)) { + Files.createDirectory(deployment); + } + + Path jarPath = jar.getPath(); + Path targetJarPath = deployment.resolve(jarPath.getFileName()); + Files.deleteIfExists(targetJarPath); + Files.copy(jarPath, targetJarPath); + + return new ArtifactResultBuildItem(targetJarPath, "function", Collections.EMPTY_MAP); + } +} diff --git a/integration-tests/google-cloud-functions-http/src/main/resources/application.properties b/integration-tests/google-cloud-functions-http/src/main/resources/application.properties index 433704e7740d9..e69de29bb2d1d 100644 --- a/integration-tests/google-cloud-functions-http/src/main/resources/application.properties +++ b/integration-tests/google-cloud-functions-http/src/main/resources/application.properties @@ -1 +0,0 @@ -quarkus.package.uber-jar=true \ No newline at end of file