Maven 全新安装不包括可执行 jar 文件的 sqlite 依赖项

Maven clean install not including sqlite dependency for executable jar file

使用后

mvn clean install

然后

java -jar executable.jar

我收到这个错误:

java.lang.ClassNotFoundException: org.sqlite.JDBC  

这是我的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>****</groupId>
<artifactId>****</artifactId>
<version>0.7-SNAPSHOT</version>
<packaging>jar</packaging>
<name>****</name>
<description>****</description>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>



<dependencies>
    <dependency>
        <groupId>org.xerial</groupId>
        <artifactId>sqlite-jdbc</artifactId>
        <version>3.23.1</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.junit/junit5-engine -->
    <dependency>
        <groupId>org.junit</groupId>
        <artifactId>junit5-engine</artifactId>
        <version>5.0.0-ALPHA</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.tinyjee.jgraphx/jgraphx -->
    <dependency>
        <groupId>org.tinyjee.jgraphx</groupId>
        <artifactId>jgraphx</artifactId>
        <version>3.4.1.3</version>
    </dependency>


</dependencies>

<build>
    <sourceDirectory>******</sourceDirectory>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2.1</version>
            <configuration>
                <mainClass>*****</mainClass>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.1.1</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>******</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

运行 intelliJ 中的程序运行没有问题。我从项目结构中将它包含在那里。

我用 **** 替换了名称和目录。这是一个学校项目,我不希望我的教授指责我向其他小组提供解决方案,以防他们找到这个 Whosebug 条目。

可能只有当你是 运行 你的 jar 时才会得到这个,因为依赖项不在 available/packaged 里面 .

尝试生成一个"fat jar"(也称为uber-jar),它将把你所有的依赖包打包到罐子:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <finalName>YOUR_JAR_FINAL_NAME</finalName>
            </configuration>
        </plugin>
    </plugins>
</build>

maven-shade-plugin 相关的文档可以是 found in here

由于您使用的是可运行的 jar 文件,因此您可以关注 this section of the documentation 可执行 Jars

相关的内容

一些背景。
Maven install 从不安装依赖项; 它只安装通过 POM 构建的项目。 依赖项的安装是您还必须执行的任务 如果您不使用 "fat jar" (我不推荐)或使用 新的 spring 启动可执行 jar 实现。

经典 "fat jar" 太可怕了 (但通常是唯一的选择) 此类问题的解决方案。

考虑使用Spring-Boot; 他们开发了一种新的, 理智的, 可执行 jar 文件的版本,包括可执行 jar 中的依赖项,并在启动时将它们添加到类路径中。 此功能类似于 "war" 文件添加到 一个 JEE 容器。

警告:我不为 Pivotal 工作, 我喜欢他们的大部分工作(Spring 框架)。

编辑: 这是一个 link 到 Executable Jar Section in the Spring Boot Reference。 它包含一些细节。