gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 12:28:02 24 4
gpt4 key购买 nike

有没有办法使用 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>

最佳答案

我遇到了同样的问题,我找到了解决方案。

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

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

生成 Maven 项目后,只需在 <project_root_path>/pom.xml 上添加此 ma​​ven-assemble-plugin 插件即可<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包含 <dependencies></dependencies> 下列出的所有外部库pom.xml 的。

注解1:如果你没有修改标准的pom.xml,<project_name>会根据<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>是您在 above link 记录的创建过程中为函数指定的名称.

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

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

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

希望这有帮助。

再见知识产权

编辑 (17/11/17)

添加<outputDirectory>${project.build.directory}/azure-functions/${functionAppName}</outputDirectory> <configuration>内POM标签及更改<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>

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

关于maven - 如何在java azure函数中添加依赖JAR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47168154/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com