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

Background information

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 Serverless Application Model. Then, add the following content to the template.yml file:
    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 the CodeUri property specifies 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 bulid
    • 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 bulid <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 bulid <your serviceName>/<your functionName>
    The expected outputs of the preceding three commands are the same, as shown in the following 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. 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 by default.

References

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