Skip to content

Commit 66a349a

Browse files
committed
Packaging improvement
1 parent d7452a0 commit 66a349a

File tree

3 files changed

+53
-20
lines changed

3 files changed

+53
-20
lines changed

docs/src/main/asciidoc/gcp-functions-http.adoc

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -142,33 +142,18 @@ public class GreetingRoutes {
142142

143143
== Build and Deploy to Google Cloud
144144

145-
To build your application, you first need to define a packaging of type `uber-jar` via your `application.properties`.
146-
147-
[source]
148-
----
149-
quarkus.package.uber-jar=true
150-
----
145+
NOTE: Quarkus forces a packaging of type `uber-jar` for your function as Google Cloud Function deployment requires a single JAR.
151146

152147
Package your application using the standard `mvn clean package` command.
153-
The result of the previous command is a single JAR file inside the `target` repository that contains classes and dependencies of the project.
154-
155-
To deploy your JAR to Google Cloud, you need to pass a directory with only this JAR inside it, to the `gcloud` utility.
156-
157-
So first, create a `deployment` directory and copy the generated artifact inside it.
158-
159-
[source]
160-
----
161-
mkdir deployment
162-
cp target/google-cloud-functions-http-1.0-SNAPSHOT-runner.jar deployment/
163-
----
148+
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.
164149

165150
Then you will be able to use `gcloud` to deploy your function to Google Cloud.
166151

167152
[source]
168153
----
169154
gcloud beta functions deploy quarkus-example-http \
170155
--entry-point=io.quarkus.gcp.functions.http.QuarkusHttpFunction \
171-
--runtime=java11 --trigger-http --source=deployment
156+
--runtime=java11 --trigger-http --source=target/deployment
172157
----
173158

174159
[IMPORTANT]
@@ -213,7 +198,7 @@ Then you can use it to launch your function locally.
213198
[source]
214199
----
215200
java -jar java-function-invoker-1.0.0-beta1.jar \
216-
--classpath target/google-cloud-functions-http-1.0-SNAPSHOT-runner.jar \
201+
--classpath target/deployment/google-cloud-functions-http-1.0-SNAPSHOT-runner.jar \
217202
--target io.quarkus.gcp.functions.http.QuarkusHttpFunction
218203
----
219204

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package io.quarkus.gcp.functions.http.deployment;
2+
3+
import java.io.IOException;
4+
import java.nio.file.Files;
5+
import java.nio.file.Path;
6+
import java.util.Collections;
7+
8+
import io.quarkus.builder.BuildException;
9+
import io.quarkus.deployment.IsNormal;
10+
import io.quarkus.deployment.annotations.BuildStep;
11+
import io.quarkus.deployment.pkg.builditem.ArtifactResultBuildItem;
12+
import io.quarkus.deployment.pkg.builditem.JarBuildItem;
13+
import io.quarkus.deployment.pkg.builditem.OutputTargetBuildItem;
14+
import io.quarkus.deployment.pkg.builditem.UberJarRequiredBuildItem;
15+
import io.quarkus.deployment.pkg.steps.NativeBuild;
16+
17+
public class CloudFunctionsDeploymentBuildStep {
18+
@BuildStep
19+
public UberJarRequiredBuildItem forceUberJar() {
20+
// Google Cloud Function needs a single JAR inside a dedicated directory
21+
return new UberJarRequiredBuildItem();
22+
}
23+
24+
/**
25+
* Creates a target/deployment dir and copy the uber jar in it.
26+
* This facilitates the usage of the 'glcoud' command.
27+
*/
28+
@BuildStep(onlyIf = IsNormal.class, onlyIfNot = NativeBuild.class)
29+
public ArtifactResultBuildItem functionDeployment(OutputTargetBuildItem target, JarBuildItem jar)
30+
throws BuildException, IOException {
31+
if (!jar.isUberJar()) {
32+
throw new BuildException("Google Cloud Function deployment need to use a uberjar, " +
33+
"please set 'quarkus.package.uber-jar=true' inside your application.properties",
34+
Collections.EMPTY_LIST);
35+
}
36+
37+
Path deployment = target.getOutputDirectory().resolve("deployment");
38+
if (Files.notExists(deployment)) {
39+
Files.createDirectory(deployment);
40+
}
41+
42+
Path jarPath = jar.getPath();
43+
Path targetJarPath = deployment.resolve(jarPath.getFileName());
44+
Files.deleteIfExists(targetJarPath);
45+
Files.copy(jarPath, targetJarPath);
46+
47+
return new ArtifactResultBuildItem(targetJarPath, "function", Collections.EMPTY_MAP);
48+
}
49+
}
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
quarkus.package.uber-jar=true

0 commit comments

Comments
 (0)