This topic shows you how to run the fun build command to build a code package for delivery based on the source code.

Important This topic is no longer maintained. If you use Funcraft to manage your Function Compute resources, we recommend that you migrate your resources to Serverless Devs and use Serverless Devs to manage your resources.

For more information about how to migrate Function Compute-related resources from Funcraft to Serverless Devs, see Migrate resources from Funcraft to Serverless Devs.

For more information about Serverless Devs, see What is Serverless Devs?.

For more information about how to use Serverless Devs to install dependencies, see Run the s build command to install dependencies.

We apologize for any inconvenience caused.

Background

The source code is not a deliverable. For example, after you write code in Java, you cannot directly upload the code. You must compile and package the code before you upload it. The package obtained after you compile and package the code is a deliverable. You can upload only one ZIP or JAR package to Function Compute. Therefore, you must compress the code deliverable and its dependencies into a JAR package and then deliver the package.

When you run the fun build command to build a code package, Funcraft first searches for specific manifest files in the code directory of the function. Then, Funcraft builds code, downloads dependencies, and compiles the code based on the manifest files.

Funcraft supports the following manifest files of mainstream package managers for different programming languages:

Features of the fun build command

Run the following command to query the features of the fun build command:
fun build -h
Expected output:
Usage: fun build [options] [[service/]function]

Build the dependencies.

Options:
  -d, --use-docker           Use docker container to build functions
  -t, --template [template]  The path of fun template file.
  -h, --help                 display help for command

Procedure

This section describes how to build a code package for delivery in Java 8.

  1. Create a template.yml file in the root directory of a function, such as /tmp/code/template.yml. For more information, see template.yml. The following sample code shows how to modify the file content:
    ROSTemplateFormatVersion: '2015-09-01'
    Resources:
      Services:
        Type: 'Aliyun::Serverless::Service'
        Properties:
          Policies:
            - AliyunOSSFullAccess
        Function:
          Type: 'Aliyun::Serverless::Function'
          Properties:
            Handler: main.main_handler
            Runtime: java8
            CodeUri: './'

    In the template.yml file, declare the Services service and the Function function for the Services service. Set the function handler to main.main_handler, the runtime of the function to java8, and the CodeUri property to the current directory. When you deploy the function, Funcraft packages the items in the directory that is specified by the CodeUri property and then uploads the package.

  2. Build the source code in one of the following ways:
    • Run the following command to directly build the source code:
      fun build
    • Run the following command to build the source code by specifying the function name. Replace <your functionName> with the name of your function, such as Function.
      fun build <your functionName>
    • Run the following command to build the source code by specifying the service and function names. Replace <your serviceName> and <your functionName> in <your serviceName>/<your functionName> with the names of your service and function, such as Services/Function.
      fun build <your serviceName>/<your functionName>
    The expected outputs of the preceding three commands are the same. Example:
    using template: template.yml
    start building function dependencies without docker
    
    building Services/Function
    
    Build Success
    
    Built artifacts: .fun\build\artifacts
    Built template: .fun\build\artifacts\template.yml
    
    Tips for next step
    ======================
    * Invoke Event Function: fun local invoke
    * Invoke Http Function: fun local start
    * Deploy Resources: fun deploy
    Note
    • By default, the source code that you build by running the fun build command is built on the host. To prevent incompatibility due to the environment difference after you deploy the function, you can add the -d or --use-docker parameter to the fun build command to compile the code in a simulated on-premises Function Compute environment.
    • If the template.yml file is not in the current directory, you can use the fun build -t templatePath command to specify the directory where the template.yml file is located.
    • The fun build command is run to build the source code of the functions that are described in the template.yml file. By default, if you do not specify a function whose source code is to be built, the source code of all the functions described in the template.yml file is built.

Additional information

You can use the following methods to build a function and perform other operations:
  • Run the following command to build all the functions described in the template.yml file:
    fun build 
  • Run the following command to build all the functions in a simulated on-premises Function Compute environment:
    fun build --use-docker 
  • Run the following command to build and invoke functions:
    fun build && fun local invoke
  • Run the following command to build and deploy functions:
    fun build && fun deploy  

Example

The following example shows you how to initialize, build, run, and modify Java code.

build_java