如何在 java azure 函数中添加依赖项 JAR [英] How to add dependency JAR in java azure functions
问题描述
有没有办法使用 JAVA 将第三方 jars 添加到 Azure 函数.我需要 json-simple jar 和 jackson-databind jar 在运行时可用于该函数.现在,我的代码抛出了一个运行时异常(ClassNotFound Exception),因为该函数在运行时无法引用该 jar,因为它不可用.
Is there a way to add third party jars to Azure functions using JAVA. I would need to have the json-simple jar and jackson-databind jars to be available for the function at run time. Right now, My code throws a runtime exception(ClassNotFound Exception) as the function is not able to reference the jar during runtime because it is unavailable.
我尝试使用 maven-shade-plugin.它确实创建了一个包含外部 jar 的可执行 jar,但部署仍采用原始 jar.
I tried using maven-shade-plugin. It does create an executable jar including the external jars but the deployment still takes the original 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>
推荐答案
我遇到了同样的问题,我想出了如何安排解决方案.
I had the same problem and I figured out how to arrange a solution.
首先,按照 这个链接.
假设 <project_root_path>
作为您将创建项目的文件夹.
Assume <project_root_path>
as the folder where you will create the project.
生成 maven 项目后,只需在 <project_root_path>/pom.xml
中的 < 中添加这个 maven-assembly-plugin 插件即可.build><plugins>...</plugins></build>
:
Once you have generated your maven project, just add this maven-assembly-plugin plugin on your <project_root_path>/pom.xml
within <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>
使用命令 mvn clean compile package
编译和打包 Azure Function 将在路径 <project_root_path>/target/<project_name>.jar
上生成一个 jar,其中包含所有pom.xml 的 <dependencies></dependencies>
下列出的外部库.
Compiling and packaging the Azure Function with command mvn clean compile package
will produce a jar on path <project_root_path>/target/<project_name>.jar
containing all the external libraries listed under the <dependencies></dependencies>
of the pom.xml.
注意1:如果你没有修改标准的pom.xml,
会根据生成版本>.jar
.
Note 1: if you didn't modify the standard pom.xml, <project_name>
will be generated according to <artifactId>_<version>.jar
.
注意 2:如果您不在上述代码段中使用 <appendAssemblyId>false</appendAssemblyId>
指令,则 <project_name>
将是 <artifactId>_<version>-<descriptorRef>.jar
.请考虑以下说明.
Note 2: if you don't use the <appendAssemblyId>false</appendAssemblyId>
directive on the above snippet, the <project_name>
will be <artifactId>_<version>-<descriptorRef>.jar
. Consider this for the following instructions.
所以,现在你应该有完整的<project_root_path>/target/
<project_root_path>/target/azure-functions/<azure_function_name>/
其中 <azure_function_name>
是您在创建过程中为函数指定的名称,记录在 以上链接.
So, now you should have your complete <project_root_path>/target/<project_name>.jar
, but before using it you have to copy it under <project_root_path>/target/azure-functions/<azure_function_name>/
where <azure_function_name>
is the name you gave to your function during the creation progress documented on the above link.
现在您可以使用 Azure Maven 插件对其进行测试:
Now you can test it using the Azure Maven plugins:
- 在本地机器上运行 Azure 函数:
mvn azure-functions:run
- 在订阅上部署 Azure 函数:
mvn azure-functions:deploy
最后,关键是将使用 maven-assembly-plugin 生成的 jar 移动到 Azure Maven 插件在运行/部署过程中将看到的正确位置.我希望我能找到一种使用标准 Maven 命令的更自动化的方法.
At the end, the key point is moving the jar generated with maven-assembly-plugin into the right place where the Azure Maven plugin will look during the run/deploy process. I wish I could find a more automatic way using standard Maven commands.
希望这会有所帮助.
Ciao知识产权
编辑(17 年 11 月 17 日)
在<configuration>
中添加<outputDirectory>${project.build.directory}/azure-functions/${functionAppName}</outputDirectory>
POM 标记并将 <goal>
标记更改为程序集,它使 Maven 自动复制 Azure Function 暂存目录中的最终 JAR.这允许 mvn azure-functions:run
和 mvn azure-functions:deploy
命令直接使用包含所有依赖项的正确 JAR 文件.不再需要手动操作.
Adding <outputDirectory>${project.build.directory}/azure-functions/${functionAppName}</outputDirectory>
within the <configuration>
POM tag and changing <goal>
tag to assembly, it makes Maven to automatically copy the final JAR in the Azure Function staging directory. This allows the mvn azure-functions:run
and mvn azure-functions:deploy
commands to directly use the correct JAR file containing all dependency. No manual actions are requested anymore.
以上 POM 已相应更新.
The above POM have been updated accordingly.
编辑(21/11/17)
如果您想使用 Maven Shade Plugin 而不是 Maven Assembly Plugin,请将上面的 XML 片段替换为这个:
In case you want to use the Maven Shade Plugin instead of the Maven Assembly Plugin, replace the above XML snippet with this one:
<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 命令工作.
It will work using the same standard Maven commands mentioned before.
这篇关于如何在 java azure 函数中添加依赖项 JAR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!