本文介绍如何在Java运行环境(Maven或Serverless Devs工具)编译程序,并打包为ZIP包或JAR包。编译打包完成后,您可以在函数计算控制台或使用Serverless Devs工具上传代码包。
Java运行时依赖库
要创建部署代码包,请将函数代码和依赖库共同编译并打包为ZIP包或JAR包。
以上依赖库可通过Maven中央存储库获取。获取以上依赖库后将其添加到您的pom.xml文件中,如下所示:
<!-- 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>
说明 如果依赖包太大,可将依赖打包到层中,以减少代码包体积。具体操作,请参见
在函数中使用层。
使用Maven编译并部署
前提条件
安装Java和Maven。关于Java的详细信息,请参见官网。关于Maven的详细信息,请参见Installing Apache Maven。
操作步骤
- 创建一个Java项目,目录结构如下。
- 在项目根目录下创建pom.xml文件,在pom.xml文件中配置
build
,示例如下。<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>
- 打开命令行窗口,切换至项目的根目录,然后执行
mvn clean package
命令打包。示例代码如下:
[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] ------------------------------------------------------------------------
- 如果显示编译失败,请根据输出的编译错误信息调整代码。
- 如果编译成功,编译后的JAR包位于项目文件夹内的target目录内,并根据pom.xml内的artifactId、version字段命名为java-example-1.0-SNAPSHOT.jar。
注意 针对macOS和Linux操作系统,压缩前请确保代码文件具有可读和可执行权限。
- 登录函数计算控制台,上传代码包,然后在函数代码页签单击测试函数进行测试。
使用Serverless Devs编译并部署
前提条件
操作步骤
- 执行以下命令,初始化项目。
s init devsapp/start-fc-event-java8 -d start-fc-event-java8
s init devsapp/start-fc-event-java8
:表示基于HTTP请求处理程序模板来初始化项目。
-d start-fc-event-java8
:表示将项目文件输出到指定目录。
执行完命令后,按照提示选择地域、服务名、函数名和密钥配置。
- 执行以下命令,进入项目目录。
- 执行以下命令,部署项目。
s deploy
执行该命令会先执行pre-deploy
,pre-deploy
会执行mvn package
编译打包,然后上传部署代码包。输出示例如下:
[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
- 执行
s invoke
命令进行测试。输出示例如下:
➜ 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