如何在java azure函数中添加依赖项JAR

mac*_*ack 7 azure maven azure-functions

有没有办法使用JAVA将第三方jar添加到Azure功能.我需要让json-simple jar和jackson-databind jar在运行时可用于该函数.现在,我的代码抛出一个运行时异常(ClassNotFound Exception),因为该函数在运行时无法引用jar,因为它不可用.

我尝试使用maven-shade-plugin.它确实创建了一个包含外部jar的可执行jar,但部署仍然采用原始jar.

请建议.

谢谢.

的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.sce.api.learning</groupId>
    <artifactId>myApi</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>Azure Java Functions</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <functionAppName>mckapi-http-nov2</functionAppName>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.microsoft.azure</groupId>
            <artifactId>azure-functions-java-core</artifactId>
            <version>1.0.0-beta-1</version>
        </dependency>

        <!-- Adding GSON dependancy -->
        <dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>1.4</version>
</dependency>

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.6.3</version>
</dependency>

<dependency>
    <groupId>com.googlecode.json-simple</groupId>
    <artifactId>json-simple</artifactId>
    <version>1.1.1</version>
</dependency>
<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20171018</version>
</dependency>

        <!-- Test -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <groupId>com.microsoft.azure</groupId>
                    <artifactId>azure-functions-maven-plugin</artifactId>
                    <version>0.1.4</version>
                </plugin>
            </plugins>
        </pluginManagement>

        <plugins>
            <plugin>
                <groupId>com.microsoft.azure</groupId>
                <artifactId>azure-functions-maven-plugin</artifactId>
                <configuration>
                    <resourceGroup>java-functions-group</resourceGroup>
                    <appName>${functionAppName}</appName>
                    <region>westus2</region>
                    <appSettings>
                        <property>
                            <name>FUNCTIONS_EXTENSION_VERSION</name>
                            <value>beta</value>
                        </property>
                    </appSettings>
                </configuration>
                <executions>
                    <execution>
                        <id>package-functions</id>
                        <goals>
                            <goal>package</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <overwrite>true</overwrite>
                            <outputDirectory>${project.build.directory}/azure-functions/${functionAppName}
                            </outputDirectory>
                            <resources>
                                <resource>
                                    <directory>${project.basedir}</directory>
                                    <includes>
                                        <include>host.json</include>
                                        <include>local.settings.json</include>
                                        **<include>**/*.jar</include>**<!-- This includes the jar files in the target/lib folder -->
                                    </includes>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                        <overwrite>true</overwrite>
                            <outputDirectory>${project.build.directory}/azure-functions/${functionAppName}
                            </outputDirectory>
              <shadedArtifactAttached>false</shadedArtifactAttached>
            </configuration>
                    </execution>
                </executions>
                <configuration>
                    <finalName>${artifactId}-${version}</finalName>
                </configuration>
            </plugin>

        </plugins>

    </build>

</project>
Run Code Online (Sandbox Code Playgroud)

iak*_*kko 5

我遇到了同样的问题,我想出了如何安排解决方案。

首先,按照此链接中的简单指南从一个全新的 Maven 项目开始。

假设<project_root_path>您将在其中创建项目的文件夹。

生成 Maven 项目后,只需在您的inside中添加此maven-assemble-plugin插件即可:<project_root_path>/pom.xml<build><plugins>...</plugins></build>

<plugin>
   <artifactId>maven-assembly-plugin</artifactId>
   <configuration>
      <outputDirectory>${project.build.directory}/azure-functions/${functionAppName}</outputDirectory>
      <appendAssemblyId>false</appendAssemblyId>
      <descriptorRefs>
         <descriptorRef>jar-with-dependencies</descriptorRef>
      </descriptorRefs>
      <archive />
   </configuration>
   <executions>
      <execution>
         <id>make-assembly</id>
         <phase>package</phase>
         <goals>
            <goal>assembly</goal>
         </goals>
      </execution>
   </executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)

使用命令编译和打包 Azure Functionmvn clean compile package会在路径上生成一个 jar ,其中包含pom.xml<project_root_path>/target/<project_name>.jar下列出的所有外部库。<dependencies></dependencies>

注1:如果没有修改标准的pom.xml,<project_name>将会按照.xml生成<artifactId>_<version>.jar

注 2:如果您不在<appendAssemblyId>false</appendAssemblyId>上面的代码片段中使用该指令,则将<project_name><artifactId>_<version>-<descriptorRef>.jar. 请在以下说明中考虑这一点。

因此,现在您应该已经完成​​了<project_root_path>/target/<project_name>.jar,但在使用它之前,您必须将其复制到上面链接中记录的创建过程中为函数指定的名称的<project_root_path>/target/azure-functions/<azure_function_name>/位置。<azure_function_name>

现在您可以使用 Azure Maven 插件对其进行测试:

  1. 在本地计算机上运行 Azure Function:mvn azure-functions:run
  2. 在订阅上部署 Azure Function:mvn azure-functions:deploy

最后,关键点是将使用 maven- assembly-plugin 生成的 jar 移动到 Azure Maven 插件在运行/部署过程中将查找的正确位置。我希望我能找到一种使用标准 Maven 命令的更自动化的方法。

希望这可以帮助。

再见IP

编辑 (17/11/17)

添加POM<outputDirectory>${project.build.directory}/azure-functions/${functionAppName}</outputDirectory>标记 <configuration>并将<goal>标记更改为程序集,它使 Maven 自动将最终的 JAR 复制到 Azure Function 暂存目录中。这允许mvn azure-functions:runmvn azure-functions:deploy命令直接使用包含所有依赖项的正确 JAR 文件。不再需要手动操作。

上述 POM 已相应更新。

编辑 (21/11/17)

如果您想使用 Maven Shade 插件而不是 Maven Assembly 插件,请将上面的 XML 片段替换为以下内容:

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-shade-plugin</artifactId>
   <version>3.1.0</version>
   <configuration>
      <shadedArtifactAttached>false</shadedArtifactAttached>
      <outputFile>${project.build.directory}/azure-functions/${functionAppName}/${project.artifactId}-${project.version}.jar</outputFile>
      <filters>
         <filter>
            <artifact>*:*</artifact>
            <excludes>
               <exclude>META-INF/*.SF</exclude>
               <exclude>META-INF/*.DSA</exclude>
               <exclude>META-INF/*.RSA</exclude>
            </excludes>
         </filter>
      </filters>
   </configuration>
   <executions>
      <execution>
         <phase>package</phase>
         <goals>
            <goal>shade</goal>
         </goals>
      </execution>
   </executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)

它将使用前面提到的相同标准 Maven 命令来工作。