【问题标题】:How to add dependency JAR in java azure functions如何在 java azure 函数中添加依赖项 JAR
【发布时间】:2017-11-07 22:00:13
【问题描述】:

有没有办法使用 JAVA 将第三方 jars 添加到 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>

【问题讨论】:

  • 向我们展示您迄今为止的尝试!带有阴影插件的pom.xml 是什么样的?
  • 同样的问题!

标签: maven azure azure-functions


【解决方案1】:

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

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

假设&lt;project_root_path&gt; 是您将在其中创建项目的文件夹。

生成 maven 项目后,只需将这个 ma​​ven-assembly-plugin 插件添加到 &lt;project_root_path&gt;/pom.xml&lt;build&gt;&lt;plugins&gt;...&lt;/plugins&gt;&lt;/build&gt; 中即可:

<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>

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

注意1:如果没有修改标准的pom.xml,会根据&lt;artifactId&gt;_&lt;version&gt;.jar生成&lt;project_name&gt;

注意 2:如果你不在上面的 sn-p 上使用 &lt;appendAssemblyId&gt;false&lt;/appendAssemblyId&gt; 指令,&lt;project_name&gt; 将是 &lt;artifactId&gt;_&lt;version&gt;-&lt;descriptorRef&gt;.jar。请考虑以下说明。

所以,现在您应该拥有完整的&lt;project_root_path&gt;/target/&lt;project_name&gt;.jar,但在使用它之前,您必须将其复制到&lt;project_root_path&gt;/target/azure-functions/&lt;azure_function_name&gt;/ 下,其中&lt;azure_function_name&gt; 是您在above link 中记录的创建过程中为您的函数命名的名称.

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

  1. 在本地机器上运行 Azure 函数:mvn azure-functions:run
  2. 在订阅上部署 Azure 函数:mvn azure-functions:deploy

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

希望这会有所帮助。

Ciao 知识产权

编辑(17 年 11 月 17 日)

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

以上 POM 已相应更新。

编辑(21/11/17)

如果你想使用 Maven Shade Plugin 而不是 Maven Assembly Plugin,请将上面的 XML sn-p 替换为这个:

<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>

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

【讨论】:

  • +1 - 这正是我所做的。是的!将 jar 复制到 target/azure-functions// 的额外工作是开销,我正在寻找自动化。微软推荐的方法是构建一个 FAT jar !!!
  • 好吧,我对创建包含所有依赖项的 jar 的需要并不感到惊讶,这也是标准老式场景(如在容器上部署)的常用方法(想想你用什么Tomcat,使用包含外部 jar 库的 war)。
  • @mack:我找到了一种无需其他命令即可自动执行 JAR 复制的解决方案。请让我知道它是否适用于您的情况。
  • 谢谢!我已经修改了脚本,以便我可以按照 MS 的建议构建 FAT jar。我尝试使用您的建议,我可以看到自定义 JAR 在 ${project.build.directory}/azure-functions//target 被替换,但没有被推送到 ${project.build.directory}/azure-functions/ ${functionAppName}。理想情况下,根据我的理解,构建的 jar 放置在 2 个位置 - 一个在目标文件夹中,然后将其推送到 ${project.build.directory}/azure-functions/${functionAppName} 位置。跨度>
  • @mack:你试过我的整个 pom.xml 了吗?它将最终的“FAT”jar 放在 ${project.build.directory}/azure-functions/${functionAppName} 中,其中 functionAppName 它是您的 Azure 函数的名称。我没有改变设置过程中自动生成的默认 pom.xml。
【解决方案2】:

azure-functions-archetype 生成的最新版本pom.xml 用于准备 Azure Function 项目(有关详细信息,请参阅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>

【讨论】:

  • 我应该在 pom.xml 中添加 Jar 依赖吗?还是只是在lib文件夹中放一个jar?
  • 我使用 mvn install:install-file 命令将我的 .jar 库添加到本地存储库。然后我将该库作为依赖项添加到 pom.xml
猜你喜欢
  • 2015-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-28
  • 2011-01-25
相关资源
最近更新 更多