gpt4 book ai didi

java - 将自定义依赖项添加到 spring 项目后缺少类

转载 作者:行者123 更新时间:2023-12-01 18:35:05 28 4
gpt4 key购买 nike

为了避免代码重复,我决定在基于spring的库项目中打包一些类。以下是我采取的步骤:

  1. 使用 springinitialzr 创建了项目。
  2. 创建了所需的结构并添加了类。
  3. 删除了主类
  4. 添加到 pom.xml 所需的依赖项
  5. 已添加到 pom.xml 插件 maven-jar-plugin

enter image description here

  • 创建了简单的脚本来自动打包和安装对本地 Maven 存储库的依赖项:
  •     #!/usr/bin/env bash

    POM="pom.xml"
    ARTIFACT="springbot-messenger-client"

    MVN_VERSION=$(mvn -q -f $POM \
    -Dexec.executable=echo \
    -Dexec.args='${project.version}' \
    --non-recursive \
    exec:exec)

    mvn package -f $POM
    mvn install:install-file \
    -Dfile="target/"$ARTIFACT"-"$MVN_VERSION".jar" \
    -DgroupId=ai.optime \
    -DartifactId=$ARTIFACT \
    -Dversion=$MVN_VERSION \
    -Dpackaging=jar

    echo "Version "$MVN_VERSION" of "$ARTIFACT" installed"

  • 脚本执行成功,我已将新创建的依赖项添加到另一个项目中。
  • 在应用程序启动期间,我遇到了异常:

    Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.dataformat.yaml.YAMLFactory

    YAMLFactory属于依赖项jackson-dataformat-yaml,它被添加到库项目的pom.xml中:

    enter image description here

    据我了解,mvn package不包含依赖项,那么如何正确包含它们?我知道 fat jar 的概念,但这可能不是正确的解决方案。

    最佳答案

    我用来编译 Spring 应用程序的是:

    <build>
    <plugins>
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>${maven-compiler.version}</version>
    <configuration>
    <source>11</source>
    <target>11</target>
    <release>11</release>
    </configuration>
    </plugin>
    <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
    <fork>true</fork>
    <mainClass>path.to.main.class</mainClass>
    </configuration>
    <executions>
    <execution>
    <goals>
    <goal>repackage</goal>
    </goals>
    </execution>
    </executions>
    </plugin>
    </plugins>
    </build>

    还有

    mvn clean install

    ,它将生成一个包含所有依赖项的 jar。

    希望对您有帮助。

    关于java - 将自定义依赖项添加到 spring 项目后缺少类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60076902/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com