英文:
How do I add Java Azure Functions in a Maven submodule
问题
<h3>问题描述</h3>
我有一个Java库A,我创建了一个新的Azure Functions模块B(遵循https://learn.microsoft.com/en-us/azure/azure-functions/functions-create-maven-intellij),应该将A作为依赖项。
现在:
- 如果我将B创建为新的**独立**模块,它可以工作;
- 如果我将B添加为共同父模块C的另一个**子模块**,它就无法工作。
更具体地说:我的函数的编译、运行和部署都没有问题,但是当它们中的任何一个被触发时,我会为A中定义的每个类得到一个**ClassNotFoundException**。不出奇,因为A.jar不在Azure函数运行文件夹的*lib*子文件夹中(类似于在本地运行的情况下,在我的Windows PC上是*{myUser}\AppData\Local\Temp\azure-functions16636049357177434984*)。但我不明白为什么依赖的jar文件不会被复制到那里,特别是在编译之后,它在我的IntelliJ项目的*target\azure-functions\taxclaims-azure-1596530670055\lib*文件夹中存在。
为什么会发生这种情况,如何解决?
----
<h3>最小示例</h3>
我有一个名为**TestParent**的Java Maven项目,有两个模块:
- ***dependency-module***
- ***azure-functions-module***(依赖于***dependency-module***)
在***dependency-module***中,我有一个空体的*DependencyClass*,而在***azure-functions-module***中,我有一个具有以下方法的*HttpTriggerFunction*类:
@FunctionName("TestFunction")
public HttpResponseMessage run(
@HttpTrigger(name = "req", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional
{
DependencyClass instance = new DependencyClass();
return request.createResponseBuilder(HttpStatus.OK).body("OK").build();
}
这个模块可以成功部署,但是当触发*TestFunction*时,我会得到*DependencyClass*的**ClassNotFoundException**。
然而,如果我创建**另一个**名为**TestStandaloneParent**的项目,有一个名为***standalone-azure-functions-module***的模块,以及与上面相同的函数,那么一切都能顺利运行。
*如果需要的话,我可以添加更多细节,特别是pom.xml文件。*
英文:
<h3> Problem description </h3>
I have my Java library A and I create a new Azure Functions module B (following https://learn.microsoft.com/en-us/azure/azure-functions/functions-create-maven-intellij), which should use A as a dependency.
Now:
- if I create B as a new and standalone module, it works;
- if I add B as another submodule of a common parent C, it does not.
More specifically: no problems with compilation, running and deployment of my functions, but when any of them is triggered I get a ClassNotFoundException for each class defined in A. And no wonder it happens, since A.jar is not present in the lib subfolder of the Azure Function's run folder (something like {myUser}\AppData\Local\Temp\azure-functions16636049357177434984 on my Windows PC in case of local run). But I don't understand why the dependency jar is not copied there, especially that after compilation it's present in the target\azure-functions\taxclaims-azure-1596530670055\lib folder of my IntelliJ project.
Why does this happen and how to fix it?
<h3> Minimal example </h3>
I have TestParent Java Maven project, with two modules:
- dependency-module
- azure-functions-module (depending on dependency-module)
In dependency-module I have a DependencyClass with empty body, whereas in azure-functions-module I have HttpTriggerFunction class with the following method:
@FunctionName("TestFunction")
public HttpResponseMessage run(
@HttpTrigger(name = "req", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request, final ExecutionContext context)
{
DependencyClass instance = new DependencyClass();
return request.createResponseBuilder(HttpStatus.OK).body("OK").build();
}
This module can be successfully deployed, but when the TestFunction is triggered, I obtain ClassNotFoundException for DependencyClass.
However, if I create another project, called TestStandaloneParent, with a module standalone-azure-functions-module and the same function as above, then everything works smoothly.
If needed of course I can add further details, especially the pom.xml files.
答案1
得分: 1
请查看这个问题:https://stackoverflow.com/questions/47168154/how-to-add-dependency-jar-in-java-azure-functions
这似乎是与打包有关的问题,您需要通过手动使用Maven Assembly插件配置进行调整。
英文:
Please have a look at this other question here: https://stackoverflow.com/questions/47168154/how-to-add-dependency-jar-in-java-azure-functions
It looks like a problem with the packaging, you need to tweak it by using the maven assembly plugin config manually.
答案2
得分: 1
看起来 IntelliJ Azure 插件并没有使用 pom.xml 文件。
你可以这样做:进入 "File" -> "Project Structure" -> "Project Settings" -> "Modules",选择包含 Azure Functions 的模块,然后手动添加 JAR 文件作为依赖。
在我的情况下,我实现了这个结果,但对解决方法不太满意。
英文:
It looks like IntelliJ Azure Plugin doesn't use pom.xml.
What you can do is to go File->Project structure->Project settings->Modules, choose module with Azure Functions and manually add jar fil as a dependency.
In my case I achieve the result, but I'm not happy with the way of solution.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论