arm*_*mer 5 java eclipse azure maven azure-functions
我编写了一些代码来在代码运行时创建一个文件。我的计划是按照定期计划将其放在 Microsoft Azure 函数应用程序上,因此它每小时或每天运行一次。我将使用计时器触发器并在那里也有一个 HTTP 请求触发器进行测试,但是,我不知道在哪里放入我自己的代码以使其启动并运行以及外部 java 库。
创建函数应用程序时给出的基本代码可以工作并使用maven运行,但是当我创建另一个函数并将我自己的代码放入其中时,它会崩溃并说我使用的外部java库有很多错误,JSON,JSON简单,Apache Commons。
我正在寻找的最终结果是能够将我的代码放入并让程序按计划运行并创建我想要的文件。目前,无论我在何处插入代码,都会遇到错误。
更新:我认为问题可能出在我正在使用的外部库上,但我不知道为什么它们会导致问题或我将如何解决它。我需要外部库来使我的代码正常工作。我将它们添加到项目构建路径中,因此它们应该可以正常工作。对于所有导入的库,它给我错误“包 org.apache.commons.io 不存在”等等
您可以按照@Caiyi提供的指南进行操作,或者按照我过去做过的一些详细步骤进行操作。
\n\n例如,我想在我的 Java 函数应用程序中使用 Azure blob 存储 sdk。
\n\n功能类:
\n\npackage com.fabrikam.functions;\n\nimport com.microsoft.azure.serverless.functions.annotation.*;\nimport com.microsoft.azure.serverless.functions.ExecutionContext;\n\nimport com.microsoft.azure.storage.*;\nimport com.microsoft.azure.storage.blob.*;\n\n/**\n * Hello function with HTTP Trigger.\n */\npublic class Function {\n\n // Configure the connection-string with your values\n public static final String storageConnectionString =\n "DefaultEndpointsProtocol=http;" +\n "AccountName=***;" +\n "AccountKey=***";\n\n @FunctionName("hello")\n public String hello(@HttpTrigger(name = "req", methods = {"get", "post"}, authLevel = AuthorizationLevel.ANONYMOUS) String req,\n ExecutionContext context) {\n\n try {\n // Retrieve storage account from connection-string.\n CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);\n\n // Create the blob client.\n CloudBlobClient blobClient = storageAccount.createCloudBlobClient();\n\n // Get a reference to a container.\n // The container name must be lower case\n CloudBlobContainer container = blobClient.getContainerReference(req);\n\n // Create the container if it does not exist.\n container.createIfNotExists();\n\n return String.format("Hello, I get container name : %s!", container.getName());\n\n } catch (Exception e) {\n // Output the stack trace.\n e.printStackTrace();\n return "Access Error!";\n }\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\nPom.xml\xef\xbc\x9a
\n\n<?xml version="1.0" encoding="UTF-8"?>\n<project xmlns="http://maven.apache.org/POM/4.0.0"\n xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\n xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">\n <modelVersion>4.0.0</modelVersion>\n\n <groupId>com.fabrikam.functions</groupId>\n <artifactId>fabrikam-functions</artifactId>\n <version>1.0-SNAPSHOT</version>\n <packaging>jar</packaging>\n\n <name>Azure Java Functions</name>\n\n <properties>\n <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>\n <maven.compiler.source>1.8</maven.compiler.source>\n <maven.compiler.target>1.8</maven.compiler.target>\n <functionAppName>fabrikam-functions-20171017112209094</functionAppName>\n </properties>\n\n <dependencies>\n <dependency>\n <groupId>com.microsoft.azure</groupId>\n <artifactId>azure-functions-java-core</artifactId>\n <version>1.0.0-beta-1</version>\n </dependency>\n\n <!-- https://mvnrepository.com/artifact/com.microsoft.azure/azure-storage -->\n <dependency>\n <groupId>com.microsoft.azure</groupId>\n <artifactId>azure-storage</artifactId>\n <version>6.0.0</version>\n </dependency>\n\n <!-- Test -->\n <dependency>\n <groupId>junit</groupId>\n <artifactId>junit</artifactId>\n <version>4.12</version>\n <scope>test</scope>\n </dependency>\n </dependencies>\n\n <build>\n <pluginManagement>\n <plugins>\n <plugin>\n <artifactId>maven-resources-plugin</artifactId>\n <version>3.0.2</version>\n </plugin>\n <plugin>\n <groupId>com.microsoft.azure</groupId>\n <artifactId>azure-functions-maven-plugin</artifactId>\n <version>0.1.4</version>\n </plugin>\n </plugins>\n </pluginManagement>\n\n <plugins>\n <plugin>\n <groupId>com.microsoft.azure</groupId>\n <artifactId>azure-functions-maven-plugin</artifactId>\n <configuration>\n <resourceGroup>java-functions-group</resourceGroup>\n <appName>${functionAppName}</appName>\n <region>westus2</region>\n <appSettings>\n <property>\n <name>FUNCTIONS_EXTENSION_VERSION</name>\n <value>beta</value>\n </property>\n </appSettings>\n </configuration>\n <executions>\n <execution>\n <id>package-functions</id>\n <goals>\n <goal>package</goal>\n </goals>\n </execution>\n </executions>\n </plugin>\n <plugin>\n <artifactId>maven-resources-plugin</artifactId>\n <executions>\n <execution>\n <id>copy-resources</id>\n <phase>package</phase>\n <goals>\n <goal>copy-resources</goal>\n </goals>\n <configuration>\n <overwrite>true</overwrite>\n <outputDirectory>${project.build.directory}/azure-functions/${functionAppName}\n </outputDirectory>\n <resources>\n <resource>\n <directory>${project.basedir}</directory>\n <includes>\n <include>host.json</include>\n <include>local.settings.json</include>\n </includes>\n </resource>\n </resources>\n </configuration>\n </execution>\n </executions>\n </plugin>\n </plugins>\n\n </build>\n\n</project>\nRun Code Online (Sandbox Code Playgroud)\n\n然后使用命令mvn clean package将你的maven项目打包成jar包。
使用命令mvn azure-functions:run在本地运行 Azure 函数。
现在,如果您运行Azure功能,您可能会看到以下问题。
\n\n\n\n\njava.lang.NoClassDefFoundError:\n com/microsoft/azure/storage/CloudStorageAccount
\n
Exception:\nStack: java.lang.reflect.InvocationTargetException\n[10/25/2017 2:48:44 AM] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\n[10/25/2017 2:48:44 AM] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)\n[10/25/2017 2:48:44 AM] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)[10/25/2017 2:48:44 AM] at java.lang.reflect.Method.invoke(Method.java:498)\n[10/25/2017 2:48:44 AM] at com.microsoft.azure.webjobs.script.broker.JavaMethodInvokeInfo.invoke(JavaMethodInvokeInfo.java:19)\n[10/25/2017 2:48:44 AM] at com.microsoft.azure.webjobs.script.broker.JavaMethodExecutor.execute(JavaMethodExecutor.java:34)\n[10/25/2017 2:48:44 AM] at com.microsoft.azure.webjobs.script.broker.JavaFunctionBroker.invokeMethod(JavaFunctionBroker.java:40)\n[10/25/2017 2:48:44 AM] at com.microsoft.azure.webjobs.script.handler.InvocationRequestHandler.execute(InvocationRequestHandler.java:33)\n[10/25/2017 2:48:44 AM] at com.microsoft.azure.webjobs.script.handler.InvocationRequestHandler.execute(InvocationRequestHandler.java:10)\n[10/25/2017 2:48:44 AM] at com.microsoft.azure.webjobs.script.handler.MessageHandler.handle(MessageHandler.java:41)\n[10/25/2017 2:48:44 AM] at com.microsoft.azure.webjobs.script.JavaWorkerClient$StreamingMessagePeer.lambda$onNext$0(JavaWorkerClient.java:84)\n[10/25/2017 2:48:44 AM] at java.util.concurrent.ForkJoinTask$AdaptedRunnableAction.exec(ForkJoinTask.java:1386)\n[10/25/2017 2:48:44 AM] at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)\n[10/25/2017 2:48:44 AM] at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)\n[10/25/2017 2:48:44 AM] at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)\n[10/25/2017 2:48:44 AM] at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)\n[10/25/2017 2:48:44 AM] Caused by: java.lang.NoClassDefFoundError: com/microsoft/azure/storage/CloudStorageAccount\n[10/25/2017 2:48:44 AM] at com.fabrikam.functions.Function.hello(Function.java:26)\n[10/25/2017 2:48:44 AM] ... 16 more\n[10/25/2017 2:48:44 AM] Caused by: java.lang.ClassNotFoundException: com.microsoft.azure.storage.CloudStorageAccount\n[10/25/2017 2:48:44 AM] at java.net.URLClassLoader.findClass(URLClassLoader.java:381)\n[10/25/2017 2:48:44 AM] at java.lang.ClassLoader.loadClass(ClassLoader.java:424)\n[10/25/2017 2:48:44 AM] at java.lang.ClassLoader.loadClass(ClassLoader.java:357)\n[10/25/2017 2:48:44 AM] ... 17 more\n[10/25/2017 2:48:44 AM] .\n[10/25/2017 2:48:44 AM] Function had errors. See Azure WebJobs SDK dashboard for details. Instance ID is \'3450abda-99a0-4d75-add2-a7bc48a0cb51\'\n[10/25/2017 2:48:44 AM] System.Private.CoreLib: Exception while executing function: Functions.hello. System.Private.CoreLib: Result:\nRun Code Online (Sandbox Code Playgroud)\n\n因为jar包里没有 dependent jar packages.
因此,请将如下配置片段添加到我的pom.xml
<plugin>\n <artifactId>maven-assembly-plugin</artifactId>\n <configuration>\n <descriptorRefs>\n <descriptorRef>jar-with-dependencies</descriptorRef>\n </descriptorRefs>\n <archive>\n <manifest>\n <mainClass>Your main class path</mainClass>\n </manifest>\n </archive>\n </configuration>\n <executions>\n <execution>\n <id>make-assembly</id>\n <phase>package</phase>\n <goals>\n <goal>single</goal>\n </goals>\n </execution>\n </executions>\n </plugin>\nRun Code Online (Sandbox Code Playgroud)\n\n然后使用命令mvn-clean-package,你会看到生成了两个jar文件。
一是不包含dependent jar packages,二是包含dependent jar packages。
将fabrikam-functions-1.0-SNAPSHOT-jar-with-dependenciesjar 移动到路径\xef\xbc\x9a${project.basedir}/target/azure-functions/${function-app-name}/
对我来说,它看起来像E:\\TestAzureFunction\\fabrikam-functions\\target\\azure-functions\\fabrikam-functions-20171017112209094。
不要忘记将 jar 重命名为fabrikam-functions-1.0-SNAPSHOT.
最后,成功运行azure函数,并通过url获取输出结果:http://localhost:7071/api/hello。
另外,你可以参考这个github文档来了解更多关于azure的配置细节function maven plugin。
| 归档时间: |
|
| 查看次数: |
2425 次 |
| 最近记录: |