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

时间:2019-02-07 16:20:12

标签: java maven sqlite executable-jar

使用后

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中运行该程序可以正常工作。我从项目结构中将其包括在内。

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

2 个答案:

答案 0 :(得分:2)

可能只有在运行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文件,因此可以遵循与{em>可执行jars

相关的this section of the documentation

答案 1 :(得分:1)

一些背景。
Maven安装永远不会安装依赖项; 它仅安装通过POM构建的项目。 依赖项的安装是您还必须执行的任务 如果您不使用“脂肪罐”(我不推荐)或使用 新的spring boot可执行jar实现。

经典的“胖子罐”真是太可怕了 (但通常是唯一的选择) 解决此类问题的方法。

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

注意:我不为Pivotal工作, 我喜欢他们的许多工作(Spring框架)。

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