This topic describes how to compile a program in the Java runtime environment (Maven
or Serverless Devs) and package it as a ZIP or JAR package. After the compilation
and packaging are complete, you can upload the code package in the Function Compute console or by using the Serverless Devs tool.
Java run time dependency library
To create a deployment code package, compile and package the function code and dependency
libraries together as a ZIP package or JAR package.
Function Compute Platform provides the following dependency libraries for Java run time:
You can use the Maven central repository to obtain the preceding dependencies. Get the above dependency libraries and add
them to your pom.xml file as follows:
<!-- https://mvnrepository.com/artifact/com.aliyun.fc.runtime/fc-java-core -->
<dependency>
<groupId>com.aliyun.fc.runtime</groupId>
<artifactId>fc-java-core</artifactId>
<version>1.4.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.aliyun.fc.runtime/fc-java-event -->
<dependency>
<groupId>com.aliyun.fc.runtime</groupId>
<artifactId>fc-java-event</artifactId>
<version>1.2.0</version>
</dependency>
Note If the dependency package is too large, you can package the dependencies into the
layer to reduce the code package size. For more information, see
Use layers in functions.
Compile and deploy using Maven
Prerequisites
Install Java and Maven. For more information about Java, visit the Java official website. For more information about Maven, see Installing Apache Maven.
Procedure
- Create a Java project that has the following directory structure:
- Create a pom.xml file under the project root directory and configure the
build
in the pom.xml file. Example: <build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
- Open a command line window, switch to the root directory of the project, and then
run the
mvn clean package
command to package. Sample code:
[INFO] Scanning for projects...
... .... ....
[INFO] --------------------------< example:example >---------------------------
[INFO] Building java-example 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
... .... ....
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 11.681 s
[INFO] Finished at: 2020-03-26T15:55:05+08:00
[INFO] ------------------------------------------------------------------------
- If the message shows that compilation failed, modify the code based on the error message.
- If the compilation succeeds, the compiled JAR package is located in the target directory in the project folder and named java-example-1.0-SNAPSHOT.jar based on the artifactId and version fields in the pom.xml.
Notice For macOS and Linux operating systems, make sure that the code file is readable and
executable before compression.
- Log on to the Function Compute console, upload the code package, and then click Test Function on the Function Code tab to test the function.
Compile and deploy using Serverless Devs
Prerequisites
Procedure
- Run the following command to initialize a project:
s init devsapp/start-fc-event-java8 -d start-fc-event-java8
s init devsapp/start-fc-event-java8
: indicates that the project is initialized based on the HTTP request handler template.
-d start-fc-event-java8
: indicates that the project file is output to the specified directory.
After you run the command, select the region, service name, function name, and key
configuration as prompted.
Note You can also execute the s init
to select a template as prompted.
- Run the following command to go to the project directory:
- Run the following command to deploy the project:
s deploy
Executing this command will first execute the pre-deploy
, pre-deploy
will execute the mvn package
compilation and packaging, and then upload the deployment code package. The following
code provides a sample output:
[2022-04-07 12:00:09] [INFO] [S-CORE] - Start the pre-action
[2022-04-07 12:00:09] [INFO] [S-CORE] - Action: mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------< example:HelloFCJava >-------------------------
[INFO] Building HelloFCJava 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
......
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.617 s
[INFO] Finished at: 2022-04-07T20:00:14+08:00
[INFO] ------------------------------------------------------------------------
[2022-04-07 12:00:14] [INFO] [S-CORE] - End the pre-action
✔Checking Service, Function (0.64s)
✔Creating Service, Function (0.71s)
Tips for next step
======================
* Display information of the deployed resource: s info
* Display metrics: s metrics
* Display logs: s logs
* Invoke remote function: s invoke
* Remove Service: s remove service
* Remove Function: s remove function
* Remove Trigger: s remove trigger
* Remove CustomDomain: s remove domain
helloworld:
region: cn-hangzhou
service:
name: hello-world-service
function:
name: start-fc-event-java8
runtime: java8
handler: example.App::handleRequest
memorySize: 128
timeout: 60
- Run the
s invoke
command to perform the test. The following code provides a sample output:
➜ start-fc-event-java8 s invoke
========= FC invoke Logs begin =========
FC Initialize Start RequestId: b246c3bf-06bc-49e5-92b8-xxxxxxxx
FC Initialize End RequestId: b246c3bf-06bc-49e5-92b8-xxxxxxxx
FC Invoke Start RequestId: b246c3bf-06bc-49e5-92b8-xxxxxxxx
FC Invoke End RequestId: b246c3bf-06bc-49e5-92b8-xxxxxxxx
Duration: 7.27 ms, Billed Duration: 8 ms, Memory Size: 128 MB, Max Memory Used: 65.75 MB
========= FC invoke Logs end =========
FC Invoke Result:
hello world
End of method: invoke