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

时间:2017-11-07 22:00:14

标签: maven azure 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>

2 个答案:

答案 0 :(得分:4)

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

首先,按照this link的简明指南,从一个全新的Maven项目开始。

假设<project_root_path>为您要创建项目的文件夹。

生成maven项目后,只需在<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> 添加 maven-assembly-plugin 插件:

mvn clean compile package

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

注1 :如果您未修改标准pom.xml,则会根据<artifactId>_<version>.jar生成<appendAssemblyId>false</appendAssemblyId>

注意2 :如果您未在上述代码段中使用<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>下,其中mvn azure-functions:run是您在创建过程中为您的函数指定的名称记录在above link

现在您可以使用Azure Maven插件测试它:

  1. 在本地计算机上运行Azure功能:mvn azure-functions:deploy
  2. 在订阅上部署Azure功能:<outputDirectory>${project.build.directory}/azure-functions/${functionAppName}</outputDirectory>
  3. 最后,关键点是将使用maven-assembly-plugin生成的jar移动到Azure Maven插件在运行/部署过程中所看到的正确位置。我希望我能找到一种使用标准Maven命令的更自动的方式。

    希望这有帮助。

    侨 IP

    编辑(17/11/17)

    <configuration> POM标记中添加<goal>并将mvn azure-functions:run标记更改为程序集,它使Maven自动复制Azure功能登台目录中的最终JAR。这允许mvn azure-functions:deploy<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> 命令直接使用包含所有依赖项的正确JAR文件。不再需要手动操作。

    上述POM已相应更新。

    编辑(21/11/17)

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

    innerHTML

    它将使用之前提到的相同标准Maven命令。

答案 1 :(得分:0)

您用于准备Azure Function项目的pom.xml生成的azure-functions-archetype的最新版本(有关更多详细信息,请参见this link)似乎已经包含用于复制依赖项的插件。

我的pom.xml默认情况下包括以下插件,并且它似乎自动将我指定的依赖项复制到${stagingDirectory}/lib中。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <outputDirectory>${stagingDirectory}/lib</outputDirectory>
                <overWriteReleases>false</overWriteReleases>
                <overWriteSnapshots>false</overWriteSnapshots>
                <overWriteIfNewer>true</overWriteIfNewer>
                <includeScope>runtime</includeScope>
                <excludeArtifactIds>azure-functions-java-library</excludeArtifactIds>
            </configuration>
        </execution>
    </executions>
</plugin>