使用 Maven 打包 Java 或 Scala 处理程序代码¶
如果您使用 Maven 构建和打包代码,则可以使用 Maven Assembly 插件 (https://maven.apache.org/plugins/maven-assembly-plugin/index.html) 创建包含所有依赖项的 JAR 文件。
有了 JAR 文件后,可以将该文件上传到 Snowflake 暂存区,然后在创建函数或过程时在 IMPORTS 语句中引用该文件。有关上传 JAR 文件的更多信息,请参阅 为代码提供依赖项。如需进一步了解是选择将代码内联,还是选择将代码存储在暂存区中,请参阅 将处理程序代码保持内联或保留在暂存区。
要创建包含处理程序代码的 JAR 文件,请使用以下步骤。
- 在项目的目录(例如, - hello-snowpark/)中,创建一个名为- assembly/的子目录。
- 在该目录中,创建一个 程序集描述符文件 (https://maven.apache.org/plugins/maven-assembly-plugin/assembly.html),该文件指定要在 JAR 文件中包含的依赖项。 - 有关示例,请参阅 jar-with-dependencies (https://maven.apache.org/plugins/maven-assembly-plugin/descriptor-refs.html#jar-with-dependencies)。 
- 如果项目需要 Snowpark 库,请从输出存档中排除该库的 JAR 文件,因为该库已包含在 Snowflake 中。 - 在程序集描述符中,添加一个 - <dependencySet>元素,该元素从 JAR 文件中排除 Snowpark 库。- 例如: - <assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.0 http://maven.apache.org/xsd/assembly-2.1.0.xsd"> <id>jar-with-dependencies</id> <formats> <format>jar</format> </formats> <includeBaseDirectory>false</includeBaseDirectory> <dependencySets> <dependencySet> <outputDirectory>/</outputDirectory> <useProjectArtifact>false</useProjectArtifact> <unpack>true</unpack> <scope>provided</scope> <excludes> <exclude>com.snowflake:snowpark</exclude> </excludes> </dependencySet> </dependencySets> </assembly> - 有关程序集描述符中的元素的信息,请参阅 程序集描述符格式 (https://maven.apache.org/plugins/maven-assembly-plugin/assembly.html)。 
- 在 - pom.xml文件中,在- <project>»- <build>»- <plugins>下,为 Maven Assembly Plugin 添加一个- <plugin>元素。- 此外,在 - <configuration>»- <descriptors>下,添加一个- <descriptor>,它指向您在前面步骤中创建的程序集描述符文件。- 例如: - <project> [...] <build> [...] <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>3.3.0</version> <configuration> <descriptors> <descriptor>src/assembly/jar-with-dependencies.xml</descriptor> </descriptors> </configuration> [...] </plugin> [...] </plugins> [...] </build> [...] </project>