【问题标题】:Maven dependencies issue?Maven依赖问题?
【发布时间】:2020-10-21 23:58:17
【问题描述】:

我在 IntelliJ 上创建了一个 Maven 项目,它运行正常。现在,我想在没有 IntelliJ 的情况下使用 Docker 运行我的项目。

为此,我使用这个命令:docker run -it --rm --name my-maven-project -v /var/www/html/Recommandation/:/usr/src/mymaven -p 88:88 -w /usr/src/mymaven maven:latest /bin/bash -c "mvn clean install && java -jar target/Recommandation-1.0-SNAPSHOT.jar org.exemple.demo.App"

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  42.037 s
[INFO] Finished at: 2020-07-01T11:33:43Z
[INFO] ------------------------------------------------------------------------
Error: Unable to initialize main class org.exemple.demo.App
Caused by: java.lang.NoClassDefFoundError: io/vertx/core/Verticle

但是存在依赖关系解析问题。有什么想法吗?

这是我的 pom.xml :

<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.exemple.demo</groupId>
    <artifactId>Recommandation</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <defaultGoal>install</defaultGoal>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>org.exemple.demo.App</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <packaging>jar</packaging>

    <name>Recommandation</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>7</maven.compiler.source>
        <maven.compiler.target>7</maven.compiler.target>
    </properties>

    <dependencies>
    ...
    </dependencies>
</project>

【问题讨论】:

  • 这是一个独立的项目,还是您指的是项目中的另一个项目?
  • 不,因为我能够构建我的项目。在这里我只想用 Docker 运行它。 (我也可以使用 IntelliJ 运行它)。 @TarunLalwani 这是一个独立的项目,但有很多依赖项。我会把我的完整 pom.xml.
  • 我不能,对于 stackoverflow 的代码太多... :-/
  • 我也推荐你看看这篇文章,phauer.com/2019/no-fat-jar-in-docker-image

标签: java docker maven centos


【解决方案1】:

正如我所见,您没有将依赖项包含到您的 jar 中。

您需要为此构建一个可执行的 jar。

IntelliJ 允许您通过在后台拉取依赖项来运行 jar,但如果您想从命令行运行 jar,则需要包含其依赖项(或者您需要从外部目录引用它们)。

【讨论】:

  • 好的,我会尝试按照乔的链接的解释,如果我设法启动我的项目,我会放 RESOLVE。谢谢你的解释。
  • 它工作正常。实际上,在可执行 jar 文件中包含依赖项引用似乎更容易。感谢@Joe 的参考。
猜你喜欢
  • 1970-01-01
  • 2021-12-06
  • 2011-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-26
  • 2021-08-10
  • 2014-01-12
相关资源
最近更新 更多