NoClassDefFoundError when 运行 application with custom module

NoClassDefFoundError when running application with custom module

我已经使用内部使用 com.googlecode.json-simple 的 Mule SDK 构建了一个 Mule 4 模块。为了创建项目,我使用了 Maven 原型 org.mule.extensions:mule-extensions-archetype-maven-plugin:1.2.0,如 https://docs.mulesoft.com/mule-sdk/1.1/getting-started

中所述

我能够构建项目并将其包含在 Anypoint Studio 7.8 中并利用我的 Mule 应用程序中的操作,我可以配置它们,更新其在 UI 中的属性,但是每当我运行 应用程序失败 java.lang.NoClassDefFoundError 因为找不到库。

我应该进行哪些额外配置以确保库在 运行 时间可用?

错误:

ERROR 2021-04-09 06:33:48,410 [WrapperListener_start_runner] org.mule.runtime.module.deployment.internal.DefaultArchiveDeployer: Failed to deploy artifact [project1]
org.mule.runtime.deployment.model.api.DeploymentException: Failed to deploy artifact [project1]
Caused by: java.lang.NoClassDefFoundError: com/google/gson/JsonObject
    at java.lang.Class.getDeclaredMethods0(Native Method) ~[?:1.8.0_232]
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701) ~[?:1.8.0_232]
    at java.lang.Class.getDeclaredMethods(Class.java:1975) ~[?:1.8.0_232]
    at org.mule.runtime.module.extension.internal.util.IntrospectionUtils.lambda$getMethodsStream(IntrospectionUtils.java:923) ~[mule-module-extensions-support-4.3.0-20210119.jar:4.3.0-20210119]
    at java.util.stream.ReferencePipeline.accept(ReferencePipeline.java:269) ~[?:1.8.0_232]
    at java.util.stream.ReferencePipeline.accept(ReferencePipeline.java:175) ~[?:1.8.0_232]
    at java.util.HashMap$KeySpliterator.forEachRemaining(HashMap.java:1556) ~[?:1.8.0_232]
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:482) ~[?:1.8.0_232]
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472) ~[?:1.8.0_232]

pom.xml 模块

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

<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>org.mule.extension</groupId>
    <artifactId>mule-basic-extension</artifactId>
    <version>1.0.1-SNAPSHOT</version>
    <packaging>mule-extension</packaging>
    <name>My Extension</name> 

    <parent>
        <groupId>org.mule.extensions</groupId>
        <artifactId>mule-modules-parent</artifactId>
        <version>1.1.3</version>
    </parent>

    <dependencies>
        <dependency>
           <groupId>com.googlecode.json-simple</groupId>
           <artifactId>json-simple</artifactId>
           <version>1.1.1</version>
        </dependency>
     </dependencies>

</project>

编辑: 操作 class

public class MyOperations {
  Logger logger = LoggerFactory.getLogger(MyOperations.class);


  /**
   * Example of an operation that uses the configuration and a connection instance to perform some action.
   */
  @MediaType(value = ANY, strict = false)
  public String retrieveInfo(@Config MyConfiguration configuration, @Connection MyConnection connection){
    return "Using Configuration [" + configuration.getConfigId() + "] with Connection id [" + connection.getId() + "]";
  }

  /**
   * Example of a simple operation that receives a string parameter and returns a new string message that will be set on the payload.
   */
  @MediaType(value = ANY, strict = false)
  public String sayHi(String person) {
    return buildHelloMessage(person);
  }

  /**
   * Returns a JSON representation of the input
   */
  @Throws(ExecuteErrorsProvider.class)
  @MediaType(value = MediaType.APPLICATION_JSON, strict = false)
  public String parseInput(String record) {
    JsonObject jsonObject = new JsonObject();

    jsonObject.addProperty("key", record);

    return jsonObject.toString();
  }


  /**
   * Private Methods are not exposed as operations
   */
  private String buildHelloMessage(String person) {
    return "Hello " + person + "!!!";
  }
}

事实证明,我在项目中使用的 JSON 库存在问题。

我替换了这个依赖

<dependency>
  <groupId>com.googlecode.json-simple</groupId>
  <artifactId>json-simple</artifactId>
  <version>1.1.1</version>
</dependency>

通过这个,我能够创建 return JSON

的操作
<dependency>
  <groupId>com.google.code.gson</groupId>
  <artifactId>gson</artifactId>
  <version>2.8.6</version>
</dependency>