0

Problem description

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?


Minimal example

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.

kdr
  • 93
  • 8
  • Does the `azure-functions-module` module reference the dependency of the `dependency-module` module? – Frank Borzage Oct 19 '20 at 09:15
  • Yes, I have the suitable dependency in azure-functions-module's pom.xml: `` `org.example` `dependency-module` `1.0-SNAPSHOT` `` – kdr Oct 19 '20 at 09:38
  • Stupid question: did you analyse the packaged result if your library is packed together with it? Like unpacking it? – Peter Branforn Oct 20 '20 at 08:16
  • You mean checking whether the dependency is not inside the jar? No, it's not there. – kdr Oct 20 '20 at 14:44
  • I have run into the same problem, but I concur with Tomek's conclusion. The IntelliJ plugin does not use the Maven pom, nor the Maven build, but IntelliJ's internal project structure. See Project Settings/Modules/Dependencies: when two modules are open in the same project, they are listed as module dependencies, not Maven:dependencies. I am fairly certain that the Azure plugin is unable to process those. When the upstream modules are not in the same project, OR, I run the app via maven azure-functions:run, everything is fine – Davide Sottara Nov 04 '22 at 03:24
  • 1
    Looks like the issue has been addressed https://github.com/microsoft/azure-tools-for-java/issues/7063 – Davide Sottara Dec 05 '22 at 22:54

2 Answers2

1

Please have a look at this other question here: 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.

Peter Branforn
  • 1,679
  • 3
  • 17
  • 29
  • Thanks, the link is really useful. Following the approach described there, I have managed to make Maven create a jar with dependencies instead of the "simple" one. However, this only in the target folder of my project. On run/deploy the .jar is still the old one, and I'm quite perplexed about this. I need to spend more time investigating this. – kdr Oct 20 '20 at 14:49
1

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.

Tomek
  • 11
  • 2