Skip to content

Commit

Permalink
Packaging improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
loicmathieu committed Jun 26, 2020
1 parent d7452a0 commit 66a349a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 20 deletions.
23 changes: 4 additions & 19 deletions docs/src/main/asciidoc/gcp-functions-http.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -142,33 +142,18 @@ 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.

[source]
----
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]
Expand Down Expand Up @@ -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
----

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
quarkus.package.uber-jar=true

0 comments on commit 66a349a

Please sign in to comment.