How to add dependency JAR in java azure functions

后端 未结 2 2052
Happy的楠姐
Happy的楠姐 2020-12-20 17:52

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

2条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-20 18:21

    I had the same problem and I figured out how to arrange a solution.

    First of all, start from a brand new Maven project following the straightforward guide at this link.

    Assume as the folder where you will create the project.

    Once you have generated your maven project, just add this maven-assembly-plugin plugin on your /pom.xml within ...:

    
       maven-assembly-plugin
       
          ${project.build.directory}/azure-functions/${functionAppName}
          false
          
             jar-with-dependencies
          
          
       
       
          
             make-assembly
             package
             
                assembly
             
          
       
    
    

    Compiling and packaging the Azure Function with command mvn clean compile package will produce a jar on path /target/.jar containing all the external libraries listed under the of the pom.xml.

    Note 1: if you didn't modify the standard pom.xml, will be generated according to _.jar.

    Note 2: if you don't use the false directive on the above snippet, the will be _-.jar. Consider this for the following instructions.

    So, now you should have your complete /target/.jar, but before using it you have to copy it under /target/azure-functions// where is the name you gave to your function during the creation progress documented on the above link.

    Now you can test it using the Azure Maven plugins:

    1. Running the Azure Function on your machine locally: mvn azure-functions:run
    2. Deploying the Azure Function on your subscription: mvn azure-functions:deploy

    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.

    Hope this helps.

    Ciao IP

    Edit (17/11/17)

    Adding ${project.build.directory}/azure-functions/${functionAppName} within the POM tag and changing 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.

    The above POM have been updated accordingly.

    Edit (21/11/17)

    In case you want to use the Maven Shade Plugin instead of the Maven Assembly Plugin, replace the above XML snippet with this one:

    
       org.apache.maven.plugins
       maven-shade-plugin
       3.1.0
       
          false
          ${project.build.directory}/azure-functions/${functionAppName}/${project.artifactId}-${project.version}.jar
          
             
                *:*
                
                   META-INF/*.SF
                   META-INF/*.DSA
                   META-INF/*.RSA
                
             
          
       
       
          
             package
             
                shade
             
          
       
    
    

    It will work using the same standard Maven commands mentioned before.

提交回复
热议问题